我正在尝试从活动中更改 a 的状态CheckBox
(它被放置在ListView
带有自定义适配器的 a 中)。这甚至可能吗?
我正在尝试获取使用它View
的CheckBox
标签,但它返回 null。
View v = new View(this);
ck = (CheckBox) v.findViewWithTag(tag); //Here ck is null
ck.setChecked(true);
代码更新:
OnCreate()
{
....
populateListView(id);
....
}
private void populateListView(int id)
{
String errorDescriptionQuery = "SELECT error_description FROM " + TABLE_ERROR + " JOIN " + TABLE_REPAIR_ERROR + " ON " +
TABLE_REPAIR_ERROR + ".error_id = " + TABLE_ERROR + "._id" +
" WHERE " + TABLE_REPAIR_ERROR + ".repair_id = " + id;
errorList = new ArrayList<Error>();
Cursor c = db.rawQuery(errorDescriptionQuery, null);
if(c.getCount() > 1)
{
c.moveToFirst();
do
{
Error e = new Error();
e.setDescription(c.getString(0));
e.setChecked(false);
errorList.add(e);
}
while(c.moveToNext());
}
else if(c.getCount() == 1)
{
c.moveToFirst();
Error e = new Error();
e.setDescription(c.getString(0));
e.setChecked(false);
errorList.add(e);
}
else
{
//Exception - no errors found with that id.
}
while(c.moveToNext());
adapter = new RepairErrorListChkBoxAdapter(this, R.layout.repairerrorcontent, errorList, passedID);
lvError.setAdapter(adapter);
}
自定义适配器
public class RepairErrorListChkBoxAdapter extends BaseAdapter
{
String TABLE_ERROR = "tbl_Error";
String TABLE_MATERIAL = "tbl_Material";
DatabaseImplementation myDB;
SQLiteDatabase db;
private ArrayList<String> materialList;
ViewHolder holder;
int testcount = 0;
public ArrayList<Error> list;
private Context mContext;
private LayoutInflater mInflator;
private int repairID;
boolean moreThanOne;
public RepairErrorListChkBoxAdapter(Context context, int textViewResourceId, ArrayList<Error> list, int repairID)
{
this.mContext = context;
this.list = list;
this.repairID = repairID;
this.materialList = new ArrayList<String>();
myDB = new DatabaseImplementation(this.mContext);
db = myDB.getWritableDatabase();
mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int position)
{
return list.get(position);
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
holder = new ViewHolder();
convertView = mInflator.inflate(R.layout.repairerrorcontent, null);
//holder.text = (TextView)convertView.findViewById(R.id.txtErrorDesc);
holder.chk = (CheckBox)convertView.findViewById(R.id.chkSelected);
holder.chk.setOnClickListener(new MyClickListener(holder.chk));
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.chk.setText(" " + list.get(position).getDescription());
holder.chk.setClickable(true);
holder.chk.setFocusable(true);
holder.chk.setTag(testcount);
if(list.get(position).isChecked() == true)
{
holder.chk.setChecked(true);
}
else
{
holder.chk.setChecked(false);
}
String materialNameQuery = "SELECT material_name FROM " + TABLE_MATERIAL + ";";
Cursor getMaterialName = db.rawQuery(materialNameQuery, null);
getMaterialName.moveToFirst();
do
{
materialList.add(getMaterialName.getString(0));
}
while(getMaterialName.moveToNext());
return convertView;
}
public void changeCheckBoxState(String tag)
{
}
private class MyClickListener implements OnClickListener
{
CheckBox checkbox = null;
public MyClickListener(CheckBox cb)
{
checkbox = cb;
}
@Override
public void onClick(final View v)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Materials");
builder.setMessage("Did you require any materials to fix this error?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
v.setTag("clickedBox");
CheckBox cb = new CheckBox(mContext);
cb = (CheckBox) v;
String clickedError;
clickedError = cb.getText().toString() ;
String tag = (String) v.getTag();
Intent intent = new Intent(mContext, Material.class);
intent.putStringArrayListExtra("materialList", materialList);
intent.putExtra("clickedError", clickedError);
intent.putExtra("repairID", repairID);
intent.putExtra("tag", tag);
((Activity) mContext).startActivityForResult(intent, 1);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if(checkbox.getTag() == v.getTag())
{
checkbox.setChecked(true);
}
}
});
builder.show();
}
}
static class ViewHolder
{
CheckBox chk;
}
}