带有复选框的ListView在Android中使用多选 [英] ListView with checkbox using multichoice in android
本文介绍了带有复选框的ListView在Android中使用多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,当我单击按钮"b"时,一个列表视图填充了每个项目的复选框,现在我想让带有复选框的项目签入另一个活动,我如何实现这一目标,我半途而废我很困惑如何做剩余的部分
here is my code, when i click button "b" a listview populates with a checkbox for each item, now i want to get items with check box checked into another activity, how do i acheive this, i came half way i'm confused how to do the remaining part
这是我的代码 XML档案:
here is my code XML file :
<Button
android:id="@+id/b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pick contact" />
<ListView
android:choiceMode="multipleChoice"
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
这是Java代码:
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b;
int PICK_CONTACT;
ArrayList<String> al;
ArrayAdapter<String> aa;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.b);
lv = (ListView) findViewById(R.id.lv);
al = new ArrayList<String>();
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
displaycontacts();
System.out.println("size" + al.size());
aa = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_multiple_choice, al);
lv.setAdapter(aa);
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position ,long arg3)
{
//here i should get the item which is checked
}
});
}
public void displaycontacts() {
try {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name);
al.add(name);
}
}
}
} catch (Exception e) {
System.out.println("Error:::::::::::::::::::" + e);
}
}
}
推荐答案
尝试使用此链接进行多项选择的列表视图. http://android-coding.blogspot.in/2011 /09/listview-with-multiple-choice.html . 让我知道您的问题是否已解决?
try this link for list view with multiple choice. http://android-coding.blogspot.in/2011/09/listview-with-multiple-choice.html. Let me know your problem is resolved or not?
这篇关于带有复选框的ListView在Android中使用多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文