我正在尝试从活动中更改CheckBox
(使用自定义适配器放置在ListView
内)的状态。这甚至可能吗?
我正在尝试使用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;
}
}
答案 0 :(得分:1)
试试这个:
View rowView = listView.getChildAt(viewID);
if(rowView != null)
{
// do whatever you want here
ck = (CheckBox) v.findViewWithTag(tag);
ck.setChecked(true);
}
希望它能帮助!!
答案 1 :(得分:1)
自己解决了。
我之前的ArrayList
包含了每个checkbox
将显示的所有文字。我使用ArrayList
来比较所点击的checkbox
的值,然后使用更新的列表重新创建listview
的适配器。
谢谢大家的帮助。
for(Error temp : errorList)
{
if(temp.getDescription().equals(clicked))
{
temp.setChecked(true);
}
}
adapter = new RepairErrorListChkBoxAdapter(this, R.layout.repairerrorcontent, errorList, passedID);
lvError.setAdapter(adapter);
答案 2 :(得分:0)
尝试以下代码
for(int i=0; i < mListViewObject.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)mListViewObject.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.checkBox);
cb.setChecked(true);
}
mListViewObject是您的列表视图对象
以上代码适用于Activity而非适配器。我正在使用它。
使用上面代码中用于listview的布局更新RelativeLayout