-1

Below I have written my code actually when I was scrolling my listview the checkbox takes its original position, any help by code will be appreciated.

 cp.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
        // TODO Auto-generated method stub
        cp.setChecked(true);
        cx.setChecked(false);

        ck.setChecked(false);
        int a=1;

        String c=getListView().getItemAtPosition(position).toString();
        updatetable(c, "FULL");
        //Toast.makeText(getApplicationContext(), c+"full", Toast.LENGTH_LONG).show();
        // Toast.makeText(getApplicationContext(), "year", Toast.LENGTH_LONG).show();
    }
});

 cx.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        // TODO Auto-generated method stub
        cx.setChecked(true);
        cp.setChecked(false);
        ck.setChecked(false);
        int a=1;
        String c=getListView().getItemAtPosition(position).toString();
        updatetable(c, "CRITICAL");

        //Toast.makeText(getApplicationContext(), c+"ok", Toast.LENGTH_LONG).show();
    }
});

 ck.setOnClickListener(new OnClickListener() {

        public void onClick(View v)  {
            ck.setOnCheckedChangeListener(null);
            // TODO Auto-generated method stub
            ck.setChecked(true);
            cp.setChecked(false);
            cx.setChecked(false);
            int a=1;
    String c=getListView().getItemAtPosition(position).toString();
    updatetable(c, "FINISH");
            //Toast.makeText(getApplicationContext(), c+"fineshed", Toast.LENGTH_LONG).show();
        }
    });
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Santosh
  • 124
  • 2
  • 13

1 Answers1

0

Your question is not clear, but I think I got the problem.

I think this is not a matter of CheckBox. I think you're doing something wrong with your adapter.

public class MyAdapter extends BaseAdapter {
    private List<Something> mList;
    private Context mContext;

    public MyAdapter(Context context, List<Something> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.getSize();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_layout, null);
        }

        CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
        /* Here you should set your checkbox state */
    }
}

I think your problem is convertView.

When you scroll down your ListView the popped up items are not destroyed, but recycled. The recycled item is given you back as convertView. convertView is not cleaned, hence if the CheckBox in the popped out item was checked, the CheckBox in convertView is checked.

You have to maintain a List<Boolean> states where you save the state of the CheckBox of all your items. Then:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_layout, null);
        }

        CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
        checkBox.setChecked(states.get(position));
    }

I haven't your code, so the example above is as general as possible. If you let us see the code we can help you.

mneri
  • 2,127
  • 2
  • 23
  • 34