列表视图与复选框,显示另一个列表视图上的已检查条目

时间:2015-08-21 11:43:46

标签: android listview listadapter

我有一个列表视图,它显示了汽车的图片,类型/描述以及一个复选框。当用户现在选择了汽车时,我想在另一个列表视图中显示它们。当他从CarListAdapter中取消选择他时,它也应该从新的列表视图中消失。我可以通过使用相同的Viewholder来实现这一点吗?

class CarListAdapter extends BaseAdapter
{

    ArrayList<ItemsHolder> holderList;
    BitmapHelper bitmapHelper = BitmapHelper.getInstance();


    public CarListAdapter(ArrayList<ItemsHolder> holderList)
    {
        this.holderList = holderList;
    }

    @Override
    public int getCount()
    {
        return holderList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

       final ViewHolder holder;

        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(RentingDataActivity.this);
            convertView = inflater.inflate(R.layout.listview_renting_entry, null, true);
            holder = new ViewHolder();
            holder.rowCarId = (TextView) convertView.findViewById(R.id.rowCarId);

            holder.rowImageView = (ImageView) convertView
                    .findViewById(R.id.rowImageView);
            holder.rowCarType = (TextView) convertView.findViewById(R.id.rowCarType);
            holder.rowCarDescription = (TextView) convertView
                    .findViewById(R.id.rowCarDescription);
            holder.rowCheckbox = (CheckBox) convertView.findViewById(R.id.rowCheckBox);
            convertView.setTag(holder);


            }


        else
        {
            holder = (ViewHolder) convertView.getTag();
            //  holder.rowCheckbox.setOnCheckedChangeListener(null);
      //      holder.rowCheckbox.setTag(R.id.rowCheckBox, position);
        }


   //     auto = holderList.get(position);
        final ItemsHolder ih = holderList.get(position);
        holder.rowImageView.setImageBitmap(bitmapHelper.getBigImage(ih.getImagePath(), 248));
        holder.rowCarType.setText(ih.getCarType());
        holder.rowCarDescription.setText(ih.getCarDescription());
        // holder.rowCheckbox.setTag(ih);

        holder.rowCarId.setText(ih.getCarId());
        holder.rowCheckbox.setChecked(ih.isSelected());

        holder.rowCheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkbox = (CheckBox)v.findViewById(R.id.rowCheckBox);

              //  Toast.makeText(getApplicationContext(), holder.rowCarType.getText().toString(),
              //  Toast.LENGTH_LONG).show();

                ih.setSelected(checkbox.isChecked());
            }
        });

       return convertView;
    }

}

1 个答案:

答案 0 :(得分:1)

我会将此添加为评论,但唉,我没有得到足够的分数:

添加到您的适配器;

notifyDataSetChanged( );

这会异步刷新列表。