2013-08-22 38 views
2

我试图通过活动更改CheckBox(它位于具有自定义适配器的ListView内)的状态。这甚至有可能吗?活动中的复选框状态更改

我想要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; 
} 

} 
+0

http://lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html –

+0

@LalitPoptani回答感谢。这不是一个回收问题。我只需要设置一个复选框来检查活动。有没有办法将适配器添加的所有复选框保存到活动的List中? – ClaireG

+0

该帖子包括您的查询答案也:-) –

回答

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); 
1

试试这个:

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

希望它有助于!

+0

嗨,感谢您的答案。你只能在自定义适配器类中实现它吗?我需要从活动而不是适配器类setChecked。 – ClaireG

+0

如果你的活动有listview,你也可以从activity类中得到它。 –

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是您的列表视图对象

上面的代码工作在活动未适配器。我正在使用它。

更新的RelativeLayout与您正在使用的ListView布局在上面的代码

+0

不错的做法。尝试了它,并以某种方式lvError.getChildCount()是0.我有4项内。 – ClaireG

+0

朋友这段代码对我来说工作正常。这是确切的副本,我认为还有一些其他问题。只需检查一次你的代码。你在哪里添加上面的代码 –

+0

onActivityResult()。因为我需要根据另一项活动的结果进行上述操作。 – ClaireG

相关问题