-1

I require a listview in a dialog with each row having the name of the app, icon and checkbox. I have used an adapter class that generates each row. I have done as :

lv = (ListView) findViewById(R.id.aplist);
lv.setAdapter(applistadapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                     /* some code*/
            } });

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
 private List<ApplicationInfo> appsList = null;
 private Context context;
 private PackageManager packageManager;
 private TextView appName;
 private ImageView iconview;
 CheckBox check;
 boolean[] itemChecked;

 public ApplicationAdapter(Context context, int textViewResourceId,
   List<ApplicationInfo> appsList) {
  super(context, textViewResourceId, appsList);
  this.context = context;
  this.appsList = appsList;
  packageManager = context.getPackageManager();
  itemChecked = new boolean[appsList.size()];
  Log.wtf("c", "34werty");
 }

 @Override
 public int getCount() {
  return ((null != appsList) ? appsList.size() : 0);
 }
 
 @Override
  public boolean isEnabled(int position) {
  return true;
 }

 @Override
 public ApplicationInfo getItem(int position) {
  Log.wtf("c", "23werty");
  return ((null != appsList) ? appsList.get(position) : null);
 }

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

 @Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  View view = convertView;
  if (null == view) {
   LayoutInflater layoutInflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   view = layoutInflater.inflate(R.layout.row, null);
   Log.wtf("c", "12werty");
  }

  ApplicationInfo data = appsList.get(position);
  if (null != data) {
   Log.wtf("c", "werty");
   appName = (TextView) view.findViewById(R.id.app_name);
   // TextView packageName = (TextView)
   // view.findViewById(R.id.app_paackage);
   iconview = (ImageView) view.findViewById(R.id.app_icon);
   check = (CheckBox) view.findViewById(R.id.checkbox1);

   appName.setText(data.loadLabel(packageManager));
   // packageName.setText(data.packageName);
   iconview.setImageDrawable(data.loadIcon(packageManager));
    
   check.setChecked(false);
   if (itemChecked[position])
    check.setChecked(true);
   else
    check.setChecked(false);

   check.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     if (check.isChecked())
      itemChecked[position] = true;
     else
      itemChecked[position] = false;
      check.setTag(position);
    }
   });

  }

  return view;
 }
};

Now the problem is that the control never goes to setOnItemClickListener().

Please help me to solve it.

Thank you.

R.K
  • 1,721
  • 17
  • 22
  • [Check Here](http://stackoverflow.com/questions/20208285/listview-itemclick-not-work/20208788#20208788) – Pankaj Arora Jan 12 '15 at 09:45
  • Can you post your adapter code? – mjp66 Jan 12 '15 at 09:45
  • @DroneDev I visited that link.. and tried out the solution.. but that did not solve the problem. I can't find the reason why my onItemClick() is not reachable – R.K Jan 13 '15 at 13:11

1 Answers1

0

The problem could be that the elements inside the listView catching the click before the listView. To avoid this, set this attribute inside the listView layout xml:

     android:descendantFocusability="blocksDescendants"

or, programmatically:

     lv.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);

Or you can also deactivate the focus of Your elements inside the listView for example:

     yourCheckBox.setFocusable(false);
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49