0

I am using custom adapter in my list view that is filterable and I am using two arraylists one is for filterable and second one is for when it come first time in list view. It work properly, and I am using checkbox I want to save the state of the checkbox for that I am using hash map, it work properly but it taking to much time to load the has map array list. What is the best way to keep the checkbox in save state.

below is my code

this is my hashmap and hashmap array list

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

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

in this my string data is coming from SQLite database. It work properly I am using it for checkbox save state.

My data is coming in the string array from sqlite:

text = dbOpenHelper.text;

and after that I am adding the data in list view and filtering it from edit 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));
        }
    });

and this is my rest of the code

 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);
    }
}
Simulant
  • 19,190
  • 8
  • 63
  • 98
  • 1
    http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button. check this – Raghunandan Dec 20 '13 at 11:22
  • The simplest solutions to solve this is to enable stable IDs http://stackoverflow.com/questions/10267731/android-how-to-make-an-adapter-with-stable-ids – Benjamin W. Dec 20 '13 at 11:24

0 Answers0