2

我正在尝试从活动中更改 a 的状态CheckBox(它被放置在ListView带有自定义适配器的 a 中)。这甚至可能吗?

我正在尝试获取使用它ViewCheckBox标签,但它返回 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;
}

}
4

3 回答 3

1

自己解决了。

我以前有一个ArrayList包含每个将呈现的所有文本checkbox。我用它ArrayList来比较 clicked 的值,然后用更新的列表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);
于 2013-08-22T09:17:01.473 回答
1

尝试这个:

View rowView = listView.getChildAt(viewID);
  if(rowView != null)
  {
     // do whatever you want here
     ck = (CheckBox) v.findViewWithTag(tag);
     ck.setChecked(true);
  }

希望能帮助到你!!

于 2013-08-22T07:43:37.427 回答
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 而不是适配器。我正在使用它。

在上面的代码中使用您用于列表视图的布局更新 RelativeLayout

于 2013-08-22T07:57:27.520 回答