[英]Unchecking Checkboxes in Custom Listview
I want to unchecked my all check boxes inside listview on button click. 我想在按钮单击时取消选中列表视图中的所有复选框。 This my adapter class.
这是我的适配器类。 It unchecked all check boxes but for that I have to scroll through listview.
它取消选中所有复选框,但为此,我必须在列表视图中滚动。 When I scroll through it, it unchecked according to the rows that are visible.
当我滚动浏览它时,它会根据可见的行取消选中。
public class ExpenseCalculatorAdapter extends ArrayAdapter<String>{
private Activity context;
private static LayoutInflater inflator = null;
private ArrayList<String> data;
private ArrayList<String> values;
DataBaseUtil dbUtils;
public ExpenseCalculatorAdapter(Activity context, ArrayList<String> data){
super(context,R.layout.expenserow, data);
this.context = context;
this.data = data;
dbUtils=new DataBaseUtil(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String row=data.get(position);
String date;
String type;
String cost;
final String id;
/* result.add(c.getString(iRowid)+"&"+c.getString(iDate)+"~"+c.getString(iCost)+"#"+c.getString(iType)+"*");*/
id=row.substring(0, row.indexOf("&"));
date=row.substring(row.indexOf("&")+1,row.indexOf("~"));
cost=row.substring(row.indexOf("~")+1,row.indexOf("#"));
type=row.substring(row.indexOf("#")+1,row.indexOf("*"));
if(convertView==null)
{
ViewHolder holder = new ViewHolder();
inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.expenserow, null);
holder.togCheck=(CheckBox)convertView.findViewById(R.id.check);
holder.textDate = (TextView) convertView.findViewById(R.id.txtrowDate);
holder.textType = (TextView) convertView.findViewById(R.id.txtrowType);
holder.textCost = (TextView) convertView.findViewById(R.id.txtrowCost);
holder.togCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction("clicked");
if(buttonView.isChecked())
{
try
{
dbUtils.open();
dbUtils.setVisibility(id);
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
dbUtils.close();
}
intent.putExtra("isClicked","yes");
intent.putExtra("ID",""+id);
context.sendBroadcast(intent);
}
else
{
try
{
dbUtils.open();
dbUtils.setInVisibility(id);
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
dbUtils.close();
}
intent.putExtra("isClicked","no");
intent.putExtra("ID",""+id);
context.sendBroadcast(intent);
}
}
});
convertView.setTag(holder);
}
ViewHolder hold = (ViewHolder) convertView.getTag();
//setting Data to List
hold.textDate.setText(date);
hold.textType.setText(type);
hold.textCost.setText(cost);
if(CheckReceiver.checkAll)
{
hold.togCheck.setChecked(true);
}
if(!CheckReceiver.checkAll)
{
hold.togCheck.setChecked(false);
}
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
static class ViewHolder{
public CheckBox togCheck;
public TextView textDate;
public TextView textType;
public TextView textCost;
}
}
As Listview recycles view its not possible to do this with only BroadCastReciver. 由于Listview回收视图,因此仅使用BroadCastReciver不可能做到这一点。 It was really a bad idea.
这真是个坏主意。 I done this using Database and BroadCastReciver.
我使用Database和BroadCastReciver完成了此操作。
holder.togCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*Intent intent=new Intent();
intent.setAction("clicked")*/;
if(holder.togCheck.isChecked())
{
//holder.togCheck.setBackgroundResource(R.drawable.check);
try
{
dbUtils.open();
dbUtils.setVisibility(id);
// Toast.makeText(context, "Checked ", Toast.LENGTH_SHORT).show();
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
dbUtils.close();
}
intent.putExtra("isClicked","yes");
intent.putExtra("ID",""+id);
context.sendBroadcast(intent);
}
else
{
// holder.togCheck.setBackgroundResource(R.drawable.uncheck);
try
{
dbUtils.open();
dbUtils.setInVisibility(id);
Toast.makeText(context, "UnChecked ", Toast.LENGTH_SHORT).show();
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
dbUtils.close();
}
intent.putExtra("isClicked","no");
intent.putExtra("ID",""+id);
context.sendBroadcast(intent);
}
}
});
what i done is i set the checked or unchecked state of checkbox according to id in database. 我所做的是根据数据库中的ID设置复选框的选中或未选中状态。 Then by fetching state of that id i done calculation!.
然后通过获取该ID的状态,我完成了计算!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.