Как сделать [textview] [imagebutton] [checkbox] как каждую строку в списке - PullRequest
0 голосов
/ 17 ноября 2010

Извините, что запутал людей.Я упростил вопрос, который является флажком.По сути, я хочу, чтобы такой вид списка был таким, я хочу, чтобы при нажатии на него устанавливался флажок.

[textview] [checkbox]

[textview] [checkbox]

[textview] [checkbox]

[textview] [checkbox]

Проблема в том, что флажок не может работать правильно.Когда я нажимал на флажок, он никогда не переключался.

Может кто-нибудь дать какие-либо предложения?Спасибо.

Это мои коды прямо сейчас:

public class MyAdapter extends SimpleAdapter {

    Map<Integer, Boolean> map;

    LayoutInflater mInflater;

    private List<? extends Map<String, ?>> mList;

    public MyAdapter(Context context, List<Map<String, String>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        map = new HashMap<Integer, Boolean>();
        mInflater = LayoutInflater.from(context);
        mList = data;
        for (int i = 0; i < data.size(); i++) {
            map.put(i, false);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.rapid_diagnosis_row, null);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.multiple_question);
        textView.setText((String) mList.get(position).get("value"));

        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
        checkBox.setChecked(map.get(position));

        // save position and checking status into tag
        checkBox.setTag(new int[] { position, checkBox.isChecked() == true ? 1 : 0 });

        checkBox.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                // get position and checking status from tag, works fine
                int[] tag = (int[]) (v.getTag());
                int p = tag[0];
                int c = tag[1];

                // try to get checking status from the clicked view, not works
                // which is always true
                boolean checked = ((CheckBox) v).isChecked();
                // try to toggle the clicked (checkbox) view, not works
                // the checkbox pressed never be toggled
                ((CheckBox) v).toggle();

                // save status into a hashmap, works fine
                mSimpleAdapter.map.put(p, (c == 1 ? true : false));
            }
        });

        return convertView;
    }

1 Ответ

0 голосов
/ 18 ноября 2010

Я решил эту проблему, заменив следующую строку toggle ():

public void onClick(View v) {
        // ((CheckBox) v).toggle();
        boolean checked = ((CheckBox) v).isChecked();
        if (checked) {
            ((CheckBox) v).setChecked(true);
        } else {
            ((CheckBox) v).setChecked(false);
        }
    }

Однако я до сих пор не понимаю, почему toggle () не работает.

Спасибо, ребята.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...