0

i need to display data in separated list view with check boxes. In that i checked some views, but when i am scrolling, the checked state changed to unchecked. i am using the following class to display my listview.

public class SearchAdapter extends ArrayAdapter<Map<String,?>>{
        private LayoutInflater inflater;
        private int resId=0; 
        private List<Map<String,?>> listitem;
        public SearchAdapter(Context context, int resId, List<Map<String,?>> dataList){
            super(context, 0, dataList);
            this.resId = resId;
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.listitem = dataList;   
        }
        @Override
         public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
         TextView code,date; CheckBox checkbox;
         view = inflater.inflate(resId, parent, false);
         code = (TextView)view.findViewById(R.id.code);
         date= (TextView)view.findViewById(R.id.date);
         checkbox=(CheckBox)view.findViewById(R.id.check);
         Map<String,?> item = listitem.get(position);
         final String[] values=(String[]) item.get("values");
         checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              Integer pos = (Integer)buttonView.getTag();     
             if(isChecked)
             {
              Log.e("checked","checked"+position);
               } else{
                   Log.e("checked","unchecked");
                    }
                    }
                    });
            code.setText(values[32]);
            date.setText(values[31]);

                return view;
        }       
    } 

my screen view enter image description here

So, please guide me , how to overcome this issue

koti
  • 1,071
  • 3
  • 15
  • 35
  • are you using cutom listview?if yes then please post that code also – Cris Jul 16 '13 at 10:24
  • here's an example http://stackoverflow.com/questions/16685366/customised-listview-using-arrayadapter-class-in-android/16686623#16686623. Source from where i picked https://groups.google.com/forum/#!topic/android-developers/No0LrgJ6q2M – Raghunandan Jul 16 '13 at 10:25
  • @cris SearchAdapter ex = new SearchAdapter(getActivity(), R.layout.searchlist_item, lists[i]); sAdapter.addSection(cust.getName(), ex); – koti Jul 16 '13 at 10:27
  • @koti i am talking about custom listview,is it android listview or you have overriden it? – Cris Jul 16 '13 at 10:29
  • @Cris he is using custom listview extending ArrayAdapter and overriding `getView`. – Raghunandan Jul 16 '13 at 10:31

1 Answers1

3

I was same issue but I solve by this code

public class CustomizeListView extends ListActivity {

    LayoutInflater inflater;
    static int index = 0;

    // AutoCompleteTextView autoCompleteTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, arrayList_Category);
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        final CustomAdapter adapter = new CustomAdapter(this,
                R.layout.activity_orderitem, arrayListItems);
        setListAdapter(adapter);
    }

    private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
            implements OnItemSelectedListener {
        boolean[] checkBoxState;
        ViewHolder viewHolder;

        public CustomAdapter(Context context, int textViewResourceId,
                ArrayList<HashMap<String, Object>> arrayListItems) {
            // let android do the initializing :)
            super(context, textViewResourceId, arrayListItems);
            checkBoxState = new boolean[arrayListItems.size()];
        }

        // class for caching the views in a row
        private class ViewHolder {
            CheckBox checkBox;
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            Log.d("getView", "getView");
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_orderitem,
                        null);
                viewHolder = new ViewHolder();
                // cache the views
                convertView.setTag(viewHolder);
            } else
                viewHolder = (ViewHolder) convertView.getTag();

            viewHolder.checkBox.setChecked(checkBoxState[position]);

            viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (((CheckBox) v).isChecked()) {
                        checkBoxState[position] = true;
                    } else {
                        checkBoxState[position] = false;
                    }
                }
            });

            return convertView;
        }
    }
}
Samadhan Medge
  • 2,049
  • 2
  • 16
  • 24