我正在尝试使用复选框在android中实现自定义列表视图。 用户可以创建一组在复选框中签入的联系人 我需要帮助实现ViewHolder类,其次我如何为每个列表项分配联系人
ContactList单行XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<TextView
android:id="@+id/textViewContactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/textViewContactNameResource"
android:textSize="20sp"/>
<TextView
android:id="@+id/textViewContactNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/textViewContactNumberResource"
android:textSize="15sp"
android:layout_below="@+id/textViewContactName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/checkBoxContactSelection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_below="@+id/textViewContactName"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
[![在此处输入图像说明] [1]] [1]
InviteAndAddPeople.Java
public class InviteAndAddPeople extends ListActivity {
ListView listViewContacts;
Cursor cursor;
private static final int read_Contacts_Permission_Request = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invite_and_add_people);
this.setTitle("Invite Friends");
View view = getLayoutInflater().inflate(R.layout.invite_add_header,null);
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
startManagingCursor(cursor);
String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
//int[] to = {R.id.textViewContactName, R.id.textViewContactNumber};
ListAdapter listadapter = new CustomContactsAdapter(this,cursor ,from);
//SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this,R.layout.contacts_list_view,cursor,from,to);
setListAdapter(listadapter);
listViewContacts = getListView();
listViewContacts.addHeaderView(view);
//listViewContacts.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
//listViewContacts.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
}
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
}
@Override
public int getSelectedItemPosition() {
return super.getSelectedItemPosition();
}
@Override
public long getSelectedItemId() {
return super.getSelectedItemId();
}
}
CustomContactsAdapter.Java
public class CustomContactsAdapter extends ArrayAdapter<String> {
Context context;
String [] from;
Cursor cursor;
public CustomContactsAdapter(Context context,Cursor cursor ,String[] from) {
super(context, R.layout.contacts_list_view,from);
this.context = context;
this.from = from;
this.cursor = cursor;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
}
答案 0 :(得分:0)
问题是你没有处于generic_add
的状态,这就是为什么你没有得到你想要的选中值。
CheckBox
或List
的{{1}}或ArrayList
来存储数据。class
实例化并设置要查看的值。HashMap
标记设置为ViewHolder
内的ViewHolder
并从View
获取标记。getView()
的特定索引的值,并相应地检查View
。使用如下的适配器:
List
}