如何在对话框中添加复选框列表视图

时间:2016-11-02 09:31:59

标签: android listview dialog android-checkbox

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

我的活动:

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

                 }


            });

我的适应者:

公共类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;
}

}

选中Box xml:

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

列表适配器:

公共类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;
}

//

}

1 个答案:

答案 0 :(得分:0)

首先在你的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删除已检查的项目Bean,并调用CustomAdapter的notifyDataStateChanged()