这是我的代码,当我单击按钮“b”时,列表视图会为每个项目填充一个复选框,现在我想将带有复选框的项目签入另一个活动,我如何实现这一点,我走了一半我很困惑剩下的部分怎么做
这是我的代码 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" />
这是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);
}
}
}