我尝试实现一个列表视图多选。我实现了在带有复选框项目的列表视图中显示我的所有联系人。我的问题是,当只检查一项时,会检查很多元素。能否请你帮忙?
public class SelectNewGroupChatMembers extends ListFragment implements
LoaderCallbacks<Cursor>, OnItemClickListener {
/** Defines a tag for identifying log entries */
private static final String TAG = "SelectNewGroupChatMembers";
/** The main query adapter */
private ContactsAdapter mAdapter;
/** ListView which will display contacts */
ListView mListView;
/** Fragments require an empty constructor. */
public SelectNewGroupChatMembers() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the main contacts adapter
mAdapter = new ContactsAdapter(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
mListView = (ListView)view.findViewById(android.R.id.list);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "clicked");
mListView.setAdapter(mAdapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView.setOnItemClickListener(this);
// Initialize the loader, and create a loader identified by ContactsQuery.QUERY_ID
getLoaderManager().initLoader(ContactsQuery.QUERY_ID, null, this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle savedInstanceState) {
// If this is the loader for finding contacts in the Content Provider
if (id==ContactsQuery.QUERY_ID) {
Uri contentUri = ContactsQuery.CONTENT_URI;
Log.i(TAG, contentUri.toString());
// Create a new CursorLoader with the following query parameters.
return new CursorLoader(getActivity(),
contentUri,
ContactsQuery.PROJECTION,
ContactsQuery.SELECTION,
null,
ContactsQuery.SORT_ORDER);
}
Log.i(TAG, "onCreateLoader - incorrect ID provided ( "+ id +" )");
return null;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// This swaps the new cursor into the adapter.
if (loader.getId() == ContactsQuery.QUERY_ID) {
Log.i(TAG,cursor.toString());
// The asynchronous load is complete and the data
// is now available for use. Only now can we associate
// the queried Cursor with the SimpleCursorAdapter.
mAdapter.swapCursor(cursor);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
if (loader.getId() == ContactsQuery.QUERY_ID) {
// When the loader is being reset, clear the cursor from the adapter.
// This allows the cursor resources to be freed.
mAdapter.swapCursor(null);
}
}
private class ContactsAdapter extends CursorAdapter {
private LayoutInflater mInflater;
public ContactsAdapter(Context context) {
super(context, null, 0);
// Stores the inflater for the later use
mInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
// Inflate the list item layout
final View itemLayout =
mInflater.inflate(R.layout.fragment_select_new_group_chat_members, viewGroup, false);
// Use ViewHolder design pattern to store each view resource.
// This allows bindView() to retrieve stored references instead of
// calling findViewById for each instance of the layout.
final ViewHolder holder = new ViewHolder();
holder.contactName = (TextView)itemLayout.findViewById(R.id.userName);
holder.contactNo = (TextView)itemLayout.findViewById(R.id.userInfo);
// Store the resourceHolder instance in Layout.
// This makes resourceHolder available to bindView
// and other methods that receive a handle to the item view
itemLayout.setTag(holder);
return itemLayout;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Get handles to individual view sources
final ViewHolder holder = (ViewHolder) view.getTag();
final String contactName = cursor.getString(ContactsQuery.DISPLAY_NAME);
final String contactNumber = cursor.getString(ContactsQuery.PHONE_NUMBER);
holder.contactName.setText(contactName);
holder.contactNo.setText(contactNumber);
}
private class ViewHolder {
public TextView contactName, contactNo;
}
}
public interface ContactsQuery {
// This query ID will be used in Loader
final static int QUERY_ID = 1;
// A content URI for the Contacts table
final static Uri CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
final static String SELECTION = null;
final static String SORT_ORDER = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
final static String [] PROJECTION = {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
SORT_ORDER,
};
final static int ID = 0;
final static int LOOKUP_KEY = 1;
final static int DISPLAY_NAME = 2;
final static int PHONE_NUMBER = 3;
final static int SORT_KEY = 4;
}
}
我的布局文件如下:
<?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="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<ImageView
android:id="@+id/userImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="6dip"
android:src="@drawable/ic_action_user" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/userImage"
android:layout_alignTop="@id/userImage"
android:layout_alignParentRight="true"
android:focusable="false"
android:gravity="center" />
<TextView
android:id="@+id/userName"
android:layout_alignTop="@id/userImage"
android:layout_toRightOf="@id/userImage"
android:layout_toLeftOf="@id/checkBox"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:gravity="center_vertical"
android:text="ContactName"
android:textStyle="bold" />
<TextView
android:id="@+id/userInfo"
android:layout_alignBottom="@id/userImage"
android:layout_toRightOf="@id/userImage"
android:layout_toLeftOf="@id/checkBox"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:gravity="center_vertical"
android:text="UserInfo" />
</RelativeLayout>
最后我的代码就像上面一样。此代码查询联系人并将它们显示在包含复选框的列表视图上。我尝试了一些不同的代码片段,但我所有的尝试都得到了相同的结果。我的问题是,当只检查一项时,会检查很多元素。你能帮忙吗?