CristianoWilson
CristianoWilson

Reputation: 143

Tick or untick checkboxes when clicking on the list item

So at the moment I have got a listview, with checkboxes at the end of each row. Right now, when you tick the box the box gets ticked and unticked. But what I want is that when you click on the listview item/row that the checkbox gets ticked or unticked.

here's my code:

public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my3);
    Button m = (Button) findViewById(R.id.button3);
    tv = (TextView) findViewById(R.id.textViewcat);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    tv.setTypeface(typeface);


    String listArray[] = new String[] { "All", "Friends & Family", "Sports", "Outside",
            "At School", "Fitness", "Photography", "Food", "Beach", "Money" };
    ListView listView = (ListView) findViewById(R.id.listView);
    List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i <= listArray.length - 1; i++) {
       HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("title", listArray[i]);
        aList.add(hm);
    }
    String[] sfrm = { "title"};
    int[] sto = { R.id.title};
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
            R.layout.row_layout, sfrm, sto);
   listView.setAdapter(adapter);
   listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                                int position, long id) {
            switch (position) {

            }

        }
    });
}

Upvotes: 0

Views: 1124

Answers (2)

NavinRaj Pandey
NavinRaj Pandey

Reputation: 1704

The suggestion of Zoltish is fine for you but if you create a custom list adapter it will be better for further modificatons of list and bug free coding for each new requirements

if the refered tutorial is too complicated for you try something like this

private class checkBoxListAdapter extends BaseAdapter {




    private class ViewHolder {

        CheckBox chk;

    }


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

        ViewHolder holder = null;

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) atvt
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.msg_receipents, null);

            holder = new ViewHolder();

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


            convertView.setTag(holder);

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

                                            holder.chk.setChecked(!holder.chk.isChecked());
                }
            });

        } else {
            holder = (ViewHolder) convertView.getTag();


        }




        return convertView;
    }

}

Upvotes: 0

zoltish
zoltish

Reputation: 2302

    @Override
    public void onItemClick(AdapterView<?> arg0, View view,position, long id) {
    CheckBox cb = (CheckBox) view.findViewById(R.id.your_checkbox);    
    cb.setChecked(!cb.isChecked());
    }

This simply makes the item checked if it wasnt, and unchecks it if it was.

Upvotes: 1

Related Questions