我正在尝试创建一个联系人列表,每个联系人旁边都有一个复选框。 我找到了一个对我很有帮助的代码,但我仍然无法让它工作。 这是:
import java.util.ArrayList;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Toast;
public class ContactReadActivity extends ListActivity {
private ArrayList<Contact> contacts = null;
private ProgressDialog progressDialog = null;
private ContactAdapter contactAdapter = null;
private Runnable viewContacts = null;
private SparseBooleanArray selectedContacts = new SparseBooleanArray() ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
contacts = new ArrayList<Contact>();
this.contactAdapter = new ContactAdapter(this, R.layout.read_contacts, contacts);
setListAdapter(this.contactAdapter);
viewContacts = new Runnable(){
@Override
public void run() {
getContacts();
}
};
Thread thread = new Thread(null, viewContacts, "ContactReadBackground");
thread.start();
progressDialog = ProgressDialog.show(ContactReadActivity.this,"Please wait...", "Retrieving contacts ...", true);
}
private void getContacts(){
try{
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID
};
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, ContactsContract.Contacts.HAS_PHONE_NUMBER+"=?", new String[]{"1"}, ContactsContract.Contacts.DISPLAY_NAME);
contacts = new ArrayList<Contact>();
while(cursor.moveToNext()){
Contact contact = new Contact();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
contact.setContactName(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contacts.add(contact);
}
cursor.close();
runOnUiThread(returnRes);
}
catch(Exception e){
e.printStackTrace();
}
}
private Runnable returnRes = new Runnable() {
@Override
public void run() {
// close the progress dialog
if (progressDialog.isShowing())
progressDialog.dismiss();
contactAdapter.notifyDataSetChanged();
}
};
public class ContactAdapter extends ArrayAdapter<Contact> {
private ArrayList<Contact> items;
public ContactAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.read_contacts, null);
}
Contact contact = items.get(position);
if (contact != null) {
CheckBox nameCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
nameCheckBox.setChecked(selectedContacts.get(position));
if (nameCheckBox != null) {
nameCheckBox.setText(contact.getContactName());
}
nameCheckBox.setOnClickListener(new OnItemClickListener(position,nameCheckBox.getText(),nameCheckBox));
}
return view;
}
}
class OnItemClickListener implements OnClickListener{
private int position;
private CharSequence text;
private CheckBox checkBox;
OnItemClickListener(int position, CharSequence text,CheckBox checkBox){
this.position = position;
this.text = text;
this.checkBox = checkBox;
}
@Override
public void onClick(View arg0) {
selectedContacts.append(position, true);
Toast.makeText(getBaseContext(), "Clicked "+position +" and text "+text,Toast.LENGTH_SHORT).show();
}
}
public class Contact {
private String contactName;
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
}
}
我有两个XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
和
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<CheckBox android:text=""
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"/>
</RelativeLayout>
当然我获得了清单中的许可
<uses-permission android:name="android.permission.READ_CONTACTS"/>
有谁能告诉我我做错了什么?
谢谢!
答案 0 :(得分:1)
尝试使用ASyncTask并简化实施。
<强>活动强>
public class YourListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(..);
// DoStuff
ListView lv = getListView();
new LoadContacts().execute();
// DoStuff
}
}
<强>的AsyncTask 强>
class LoadContacts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Do Dialog stuff here
}
protected String doInBackground(String... args) {
// Put your implementation to retrieve contacts here
getContacts();
}
protected void onPostExecute(String file_url) {
// Dismiss Dialog
runOnUiThread(new Runnable() {
public void run() {
// Create list adapter and notify it
}
});
}
}
答案 1 :(得分:0)
代码似乎很好。我对ArrayAdapter不太满意,我更喜欢使用SimpleAdapter。在阅读了你的目的之后,我认为使用简单的适配器实现它似乎很容易。 Here's一个简单的启动。
关于ListView的更多内容,我认为最好的,总是帮助了我很多: http://www.vogella.com/articles/AndroidListView/article.html
更多关于SimpleAdapter,它非常简单: http://wptrafficanalyzer.in/blog/listview-with-images-and-text-using-simple-adapter-in-android/