2

我正在尝试创建一个ListView包含目录名称。ListView包含一个用于显示TextView文件夹名称和一个checkbox. 列表已成功创建,但是当我向上/向下滚动列表时,我遇到了复选框状态的问题。复选框的状态随机变化。为了保存复选框的状态,我使用了一个HashMap<String, Boolean>,字符串来连接目录的名称和布尔值 - 复选框状态。

下面是代码(对不起,长度): MainActivity.java

public class MainActivity extends ListActivity implements OnClickListener {

    Button done, cancel, selectAll;

    ArrayList<String> m_list;
    HashMap<String, Boolean> m_checkedMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        m_checkedMap = new HashMap<String, Boolean>();

        done = (Button) findViewById(R.id.bDone);
        cancel = (Button) findViewById(R.id.bCancel);
        selectAll = (Button) findViewById(R.id.bSelAll);
        done.setOnClickListener(this);
        cancel.setOnClickListener(this);

        new AsyncHandler(this).execute();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Toast.makeText(this, m_list.get(position), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.bDone:
            //Update CheckedMap in DB
            Toast.makeText(this, m_checkedMap.values().toString(),
                    Toast.LENGTH_LONG).show();
            break;
        case R.id.bCancel:
            this.finish();
            break;
        }
    }

    public class AsyncHandler extends AsyncTask {

        Context context;
        public AsyncHandler(Context c) {
            context = c;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(context, "In onPreExecute()", Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        protected Object doInBackground(Object... arg0) {
            getList();
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            // super.onPostExecute(result);
            setListAdapter(new ElementAdapter(context, m_list, m_checkedMap));
        }

        private void getList() {
            m_list = new ArrayList<String>();
            File root = new File("/");
            File[] files = root.listFiles();
            for (File f : files) {
                if (f.isDirectory() && !f.isHidden()) {
                    m_list.add(f.getName());
                    m_checkedMap.put(f.getName(), false);
                }
            }
            Collections.sort(m_list);
        }
    }
}

适配器类

public class ElementAdapter extends ArrayAdapter {

    LinearLayout rowLayout;
    ArrayList<String> items = new ArrayList<String>();
    TextView tvElement;
    HashMap<String, Boolean> checkedMap;
    CheckBox cBox; 
    Context context;

    public ElementAdapter(Context c, ArrayList<String> elements,
            HashMap<String, Boolean> m_checkedMap) {
        super(c, R.layout.row, elements);
        this.items = elements;
        this.checkedMap = m_checkedMap;
        this.context = c;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.row, null);
        }

        rowLayout = (LinearLayout) view.findViewById(R.id.row);
        tvElement = (TextView) view.findViewById(R.id.tvElement);
        tvElement.setText(items.get(position));
        cBox = (CheckBox) view.findViewById(R.id.checkBox1);
        cBox.setOnClickListener(new OnClickListener() {
            public void onClick(View paramView) {
                if (cBox.isChecked()) {
                    //checkedMap.put(tvElement.getText().toString(), false);
                    checkedMap.put(items.get(position), false);
                } else if (!cBox.isChecked()) {
                    //checkedMap.put(tvElement.getText().toString(), true);
                    checkedMap.put(items.get(position), true);
                }
            }
        });
        cBox.setChecked(checkedMap.get(items.get(position)));
        return view;
    }
}
4

1 回答 1

3

出于与 ListView 滚动 CheckBox 问题相同的原因,我创建了一篇博客文章, ListView with CheckBox Scrolling Issue

于 2012-07-11T10:30:12.247 回答