Ani*_*udh 3 checkbox android listview
这是我的代码,当我点击按钮"b"时,listview填充每个项目的复选框,现在我想获取带有复选框的项目检查到另一个活动,我如何实现这一点,我来了一半我很困惑如何做剩下的部分
这是我的代码XML文件:
<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" />
Run Code Online (Sandbox Code Playgroud)
这是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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
尝试此链接以获得多个选择的列表视图.http://android-coding.blogspot.in/2011/09/listview-with-multiple-choice.html.让我知道你的问题是否得到解决?