2011-04-23 101 views
2

我是新来的Android,我试图做一个简单的应用程序,我必须在应用程序的ListView中显示电话簿中可用的联系人列表,我尝试添加复选框t每个项目。当用户选中复选框时,我想要检索该项目。到目前为止,我能够显示列表,但不清楚如何检查复选框检查事件。Android:如何点击ListView中的复选框时获取事件?

我的代码如下:

XML文件:

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

<ListView 
    android:id="@android:id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight = "1" 
    android:choiceMode="multipleChoice" android:clickable="true"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id ="@+id/savebutton" 
    android:text = "save" 
    /> 

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

<ListView 
    android:id="@android:id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight = "1" 
    android:choiceMode="multipleChoice" android:clickable="true"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id ="@+id/savebutton" 
    android:text = "save" 
    /> 

</LinearLayout> 

源代码:

public class testcontacts extends ListActivity 
{ 
    private ListAdapter adapter = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //CheckBox box = (CheckBox)findViewById(R.id.checkbox); 
     //box.setFocusable(false); 

     ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
       null, null, null, null); 

     adapter = new SimpleCursorAdapter 
     (this, R.layout.contact_entry, cur,new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[] {R.id.contactEntryText}); 
     setListAdapter(adapter); 

     ListView list = getListView(); 
     list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

    } 
} 

回答

0

您可以使用getCheckedItemPosition方法来获取被检查项目的位置。一旦拥有该位置,您就可以使用findViewById检索项目并检查isChecked方法。 下面是一个例子:

 int pos = list.getCheckItemPosition(); //list being a ListView previously defined 
    //Look for the checked CheckBox in the particular position 
    CheckBox checkBox = (CheckBox)findViewById(pos); 
    if (checkBox.isChecked()) { 
    //Do something here 
} 

希望它帮助。

0

这将使该项目的当前位置检查

.getCheckedItemPositions() 
+0

你好Soorya,谢谢你的回复,但我该如何捕捉事件?如何知道何时选中/取消选中复选框? – Madz 2011-04-24 13:11:48

0

在listView onclick项目中。 做这样的..

CheckBox chkBox = (CheckBox) viewRow.findViewById(R.id.checkBoxXML); 

此复选框将具有相同的状态,点击列表项复选框。

0

要创建收听点击列表视图的项目,你可以试试这个事件的事件:

list.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       CheckedTextView textView = (CheckedTextView)view; 
       textView.setChecked(!textView.isChecked()); 
       switchAlarm(textView.isChecked(),position); 
      } 
     }); 

此代码将改变项目的chacked状态。

我希望它有帮助。

相关问题