如何将复选框添加到对话框中的列表视图

how to add check boxes to list view in dialog box

我有一个列表视图,通过单击列表视图中的项目,我得到一个包含相同列表的对话框。现在我需要将复选框附加到列表,并且通过选择复选框我需要禁用项目。请帮我更新代码,如果有人帮助我修改代码,将不胜感激。

我的 ACTIVITY:

listView1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

                 @Override
                 public boolean onItemLongClick(AdapterView<?> parent, View view,
                 int position, long id) {
                 // TODO Auto-generated method stub
                  newListitems2.addAll(itemsList1);


                     dialog = new Dialog(PendingOrdersActitvity.this);
                        dialog.setContentView(R.layout.itembumping);

                        list1=(ListView )dialog.findViewById(R.id.list1);

                        ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this,newListitems2);
                        list1.setAdapter(adapter2);
                    dialog.show();

                     return(true);

                 }


            });

我的适配器:

public class ItemBumpingAdapter 扩展了 BaseAdapter {

ArrayList<String> childList = new ArrayList<String>();
ArrayList<String> qtychildList = new ArrayList<String>();
String parentobjid=null;

ArrayList<ItemsBean> newListitems = new ArrayList<ItemsBean>();
Context ctx;
LayoutInflater inflator;
ViewHolder holder;
int position;
public ItemBumpingAdapter(PendingOrdersActitvity itemActitvity,
        ArrayList<ItemsBean> newList1) {
    // TODO Auto-generated constructor stub
    this.ctx=itemActitvity;
    this.newListitems = newList1;
    this.inflator = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return newListitems.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}


public class ViewHolder {
    TextView qty, name, childText, qtyChild;
}
@Override

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub


                ViewHolder holder;
                String item = null, qty = null;
                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = inflator.inflate(R.layout.invoicelistadapter, null);
                    holder.qty = (TextView) convertView.findViewById(R.id.qty);
                    holder.name = (TextView) convertView.findViewById(R.id.item);
                    holder.childText = (TextView) convertView
                            .findViewById(R.id.childitem);
                    holder.qtyChild = (TextView) convertView
                            .findViewById(R.id.qtychild);
                    convertView.setTag(holder);

                } else {

                    holder = (ViewHolder) convertView.getTag();
                }
                parentobjid=newListitems.get(position).getParentobjectid();
                if(!parentobjid.isEmpty())
                {
                    holder.name.setText("   " +newListitems.get(position).getItemnNameDisplay());
                    holder.name.setTextColor(Color.parseColor("#CC0000"));
                    holder.qty.setText("      "+String.valueOf(newListitems.get(position)
                            .getQuantityDisplay()));
                    holder.qty.setTextColor(Color.parseColor("#CC0000"));
                }
                else
                {

                holder.name.setText(newListitems.get(position).getItemnNameDisplay());
                holder.qty.setText(String.valueOf(newListitems.get(position)
                        .getQuantityDisplay()));
                holder.name.setTextColor(Color.parseColor("#FFFFFF"));
                holder.qty.setTextColor(Color.parseColor("#FFFFFF"));
    }
    return convertView;
}

}

复选框 xml:

 <CheckBox 
    android:id="@+id/chckbox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    />

列表适配器:

public class CustomAdapter 扩展了 BaseAdapter {

Context ctx;
LayoutInflater inflator;
ArrayList<ItemsBean> newList = new ArrayList<ItemsBean>();
ArrayList<ItemsBean> newListitems = new ArrayList<ItemsBean>();
ArrayList<String> childList = new ArrayList<String>();
ArrayList<String> qtychildList = new ArrayList<String>();
String parentobjid=null;

PendingOrdersActitvity myactivity;
public CustomAdapter(PendingOrdersActitvity kdsActitvity,
        ArrayList<ItemsBean> invoiceDataList) {
    // TODO Auto-generated constructor stub
    this.ctx = kdsActitvity;


    this.newList = invoiceDataList;
    this.inflator = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



@Override
public int getCount() {
    // TODO Auto-generated method stub
    return newList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public static class ViewHolder {
    TextView qty, name, childText, qtyChild;

}

@SuppressLint("NewApi") @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    String item = null, qty = null;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflator.inflate(R.layout.invoicelistadapter, null);
        holder.qty = (TextView) convertView.findViewById(R.id.qty);
        holder.name = (TextView) convertView.findViewById(R.id.item);
        holder.childText = (TextView) convertView
                .findViewById(R.id.childitem);
        holder.qtyChild = (TextView) convertView
                .findViewById(R.id.qtychild);
        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();
    }


    parentobjid=newList.get(position).getParentobjectid();
    if(!parentobjid.isEmpty())
    {
        holder.name.setText("   " +newList.get(position).getItemnNameDisplay());
        holder.name.setTextColor(Color.parseColor("#CC0000"));
        holder.qty.setText("      "+String.valueOf(newList.get(position)
                .getQuantityDisplay()));
        holder.qty.setTextColor(Color.parseColor("#CC0000"));
    }
    else
    {

    holder.name.setText(newList.get(position).getItemnNameDisplay());
    holder.qty.setText(String.valueOf(newList.get(position)
            .getQuantityDisplay()));
    holder.name.setTextColor(Color.parseColor("#FFFFFF"));
    holder.qty.setTextColor(Color.parseColor("#FFFFFF"));

    }


return convertView;
}

//

}

所以首先在你 ItemsBean.java 添加一个标志来检查这个项目是否需要可见。

我们假设

    public class ItemsBean{

    //some old variables
    private boolean isChecked=false;

    public void setChecked(boolean isChecked){
       this.isChecked=isChecked;
    }

    public boolean isChecked(){
       return isChecked;
    }
   }

现在在 ItemBumpingAdapter 的 getView() 中

//You have to add checkbox in xml and in ViewHolder.
//And make sure that newListitems has all ItemsBean.isChecked set to false.


public View getView(final int position, View convertView, ViewGroup parent) {
 //Your previous implementations
holder.checkbox.setChecked(false);
holder.checkbox.settag(position);
holder.checkbox.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view){
int pos=view.gettag();//Cast object to integer

  newListitems.get(pos).setChecked(!newListitems.get(pos).isChecked());

}

});
return convertView;
}

现在单击对话框的“Ok”按钮,您需要检查 newListitems 的 isChecked 标志,准备新的临时列表并设置为 CustomAdapter.java 适配器,或者如果您有副本列出然后从 invoiceDataList 中删除选中的 itemsbean 并调用 CustomAdapter 的 notifyDataStateChanged()