单击同一行中的另一个项目时更改ListView项目可见性

时间:2016-03-04 11:56:46

标签: android listview

我有一个ListView,其中一个CheckBox和一个EditText。我希望在每行中检查EditText时显示CheckBox。目前,我已将EditText的展示率设置为GONE

这是我的Adapter类:

 private class MyCustomAdapter extends ArrayAdapter<Cheese> {

        private ArrayList<Cheese> cheeseList;

        public MyCustomAdapter(Context context, int textViewResourceId,
                ArrayList<Cheese> cheeseList) {
            super(context, textViewResourceId, cheeseList);
            this.cheeseList = new ArrayList<Cheese>();
            this.cheeseList.addAll(cheeseList);
        }

        private class ViewHolder {
            EditText code;
            CheckBox name;
        }

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

                ViewHolder holder = null;
                Log.v("ConvertView", String.valueOf(position));

                if (convertView == null) {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = vi.inflate(R.layout.cheese_info, null);

                    holder = new ViewHolder();
                    holder.code = (EditText) convertView
                            .findViewById(R.id.quantity);
                    holder.name = (CheckBox) convertView
                            .findViewById(R.id.checkBox1);
                    convertView.setTag(holder);

                    holder.name.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            CheckBox cb = (CheckBox) v;
                            Cheese country = (Cheese) cb.getTag();
                            Toast.makeText(
                                    getApplicationContext(),
                                    "Clicked on Checkbox: " + cb.getText() + " is "
                                            + cb.isChecked(), Toast.LENGTH_LONG)
                                    .show();
                            country.setSelected(cb.isChecked());
                            ViewHolder holder1 = (ViewHolder)v.getTag();
                            holder1.code.setVisibility(View.VISIBLE);

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

                Cheese country = cheeseList.get(position);
                holder.name.setText(country.getName());
                holder.name.setChecked(country.isSelected());
                holder.name.setTag(country);

                return convertView;

            }

        }

我尝试添加

ViewHolder holder1 = (ViewHolder)v.getTag();
holder1.code.setVisibility(View.VISIBLE);

CheckBox内点击。但应用程序正在崩溃,给出错误

  

java.lang.ClassCastException:com.example.viewpagersample.Cheese   无法施展   com.example.viewpagersample.MainActivity $ $ MyCustomAdapter ViewHolder

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。我必须得到父ViewGroup并找到 EditText并更改可见性,如下所示:

ViewGroup viewGroup=(ViewGroup)v.getParent();
EditText quantity=(EditText)viewGroup.findViewById(R.id.quantity);
quantity.setVisibility(View.VISIBLE);

答案 1 :(得分:0)

 private class MyCustomAdapter extends ArrayAdapter<Cheese> {

        private ArrayList<Cheese> cheeseList;

        public MyCustomAdapter(Context context, int textViewResourceId,
                ArrayList<Cheese> cheeseList) {
            super(context, textViewResourceId, cheeseList);
            this.cheeseList = new ArrayList<Cheese>();
            this.cheeseList.addAll(cheeseList);
        }

        private class ViewHolder {
            EditText code;
            CheckBox name;
        }

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

                ViewHolder holder = null;
                Log.v("ConvertView", String.valueOf(position));

                if (convertView == null) {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = vi.inflate(R.layout.cheese_info, null);

                    holder = new ViewHolder();
                    holder.code = (EditText) convertView
                            .findViewById(R.id.quantity);
                    holder.name = (CheckBox) convertView
                            .findViewById(R.id.checkBox1);
                    convertView.setTag(holder);


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

                Cheese country = cheeseList.get(position);
                holder.name.setText(country.getName());
                holder.name.setChecked(country.isSelected());
                holder.name.setTag(country);
holder.name.setOnClickListener(new Listener(country,holder.name,holder.code));
                return convertView;

            }
class Listener implements View.OnClickListener
    {
    Cheese country;
     CheckBox cb;
     EditText view;
    Listener(Cheese country, CheckBox cb,EditText view)
    {
    this.country = country;
    this.cb = cb;
    this.view = view;
    }

     @Override
        public void onClick(View v) {

                            Toast.makeText(
                                    getApplicationContext(),
                                    "Clicked on Checkbox: " + cb.getText() + " is "
                                            + cb.isChecked(), Toast.LENGTH_LONG)
                                    .show();
                            country.setSelected(cb.isChecked());
                            view.setVisibility(View.VISIBLE)

        }
    }

        }