Item checked when created in android listview

跟風遠走 提交于 2019-12-25 06:47:41

问题


I am trying to create an alarm system for my android app, and I have a listview in which I am adding the alamrs. This listview adapter is an simple_list_item_multiple_choice. I want that when I add an alarm this appears as checked in the listview, but I have not found the solution for that.

This code is not working as I expected:

{
    ListView list = (ListView)findViewById(R.id.listView1);
    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, this.alarmList);
    list.setAdapter(adapter); 
    list.setItemChecked(itemPos, true); 
}

Do you know what else could I try?

Thanks in advance.


回答1:


Dude try this code i hope that is that you mean:

final SparseBooleanArray mSelectedItemsIds = new SparseBooleanArray();
final  ListView list = (ListView)findViewById(R.id.listView1);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_multiple_choice, this.alarmList);
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        list.setAdapter(adapter );
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //SparseBooleanArray Checked = list.getCheckedItemPositions();
                if(!mSelectedItemsIds.get(position)){
                   list.setItemChecked(position, true);
                    mSelectedItemsIds.put(position, true);
                }
                else{
                    mSelectedItemsIds.delete(position);
                    list.setItemChecked(position, false);
                }



            }
        });

Edit: i set the list as final, make sure you do it also if you didnt otherwise it wont work




回答2:


multiple_choice.xml

<CheckedTextView 
  xmlns:android="http://schemas.android.com/apk/res/android"     

  android:id="@+id/checkList" 
  android:paddingLeft="20dip" 
  android:paddingRight="20dip" 
  android:paddingTop="10dip"
  android:paddingBottom="10dip" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="?android:attr/listPreferredItemHeight"  
  android:gravity="center_vertical"  
  android:checkMark="?android:attr/listChoiceIndicatorMultiple"

in your java file

final ListAdapter ladapter=new ArrayAdapter<String>(getActivity(),
                     R.layout.multiple_choice, <string array>);

       listView.setAdapter(ladapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                listView.setItemChecked(2, true);

this will set the 3rd item in the listview to be checked.




回答3:


If I'm not mistaken, you still need to call adapter.notifyDataSetChanged().

This notifies the adapter to update the contents of the list to display the changes you made to it.



来源:https://stackoverflow.com/questions/21907598/item-checked-when-created-in-android-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!