我遇到复选框问题。那么两个问题。
- 当我选中复选框中的项目时,其他项目也被选中。
- 我无法将名称(在检查后)捕获到 SQLiteDatabase 表中。
我的应用程序有一个联系人姓名列表视图(从 android 内置联系人列表中检索)和复选框。另外,用户可以多选列表中的名称。
我在一节课中有两节课
1. public class ContactsList
2.public class MyCustomAdapter
这是我的代码:ContactsList.java -更新了我的代码
公共类 ContactsList 扩展 ListActivity { static ArrayList friendsList = new ArrayList();
ListView list;
private ContactsList[] friends;
private ArrayAdapter<ContactsList> listAdapter;
final Context context = this;
Cursor cursor;
String[] buddiesList =
{"Kanak Priya",
"Joanne Liew",
"Michelle Lam",
"Teo Kin Hua",
"Melissa Haiting",
"David Yeo",
"Natasha Akhbar",
"Gillian Gan",
"Sonia",
"Ms Long",
"Joan Tang",
"Stephanie",
"Nur Ashiqin"
};
BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setListAdapter(new MyCustomAdapter(ContactsList.this, R.layout.contacts_list, buddiesList));
setContentView(R.layout.contacts_list);
setListAdapter(new ArrayAdapter<String>(this, R.layout.contacts_list, buddiesList));
ListView list = (ListView) findViewById(android.R.id.list);
//ListView list = getListView();
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
ContactsList friends = listAdapter.getItem(position);
friends.**toggleChecked**();
ContactsListViewHolder viewHolder = (ContactsListViewHolder) view.getTag();
viewHolder.**getCheckBox**().setChecked(friends.**isChecked**());
Cursor cursor = null;
cursor = (Cursor) parent.getItemAtPosition(position);
Intent intent = new Intent(ContactsList.this, Create_Events.class);
intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
startActivity(intent);
}
});
ContactsList[] arrContacts = friendsList.toArray(new ContactsList[0]);
Uri allContacts = Uri.parse("content://contacts/people");
Cursor c = managedQuery(allContacts, null, null, null, null);
String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
int[] views = new int[] {R.id.contactName};
startManagingCursor(c);
SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
this.setListAdapter(friendsAdapter);
}
private static class ContactsName
{
private String name = "";
private boolean checked = false;
public ContactsName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public boolean isChecked()
{
return checked;
}
public void setChecked(boolean checked)
{
this.checked = checked;
}
public String toString()
{
return name;
}
public void toggleChecked()
{
checked = !checked;
}
}
private static class ContactsListViewHolder
{
private CheckBox nameCheck;
private TextView contactName;
public ContactsListViewHolder(TextView contactName, CheckBox nameCheck)
{
this.nameCheck = nameCheck;
this.contactName = contactName;
}
public CheckBox getNameCheck()
{
return nameCheck;
}
public TextView getContactName()
{
return contactName;
}
}
private static class ContactsCustomAdapter extends ArrayAdapter<ContactsList>
{
private LayoutInflater inflater;
public ContactsCustomAdapter(Context context, List<ContactsList> friendsList)
{
**super(context, R.layout.contacts_list, R.id.contactName, R.id.contactCheckbox, friendsList);**
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ContactsList friends = (ContactsList) this.getItem(position);
CheckBox nameCheck;
TextView contactName;
if(convertView == null)
{
convertView = inflater.inflate(R.layout.contacts_list, null);
contactName = (TextView) convertView.findViewById(R.id.contactName);
nameCheck = (CheckBox) convertView.findViewById(R.id.contactCheckbox);
convertView.setTag(new ContactsListViewHolder(contactName, nameCheck));
nameCheck.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
ContactsList friends = (ContactsList) cb.getTag();
friends.**setChecked**(cb.isChecked());
if(cb.isChecked())
{
friendsList.add(friends.**name**);
}
else
{
friendsList.remove(friends.**name**);
}
}
});
}
else
{
ContactsListViewHolder viewHolder = (ContactsListViewHolder) convertView.getTag();
nameCheck = viewHolder.**getCheckBox**();
contactName = viewHolder.**getTexView**();
}
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
buddyDB.open();
long name_id;
super.onListItemClick(l, v, position, id);
Cursor cursor = null;
cursor = (Cursor) l.getItemAtPosition(position);
Intent intent = new Intent(ContactsList.this, Create_Events.class);
intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
startActivity(intent);
ListView list = getListView();
list.setChoiceMode(2);
list.setTextFilterEnabled(true);
l.setItemChecked(position, l.isItemChecked(position));
Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
c.moveToPosition(position);
TextView contactName = (TextView) v.findViewById(R.id.contactName);
String NameValue = contactName.getText().toString();
name_id = buddyDB.insertNames(NameValue);
Toast.makeText(getBaseContext(),
"Selected: " + buddiesList[position], Toast.LENGTH_SHORT).show();
buddyDB.close();
}
}
注意
我加粗的代码意味着这些是我遇到的错误。它要求为isChecked(), toggleChecked(), setChecked() and getCheckBox()
.
要求为其创建字段类型或常量类型,name
但我已经创建了它
private static class ContactsName
{
private String name = "";
我不确定我在哪里编码错误。我需要能够将名称插入数据库表中,并且我想确保当我检查一个名称时,不应该检查其他名称。任何人都可以帮助我吗?
谢谢!=)