1

I have a list view and each row contains two textviews and a checkbox. I need user sets just one of checkboxes and if user sets other checkbox, previous checkbox should be cleared and second checkbox can be set.

I can generate the list but i don't know why onListItemClick() method does not work? therefore, I don't know which checkbox is set.

and my next question is, how can i redraw list after clicking checkbox by user?

my code is:

    package com.Infindo.DRM;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class Infindo_PackageActivity extends ListActivity {

    private final String[] titels = {"Try before buy", "Pay per play", "Pay per day", "Pay per week", "Pay per month", "Daily subscription", "Weekly subscription", "Monthly subscription"};
    private final String[] descriptions = {"Free of charge", "Price: $1.00", "Price: $5.00", "Price: $10.00", "Price: $30.00", "Price: $5.00", "Price: $10.00", "Price: $30.00"};
    private String[] flag = {"false", "false", "false", "false", "false", "false", "false", "false"};
    private ListAdapter listAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infindo_packagepage);

        listAdapter = new ListAdapter(this);
        setListAdapter(listAdapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.i("List Clicked:", "******");
    }

    public void onCheckboxClicked(View v){
        int i = getSelectedItemPosition();
        Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
    }


    //*********************
    //  RowModel Class
    //*********************
    private class RowModel{
        TextView title;
        TextView description;
        CheckBox checkbox;
    }

    //*********************
    //  ListAdapter Class
    //*********************
    private class ListAdapter extends ArrayAdapter<String>{

        public ListAdapter(Context c) {
            super(c, R.layout.infindo_listformat, titels);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            RowModel holder;
            View row = convertView;

            if(row == null){
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.infindo_listformat, parent, false);

                holder = new RowModel();
                holder.title = (TextView) row.findViewById(R.id.title);
                holder.description = (TextView) row.findViewById(R.id.description); 
                holder.checkbox = (CheckBox) row.findViewById(R.id.checkbox);

                row.setTag(holder);

            } else {
                holder = (RowModel) row.getTag(); 
            }

            holder.title.setText(titels[position]);
            holder.description.setText(descriptions[position]);
            String s = flag[position];
            if(s.equalsIgnoreCase("false")) 
                holder.checkbox.setChecked(false);
            else
                holder.checkbox.setChecked(true);

            return(row);
        }
    }
}

Update: I have added

@Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            Log.i("List Redrawed:", "**notifyDataSetChanged()**");
        }

into my List Adapter class and called it in

public void onCheckboxClicked(View v){
        int i = getSelectedItemPosition();
        Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
        listAdapter.notifyDataSetChanged();
    }

I think the problem of redraw is solved. but I still I don't know what checkbox is clicked by the user in order to change flag array.

Hesam
  • 52,260
  • 74
  • 224
  • 365

2 Answers2

2

You can use setOnCheckedChangeListener to know which checkbox was selected. You can look at complete example of ListView with CheckBox here.

UPDATE

To make your onListItemClick() work you need to write android:focusable="false" for other items of the ListView, its because of the focus of other views ListView's onListItemClick() is not peforming as it should be performing.

Checkout this answer, why Android custom ListView unable to click on items

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • thanks dear Lalit, I saw that link before and it is so helpful. But my code is correct as well as that one you suggested. Just I don't know why "onListItemClick()" do not run in my code while will run in their code. I checked and compared my code with their code several times and everything seems OK, but this method does not run in my code. this is my question, why? – Hesam Nov 30 '11 at 03:58
  • yes its because you have to add android:focusable="false" to other views in the ListView items, then listview's item click will work for you. – Lalit Poptani Nov 30 '11 at 04:03
  • thanks dear Lalit. You are right. I added that line of code to all items of row and problem solved. Now, "onListItemClick()" works fine although i don't know what is the reason(s)? – Hesam Nov 30 '11 at 04:15
  • Well you must know the reason, read the link that I had given you it will help you in future too. Thanks. – Lalit Poptani Nov 30 '11 at 04:16
0

You probably need to handle the click from the checkbox itself and not just the listview.

dhaag23
  • 6,106
  • 37
  • 35
  • Yes but in this situation, user should click checkbox. but if user clicks row instead of checkbox nothing will happen. It maybe not be important but Actually I need to know what checkbox is clicked. i think i need to know what is it's position in the list. I tried to find it in "onCheckboxClicked()" but for all checkboxes, the result is -1. I'm so confused and i don't know what and where is my mistakes? – Hesam Nov 30 '11 at 03:21