Saturday, October 8, 2011

2 lines text in single choice listview (Android)

Requirement: 
1, single choice
2, two lines text align center in vertical 
3, some items own one line text only, and it should also stay in center of vertical.
4, radio button should be checked when tap any position of the list item (radio button should not response users' click directly)



<listitem_single_check.xml>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
     android:layout_width="fill_parent" android:layout_height="wrap_content"    
     xmlns:android="http://schemas.android.com/apk/res/android">
     
     <LinearLayout android:layout_centerVertical="true"
         android:layout_width="fill_parent" android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:orientation="vertical" android:gravity="center_vertical">
         
         <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:id="@+id/tv_MainText"
              android:textAppearance="?android:attr/textAppearanceMedium" />
         <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:id="@+id/tv_SubText"
              android:textAppearance="?android:attr/textAppearanceSmall" />
     </LinearLayout>

     <RadioButton android:layout_height="wrap_content"
         android:id="@+id/rb_Choice" android:layout_width="wrap_content"  android:layout_centerVertical="true"
         android:layout_alignParentRight="true" android:gravity="center_vertical"
         android:focusable="false"  android:clickable="false"
        ></RadioButton>

</RelativeLayout>


java code:
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        m_this = this;
        
        //begin init test data
        final ArrayList<HashMap<String, Object>> m_data = new ArrayList<HashMap<String, Object>>();
        
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("maintext", "Large text 1");
        map1.put("subtext", "small text 1A");        
        m_data.add(map1);
        
        HashMap<String, Object> map2 = new HashMap<String, Object>();
        map2.put("maintext", "Large text 2");// no small text of this item!
        m_data.add(map2);
        
        HashMap<String, Object> map3 = new HashMap<String, Object>();
        map3.put("maintext", "Large text 3");
        map3.put("subtext", "small text 3");
        m_data.add(map3);
        
        for (HashMap<String, Object> m :m_data) //make data of this view should not be null (hide )
m.put("checked", false);
      //end init data
        
        
        final ListView lv = (ListView) m_this.findViewById(R.id.lv1);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        
        
        final SimpleAdapter adapter = new SimpleAdapter(m_this,
        m_data,
                R.layout.listitem_single_check,
                new String[] {"maintext", "subtext", "checked"}, 
                new int[] {R.id.tv_MainText, R.id.tv_SubText, R.id.rb_Choice}); 
        
        adapter.setViewBinder(new ViewBinder()
{
        public boolean setViewValue(View view, Object data, String textRepresentation)
        {
if (data == null) //if 2nd line text is null, its textview should be hidden 
{
view.setVisibility(View.GONE);
return true;
}
view.setVisibility(View.VISIBLE);
return false;
}

});
        

        // Bind to our new adapter.
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
RadioButton rb = (RadioButton) v.findViewById(R.id.rb_Choice);
if (!rb.isChecked()) //OFF->ON
{
for (HashMap<String, Object> m :m_data) //clean previous selected
m.put("checked", false);
m_data.get(arg2).put("checked", true);
adapter.notifyDataSetChanged();
}
}        
});
        
        //show result
        ((Button)m_this.findViewById(R.id.Button01)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int r = -1;
for (int i = 0; i < m_data.size(); i++) //clean previous selected
{
HashMap<String, Object> m = m_data.get(i);
Boolean x = (Boolean) m.get("checked");
if (x == true)
{
r = i;
break; //break, since it's a single choice list
}
}
new AlertDialog.Builder(m_this).setMessage("you selected:"+r).show();
}
});
        
    }  



6 comments:

  1. As a business head, your prime objective would be to promote your products or service far and wide. Using online scheduling software is another effective to promote your business as your company would be listed in a director and it would appear in the search engine results page. driver toolkit key

    ReplyDelete
  2. Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

    Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  4. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  5. Eventually, android phones are not just a good option from product point of view but also it is providing employment opportunities on a wide scale.teleprompter

    ReplyDelete