如何在android中使用自定义适配器保存listview中的复选框状态

时间:2013-12-20 11:20:48

标签: android listview

我在列表视图中使用自定义适配器是可过滤的,我使用两个arraylists,一个用于过滤,第二个用于第一次出现在列表视图中。 它工作正常,我使用复选框我想保存复选框的状态,我正在使用哈希映射,它工作正常,但它花了很多时间加载有地图数组列表。保持复选框处于保存状态的最佳方法是什么。

下面是我的代码

这是我的hashmap和hashmap数组列表

static HashMap<String, String> map = new HashMap<String, String>();

static ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

在这个我的字符串数据来自SQLite数据库。它正常工作我正在使用它进行复选框保存状态。

我的数据来自sqlite的字符串数组:

text = dbOpenHelper.text;

然后我在列表视图中添加数据并从编辑文本中过滤它。

    edittext = (EditText) findViewById(R.id.EditText01);
    listview = (ListView) findViewById(R.id.ListView01);
    listview.setAdapter(new MyCustomAdapter(text, image));

    System.out
    .println("size of map"+map.size());

    System.out
    .println("size of list"+mylist.size());

    edittext.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            filter_flag = 1;
            textlength = edittext.getText().length();
            text_sort.clear();
            image_sort.clear();

            for (int i = 0; i < text.length; i++) {
                if (textlength <= text[i].length()) {
                    if (edittext
                            .getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) text[i].subSequence(0,
                                            textlength))) {
                        text_sort.add(text[i]);
                        image_sort.add(image[i]);
                    }
                }
            }

             listview.setAdapter(new MyCustomAdapter(text_sort, image_sort));
        }
    });

这是我剩下的代码

 listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapter, View v,
                int position, long id) {

            String str = text[position];

            if (filter_flag == 0) {

                Toast.makeText(getApplicationContext(),
                        "text[] = " + text[position], Toast.LENGTH_SHORT)
                        .show();

                Toast.makeText(getApplicationContext(),
                        "image[] = " + image[position], Toast.LENGTH_SHORT)
                        .show();

                Bitmap bitmap = BitmapFactory.decodeResource(
                        getResources(), image[position]);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] b = baos.toByteArray();

                System.out.println("imagesssss" + b);

                Intent intent = new Intent(AllBreeds.this,
                        BreedDescription.class);
                intent.putExtra("picture", b);
                startActivity(intent);
                // finish();

            } else {
                Toast.makeText(getApplicationContext(),
                        "text_sort = " + text_sort.get(position),
                        Toast.LENGTH_SHORT).show();

                Toast.makeText(getApplicationContext(),
                        "image_sort = " + image_sort.get(position),
                        Toast.LENGTH_SHORT).show();

                Bitmap bitmap = BitmapFactory.decodeResource(
                        getResources(), image_sort.get(position));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] b = baos.toByteArray();

                System.out.println("imagesssss" + b);

                Intent intent = new Intent(AllBreeds.this,
                        BreedDescription.class);
                intent.putExtra("picture", b);
                startActivity(intent);
                // finish();

            }
        }
    });
}

class MyCustomAdapter extends BaseAdapter {

    String[] data_text;
    int[] data_image;
    Boolean status;
    int count = 0;

    int len;
    private SparseBooleanArray mCheckStates;
    private SparseBooleanArray mFilter;

    ViewHolder holder = null;

    private class ViewHolder {

        ImageView imageview;

        TextView textview;
        CheckBox chkbox;
        Button btn_fav;
    }

    MyCustomAdapter() {
    }

    MyCustomAdapter(String[] text, int[] image) {
        data_text = text;
        data_image = image;

        mCheckStates = new SparseBooleanArray(data_text.length);
        mFilter = new SparseBooleanArray(text_sort.size());
    }

    MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image) {

        data_text = new String[text.size()];
        data_image = new int[image.size()];

        for (int i = 0; i < text.size(); i++) {
            data_text[i] = text.get(i);
            data_image[i] = image.get(i);
        }
    }

    public int getCount() {
        return data_text.length;
    }

    public String getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

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

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.listview, null);
            holder = new ViewHolder();

        }

        holder.textview = (TextView) convertView
                .findViewById(R.id.TextView01);
        holder.imageview = (ImageView) convertView
                .findViewById(R.id.ImageView01);

        holder.chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);

        holder.textview.setText(data_text[position]);
        holder.imageview.setImageResource(data_image[position]);
        holder.btn_fav.setTag(position);

        holder.chkbox.setTag(position);

        System.out.println("Example 3...");
        // weired way, but works anyway
        for (Object key : map.keySet()) {
            System.out.println("Key : " + key.toString() + " Value : "
                + map.get(key));
        }

        for (HashMap<String, String> map2 : mylist) {

            String Values = "", Query, Keys = "";
            int count = 0, count1 = 0;

            for (Entry<String, String> mapEntry : map2.entrySet()) {
                String key = mapEntry.getKey();
                String value = mapEntry.getValue();
                System.out
                        .println("iterator key: " + key + "value: " + value);
        }

        if (filter_flag == 0) {
            mCheckStates = new SparseBooleanArray(data_text.length);
            holder.chkbox.setChecked(mCheckStates.get(position, false));
        } else {
            mFilter = new SparseBooleanArray(text_sort.size());
            holder.chkbox.setChecked(mFilter.get(position, false));
        }

        holder.chkbox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // System.out.println("\n inside onCheckedChanged: ");

                CheckBox cb = (CheckBox) v;

                if (cb.isChecked() == true) {
                    // System.out.println("inside isChecked true");
                    // System.out.println("count= " + count);

                    if (count >= 2) {
                        // checked false (UnChecked)

                        // System.out.println("inside count>2: " + count);
                        cb.setChecked(false);

                        // String msg = "Please select only two cars!";
                        // showCustomToast(msg);

                        Toast.makeText(getApplicationContext(),
                                "Please select only two pet!",
                                Toast.LENGTH_LONG).show();

                        // Toast.makeText(getApplicationContext(),
                        // "Please select only two cars!",
                        // Toast.LENGTH_LONG).show();

                    } else {

                        count++;
                        // System.out.println("inside else: " + count);

                        // cb.setChecked(true);

                        if (filter_flag == 0) {
                            cb.setChecked(true);
                            mCheckStates.put((Integer) cb.getTag(), true);
                        } else {
                            cb.setChecked(true);
                            mFilter.put((Integer) cb.getTag(), true);
                        }

                        // mCheckStates.put((Integer) cb.getTag(), true);
                    }

                } else {

                    // checked false (UnChecked)
                    // System.out.println("inside isChecked false" + count);

                    // cb.setChecked(false);

                    if (filter_flag == 0) {
                        cb.setChecked(false);
                        mCheckStates.put((Integer) cb.getTag(), false);
                    } else {
                        cb.setChecked(false);
                        mFilter.put((Integer) cb.getTag(), false);
                    }

                    // mCheckStates.put((Integer) cb.getTag(), false);

                    count--;

                }

                int cntr = count;

                Toast.makeText(getApplicationContext(), "pet id===" + cntr,
                        Toast.LENGTH_LONG).show();
            }
        });

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

                Button b1 = (Button) v;
                status = favourites.get(position, false);
                // Boolean online = isOnline();

                if (status == true) {
                    holder.btn_fav
                            .setBackgroundResource(R.drawable.starfill);
                } else {
                    holder.btn_fav
                            .setBackgroundResource(R.drawable.starblank);
                }

                Toast.makeText(getApplicationContext(),
                        "star id===" + status, Toast.LENGTH_SHORT).show();

            }
        });

        return (convertView);
    }
}

0 个答案:

没有答案