开发者

How to make list view as multiselectable

In screen layout as I declared my list view as

 <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:text="No da开发者_如何学Cta"/>

And row layout is as

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

     <TextView android:id="@+id/text1"
         android:textSize="16sp"
         android:textStyle="bold"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/text2"
         android:textSize="16sp"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>
 </LinearLayout>

My problem is I want to make my list view as multi selected, but when I add attribute in list view android:choiceMode="multipleChoice" it doesn't work. And one way is there, with java code but when I changed id of listview android:id="@id/multi_selectable_list as before android:id="@id/android:list then at loding time of layout it gives error. Is there any way, or in same way I am doing something wrong. Actually I want multisectable list view, rules are

  1. there is a button if I clicked on that list should be change in multiselected.
  2. By default list view can not multiselectable.


You can add this method for being listview multiselect:


listView.setChoiceMode (int choiceMode);//for multiple choiceMode=2,For single choiceMode=1;for none cj\hoicMode=0;


If you mean that you wish to move from clicking the whole row item to clicking each child element separately, then I think this happens automatically [android 2.1].Try assigning an OnClickListener to each view separately. if you wish to get the row item then get the view's parent by using v.getParent().getParent().

//this is the onItemClick() not in use now

public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
{
    t.show();

    Object a = parent.getAdapter().getItem(position);

    if(a instanceof YourListItem)
        {
            YourListItem yourListItem = (YourListItem) a;

        }
    else
    {

    }
}

// this onClick function reconstructs it

public void onClick(View v) 
{
    if(v.getId() == R.id.b_searchSaved)
    {
        context.startActivity(new Intent(context, Search.class));
    }
    else if(v.getId() == R.id.savedRowItemText)
    {
        t.show();

        //there must be some more efficient way to do this
        //i'm reconstructing data from onItemClick to find out position and item [route]
        AdapterView<?> parent = (AdapterView<?>) v.getParent().getParent();

        int position = parent.getPositionForView(v);

        Object a = parent.getAdapter().getItem(position);

        if(a instanceof YourListItem)
        {
            YourListItem yourListItem = (YourListItem) a;

        }
    }
    else
    {

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜