当我检查CheckBox
中的项目时,还会检查其他项目。
我的应用有ListView
个联系人姓名和CheckBox
。此外,用户可以多选列表中的名称。
我有3个班级
感谢您的帮助。
ChoisirContactActivity
public class ChoisirContactActivity extends ListActivity implements View.OnClickListener {
// ArrayList
ArrayList<SelectUser> selectUsers;
SelectUser selectUser;
List<SelectUser> temp;
// Contact List
ListView listView;
// Cursor to load contacts list
Cursor phones, email;
// Pop up
ContentResolver resolver;
SearchView search;
SelectUserAdapter adapter;
private SelectUserManager usermanager;
String arrData[][];
private Button btnSend;
private Poste poste;
private String dest1, dest2, dest3, dest4, dest5, yuyu;
private CheckBox check;
private PosteManager postmanager;
private Intent poste2Intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choisir_contact);
selectUsers = new ArrayList<SelectUser>();
resolver = this.getContentResolver();
// listView = (ListView) findViewById(R.id.list);
usermanager = new SelectUserManager(this);
phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
LoadContact loadContact = new LoadContact();
loadContact.execute();
Intent data = getIntent();
poste = data.getParcelableExtra("yuyu");
btnSend = (Button) findViewById(R.id.btSend);
btnSend.setOnClickListener(this);
postmanager = new PosteManager(this);
}
@Override
public void onClick(View v) {
dest1 = "Mehdi";
dest2 = "Bouma";
dest3 = "Benani";
dest4 = "Anis";
dest5 = "Saad";
poste.setDest1(dest1);
poste.setDest2(dest2);
poste.setDest3(dest3);
poste.setDest4(dest4);
poste.setDest5(dest5);
postmanager.openForWrite();
long res = postmanager.createPoste(poste);
SparseBooleanArray sp=getListView().getCheckedItemPositions();
String str="";
for(int j = 0;j<sp.size();j++)
{
str+=arrData[sp.keyAt(j)][1]+",";
check = (CheckBox)findViewById(R.id.check);
check.setChecked(true);
}
Toast.makeText(getApplicationContext(), ""+str, Toast.LENGTH_SHORT).show();
poste2Intent = new Intent(ChoisirContactActivity.this, MainActivity.class);
startActivity(poste2Intent);
}
// Load data on background
class LoadContact extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Get Contact list from Phone
if (phones != null) {
Log.e("count", "" + phones.getCount());
if (phones.getCount() == 0) {
Toast.makeText(ChoisirContactActivity.this, "No contacts in your contact list.", Toast.LENGTH_LONG).show();
}
while (phones.moveToNext()) {
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String EmailAddr = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA2));
SelectUser selectUser = new SelectUser();
selectUser.setName(name);
selectUser.setPhone(phoneNumber);
selectUser.setEmail(EmailAddr);
usermanager.openForWrite();
long res = usermanager.createUser(selectUser);
selectUsers.add(selectUser);
}
} else {
Log.e("Cursor close 1", "----------------");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
final DbHelper myDb = new DbHelper(ChoisirContactActivity.this);
arrData = myDb.SelectAllDataUser();
listView = getListView();
adapter = new SelectUserAdapter(ChoisirContactActivity.this, arrData);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(5, true);
listView.setFastScrollEnabled(true);
// Select item on listclick
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("search", "here---------------- listener");
adapterView.getItemAtPosition(i);
SelectUser data = selectUsers.get(i);
data = usermanager.getUser(data, selectUsers);
yuyu = data.getName();
Toast.makeText(getApplicationContext(), "Uri:" + yuyu, Toast.LENGTH_LONG).show();
check = (CheckBox)view.findViewById(R.id.check);
check.setChecked(true);
}
});
listView.setFastScrollEnabled(true);
}
}
@Override
protected void onStop() {
super.onStop();
phones.close();
}
}
SelectUserAdapter
public class SelectUserAdapter extends BaseAdapter{
private Context context;
private String[][] lis;
public SelectUserAdapter(Context c, String[][] li) {
context = c;
lis = li;
}
public int getCount() {
if(lis != null) {
return lis.length;
}else{
return 0;
}
}
// @Override
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView name;
CheckBox box;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_row, null);
}
TextView txtName = (TextView) convertView.findViewById(R.id.name);
txtName.setPadding(10, 0, 0, 0);
txtName.setText(lis[position][1].toString());
return convertView;
}
}
SelectUserManager
public class SelectUserManager {
private SelectUser user;
private DbHelper dbHelper;
private Cursor cursor;
private Context context;
private SQLiteDatabase db;
private ContentValues contentValues;
private ArrayList<SelectUser> allUsers;
int iName, iPhone, iEmail;
private String[] columns = new String[]{DbHelper.KEY_USER,DbHelper.NAME,DbHelper.PHONE,DbHelper.EMAIL};
public SelectUserManager(Context context){
this.context = context;
dbHelper = new DbHelper(context);
contentValues = new ContentValues();
}
public SelectUserManager openForWrite(){
db = dbHelper.getWritableDatabase();
return this;
}
public SelectUserManager openForRead(){
db = dbHelper.getReadableDatabase();
return this;
}
public void close(){
dbHelper.close();
}
public long createUser(SelectUser user){
contentValues.put(DbHelper.NAME, user.getName());
contentValues.put(DbHelper.PHONE, user.getPhone());
contentValues.put(DbHelper.EMAIL, user.getEmail());
return db.insert(DbHelper.TABLE_USER, null, contentValues);
}
public int updateUser(SelectUser user) {
String[] whereArgs = {user.getName()};
openForWrite();
contentValues.put(DbHelper.NAME, user.getName());
contentValues.put(DbHelper.PHONE, user.getPhone());
contentValues.put(DbHelper.EMAIL, user.getEmail());
return db.update(DbHelper.TABLE_USER, contentValues, DbHelper.NAME + "= ?", whereArgs);
}
public int deleteUser(SelectUser user){
return db.delete(DbHelper.TABLE_USER, DbHelper.NAME + "=" + user.getName(), null);
}
public ArrayList<SelectUser> getAllUsers(){
openForRead();
allUsers = new ArrayList<SelectUser>();
cursor = db.query(DbHelper.TABLE_USER, columns, null, null, null, null, null, null);
iName = cursor.getColumnIndex(DbHelper.NAME);
iPhone = cursor.getColumnIndex(DbHelper.PHONE);
iEmail = cursor.getColumnIndex(DbHelper.EMAIL);
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
user = new SelectUser(cursor.getString(iName),cursor.getString(iPhone), cursor.getString(iEmail));
allUsers.add(user);
}
return allUsers;
}
public SelectUser getUser(SelectUser user, ArrayList<SelectUser> allUsers){
openForRead();
cursor = db.query(DbHelper.TABLE_USER, columns, null, null, null, null, null);
iName = cursor.getColumnIndex(DbHelper.NAME);
iPhone = cursor.getColumnIndex(DbHelper.PHONE);
iEmail = cursor.getColumnIndex(DbHelper.EMAIL);
if(cursor!= null){
cursor.moveToPosition(allUsers.indexOf(user));
user = new SelectUser(cursor.getString(iName),cursor.getString(iPhone), cursor.getString(iEmail));
}
return user;
}
}
答案 0 :(得分:0)
您正在检查OnItemClickListener中的复选框。那不行。
在适配器中,您有每个项目在<form>
中显示的字符串。您还需要一个布尔数组/列表来跟踪每个项目的复选框状态。
在适配器TextView
中,您需要获取该位置的checked-state布尔值,然后找到复选框视图并根据布尔值设置复选框状态。
在getView()
中,您需要更新该位置的布尔值,并在适配器上调用OnItemClickListener
。
我建议您阅读notifyDataSetChanged()
并真正了解回收视图时发生的情况,以便了解每个子视图的状态必须保存在适配器中的原因。
ListView
然后在你的活动中:
public class SelectUserAdapter extends BaseAdapter{
private Context context;
private String[][] lis;
private boolean[] checkedState;
public SelectUserAdapter(Context c, String[][] li) {
context = c;
lis = li;
checkedState = new boolean[lis.length];
}
// ...
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_row, parent, false);
}
TextView txtName = (TextView) convertView.findViewById(R.id.name);
txtName.setPadding(10, 0, 0, 0);
txtName.setText(lis[position][1].toString());
CheckBox cb = (CheckBox) convertView.findViewById(R.id.check);
cb.setChecked(checkedState[position]);
return convertView;
}
public void markSelected(int position) {
checkedState[position] = true;
notifyDataSetChanged(); // refresh the ListView
}
}