Question

I have seen similar questions and tried the solutions such as setting focusable to false but it is still not working. Here is my layout that contains the listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<ListView 
    android:id="@+id/listView_open_groups"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button_add_group"
    android:layout_alignParentTop="true"
    ></ListView>

<Button 
    android:id="@+id/button_add_group"
    android:text="Add Group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:focusable="false"
    />

<Button 
    android:id="@+id/button_add_entry"
    android:text="Add Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button_add_group"
    android:focusable="false"
    />


</RelativeLayout>

And my custom_list_row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true" >


<ImageView 
    android:id="@+id/imgView_group_entry_icon"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    />
<TextView 
    android:id="@+id/textView_group_entry_name"
    android:layout_toRightOf="@id/imgView_group_entry_icon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"/>
<TextView 
    android:id="@+id/textView_group_entry_type"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"/>


</RelativeLayout>

And my custom adapter:

public class OpenDbListAdapter extends ArrayAdapter<KeepassDBGroupv1>{

Context context;

public OpenDbListAdapter(Context context, int resource,
        List<KeepassDBGroupv1> objects) {
    super(context, resource, objects);
    this.context=context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    KeepassDBGroupv1 rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_list_row, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
        holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
        convertView.setTag(holder);
    } else 
        holder = (ViewHolder) convertView.getTag();

    holder.name.setText(rowItem.getGroupName());
    holder.type.setText("Group");
    Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
    holder.imageView.setImageDrawable(d);
    //App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);

    return convertView;
}

private class ViewHolder {
    ImageView imageView;
    TextView name;
    TextView type;
}

}

And finally, here is how i set the listener:

public class OpenDbActivity extends Activity{    
    static ListView lv;  

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opendb);
    db = MyApplication.keepassdb;
    InitializeComponents();

}

    private void InitializeComponents() {

    lv = (ListView) findViewById(R.id.listView_open_groups);
    OpenDbListAdapter adapter = new OpenDbListAdapter(this, R.layout.custom_list_row, childGroups);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            Log.d("Something");

        }
    });



}

Can anyone see what the problem is?

Thanks

Was it helpful?

Solution

I too faced this issue, I overcome from this issue by setting click listener to convertView of custom adapter. I don't know is this good approach but it solved my issue.

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    KeepassDBGroupv1 rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_list_row, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
        holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
        convertView.setTag(holder);
    } else 
        holder = (ViewHolder) convertView.getTag();

    holder.name.setText(rowItem.getGroupName());
    holder.type.setText("Group");
    Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
    holder.imageView.setImageDrawable(d);
    //App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);
    convertView.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
              Log.v("OpenDbListAdapter ","List View Clicked");
          }
    });
    return convertView;
}

OTHER TIPS

Actually nothing is wrong.

But the problem is from the custom_list_row.xml, when you click on the list view item,

You actually click on other tags in your custom_list_row.xml

and these tags are not responsible for firing the ItemClickListener.

Change value of property android:layout_width of tags in the custom_list_row.xml to "wrap_content" or remove the tags and try again.

after setting

android:focusable="false"

for your ListView content, in your ListView Tag Add

android:clickable="true"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top