[英]Android Custom Contact ListView With checkbox
我正在嘗試使用復選框在android中實現自定義listview。 用戶可以創建一組在復選框中選中的聯系人,我在實現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);
}
}
問題是您沒有保持CheckBox
的狀態,這就是為什么您沒有按需獲取檢查值的原因。
class
或HashMap
List
或ArrayList
來存儲數據。 ViewHolder
實例化並設置要查看的值。 ViewHolder
作為標簽來View
里面getView()
並從獲得的標簽View
。 List
的特定索引的值,並相應地檢查CheckBox
。 使用如下適配器:
public class CustomContactsAdapter extends ArrayAdapter<Employee> {
Context context;
ArrayList<Employee> employees;
LayoutInflater inflator;
public CustomContactsAdapter(Context context, ArrayList<Employee> employees) {
super(context, R.layout.list_item_employee);
this.context = context;
this.employees = employees;
inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
View view = null;
if (convertView == null) {
view = inflator.inflate(R.layout.list_item_employee, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.nameTextView = (TextView) view.findViewById(R.id.codeTextView);
viewHolder.experienceEditText = (EditText) view.findViewById(R.id.experienceEditText);
viewHolder.isMarriedCheckBox = (CheckBox) view.findViewById(R.id.marriedCheckBox);
viewHolder.nameTextView.setText(employees.get(position).getName());
// Text Changed Listener
viewHolder.experienceEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals("")) {
Employee employee = (Employee) viewHolder.experienceEditText.getTag();
employee.setExperienceYear(Integer.valueOf(s.toString()));
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
// Check Changed Listener
viewHolder.isMarriedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Employee employee = (Employee) viewHolder.isMarriedCheckBox.getTag();
employee.setIsMarried(buttonView.isChecked());
}
});
viewHolder.experienceEditText.setTag(employees.get(position));
viewHolder.isMarriedCheckBox.setTag(employees.get(position));
view.setTag(viewHolder);
} else {
view = convertView;
((ViewHolder) view.getTag()).experienceEditText.setTag(employees.get(position));
((ViewHolder) view.getTag()).isMarriedCheckBox.setTag(employees.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.experienceEditText.setText(String.valueOf(employees.get(position).getExperienceYear()));
if (employees.get(position).isMarried() == true)
holder.isMarriedCheckBox.setChecked(true);
else
holder.isMarriedCheckBox.setChecked(false);
holder.nameTextView.setText(employees.get(position).getName());
return view;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getPosition(Employee item) {
return super.getPosition(item);
}
@Override
public Employee getItem(int position) {
return employees.get(position);
}
@Override
public int getCount() {
return employees.size();
}
static class ViewHolder {
protected TextView nameTextView;
protected CheckBox isMarriedCheckBox;
protected EditText experienceEditText;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.