Monday, 9 September 2013

Setting different behaviour for the different items in the same row in the list view

By default all the items in a list view behaves the same way if the user clicks on it, but it is possible set different behaviour for different items in the same row.

The key point here is not set the OnItemClickListener. Instead, in the adapter, set the click listener by item in the method getView:

public class MyListViewAdapter extends BaseAdapter {
    ........
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);
            TextView text = (TextView) view.findViewById(R.id.label);
            CheckBox checkbox = (CheckBox) view.findViewById(R.id.check);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // Do something
          }
            });
    .......
}

Source Code:
StackOverflow - Android listview with checck boxes?

No comments:

Post a Comment