Travelling Salesman
Travelling Salesman

Reputation: 2271

Android ListView with CheckBox can distinguish the click

I am trying to create a ListView with CheckBox's...Th ListView should allow the user to both select an item or open that item to select other choice inside it. In other words, the ListView should be able to distinguish between the click on the checkbox and the click on the item itself.

I tried to implement it using android.R.layout.simple_list_item_multiple_choice but this one allows me to only check the checkbox even if I click outside the checkbox (on the item).

anyone can help?
Here's my code,

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends Activity implements OnItemClickListener {  

ListView directoryList;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ArrayList<String> contacts = new ArrayList<String>();
    contacts.add("zaid");
    contacts.add("hazem");
    contacts.add("Oubai");


    directoryList= (ListView) findViewById(R.id.directoryList);


    final ArrayAdapter<String> arrayAdapter;
    arrayAdapter = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_multiple_choice,
                                  contacts);


    directoryList.setAdapter(arrayAdapter);
    directoryList.setOnItemClickListener(this);
    directoryList.setClickable(true);



}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {




}

}

Upvotes: 0

Views: 1661

Answers (1)

Shubhayu
Shubhayu

Reputation: 13552

You need a customized array adapter. In your getView(), get a hold of your checkbox and set the OnCheckedChangeListener()

Here is a tutorial on how you can make your own custom adapter. The example has a clickable textbox but you can change it to work for a checkbox.

http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items

Upvotes: 2

Related Questions