如何在嵌套的Click结构中获取复选框值

时间:2015-08-13 14:38:58

标签: android listview checkbox android-tabhost

我有一个ListView,上面有CheckBox。我正在使用Custom Adapter填充ListView。 我使用checkbox

绑定viewHolder数据
private static class PlanetArrayAdapter extends ArrayAdapter<Planet>
    {

        private LayoutInflater inflater;

        public PlanetArrayAdapter(Context context, List<Planet> planetList)
        {
            super(context, R.layout.simplerow, R.id.rowTextView, planetList);
            // Cache the LayoutInflate to avoid asking for a new one each time.
            inflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            // Planet to display
            Planet planet = (Planet) this.getItem(position);

            // The child views in each row.
            CheckBox checkBox;


            // Create a new row view
            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.simplerow, null);

                // Find the child views.

                checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);

                // Optimization: Tag the row with it's child views, so we don't
                // have to
                // call findViewById() later when we reuse the row.
                convertView.setTag(new PlanetViewHolder(textView, checkBox));

                // If CheckBox is toggled, update the planet it is tagged with.
                checkBox.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        CheckBox cb = (CheckBox) v;
                        Planet planet = (Planet) cb.getTag();
                        planet.setChecked(cb.isChecked());
                    }
                });
            }
            // Reuse existing row view
            else
            {
                // Because we use a ViewHolder, we avoid having to call
                // findViewById().
                PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
                        .getTag();
                checkBox = viewHolder.getCheckBox();

            }

            // Tag the CheckBox with the Planet it is displaying, so that we can
            // access the planet in onClick() when the CheckBox is toggled.
            checkBox.setTag(planet);

            // Display planet data
            checkBox.setChecked(planet.isChecked());


            return convertView;
        }

    }

FilterActivity.java

@Override
    public void onClick(View v) {
case R.id.sector:
                setWhiteBack_TextColor(sector);
                setWhiteText_BackgroundColor(status, needs, expertise, network);
                expertiseLayout = (LinearLayout) findViewById(R.id.linearHost);
                expertiseLayout.removeAllViews();
                expertiseLayout.addView(View.inflate(FiltersActivity.this, R.layout.expertiseradio, null));
                SectorlistView = (ListView) findViewById(R.id.list);
                try {
                    secAdapter = new sectorAdapter(FiltersActivity.this, R.layout.checkbox_item,arrayOfSector);
                if (arrayOfSector.isEmpty()) {
                }else {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            SectorlistView.setAdapter(secAdapter);
                        }
                    });
                }
                SectorlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                            CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
                            System.out.println("Checked ID :: " + cb.getId());
                            Toast.makeText(FiltersActivity.this, "Click item", Toast.LENGTH_SHORT).show();
                        }
                    });
        }catch(Exception e){
            e.printStackTrace();

        }
}

现在我没有得到这个Toast或点击事件

但问题是我在垂直方向上将其显示为TabHost。其中一个标签点击我显示此Listview

所以我无法在点击事件中获得点击事件。

或建议任何更好的方法来完成这项工作。

0 个答案:

没有答案