1

Have seen this issue many times, i am using a customer adapter to populate my Listview with my customer layout xml. I understand the click is probably never going to List view and my layout is blocking it, but after trying the possible solutions nothing seems to be trying :

Tried: android:descendantFocusability="blocksDescendants" for layout of my custom xml

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" 

for each individual elements in the layout

Any other way to solve this

EDIT

Adding the code for where listview is attached to adapter:

if(!allcontactsstring.isEmpty()){
  adapter = new MyContactListAdapter(getContext(),R.layout.contact_list_view,imageArry);
       listView.setAdapter(adapter);
   }

AdapterView.OnItemClickListener mylistViewClicked = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG);
        }
    };
    listView.setOnItemClickListener(mylistViewClicked);

My Custom adapter :

public class MyContactListAdapter extends ArrayAdapter<MySQLiteContact> {

Context context;
int layoutResourceId;
ArrayList<MySQLiteContact> data=new ArrayList<MySQLiteContact>();


public MyContactListAdapter(Context context, int resource, ArrayList<MySQLiteContact> objects) {
    super(context, resource, objects);
    //this.layoutResourceId = layoutResourceId;
    this.layoutResourceId = resource;
    this.context = context;
    this.data = objects;



}

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

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        //ImageView tmp = (ImageView)row.findViewById(R.id.icon);
        //tmp.clearFocus();
        //tmp.isClickable();

        //TextView tmp1=(TextView)row.findViewById(R.id.Itemname);
        //tmp1.clearFocus();


        holder = new ImageHolder();
        holder.txtTitle = (TextView)row.findViewById(R.id.Itemname);
        holder.imgIcon = (ImageView)row.findViewById(R.id.icon);
        row.setTag(holder);

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

    MySQLiteContact picture = data.get(position);
    holder.txtTitle.setText(picture.getsFirstName()+" "+picture.getsLastName());
    byte[] outImage=picture.getBimage();
    ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
    Bitmap theImage = BitmapFactory.decodeStream(imageStream);
    theImage=Bitmap.createScaledBitmap(theImage , 100, 100, true);
    theImage= ContactDetailsMain.getRoundedRectBitmap(theImage,100);
    holder.imgIcon.setImageBitmap(theImage);


    return row;



}

static class ImageHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}

}

My xml with text view and imageview:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"   
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<ImageView                                          
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:src="@drawable/ic_contact_picture_holo_light"
/>
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="@color/black"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:paddingTop="5dp"
/>
</LinearLayout>

EDIT A dirty workaround

As a crud and dirty workaround i have added a on click listener inside the adapter to each and every layout that gets inflated and this seems to work currently to get the positions.

if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println(position);
            }
        });

Not clean to have so many listeners , would be nice to have only one but yes for the moment it work..

anil keshav
  • 479
  • 1
  • 5
  • 18
  • *I understand the click is probably never going to List view and my layout is blocking it* what you mean exactly with this? can you please extend/clarify? thanks – Jordi Castilla Oct 07 '15 at 13:44
  • @jordi Castilla,I have a linear layout with a imageview and text view . It is this layout that is getting populated in every item of my listview. So if i click on the listview is my click being picked up by the OnItemClickListener of my listview? . – anil keshav Oct 07 '15 at 13:55
  • if you added a listener to your `ListView` then yes. – Jordi Castilla Oct 07 '15 at 13:58
  • @Jordi Castilla, i am not sure as the Image view or the text view clicks tend to cause a problem, facing something similar to [link](http://stackoverflow.com/questions/16437673/onitemclicklistener-and-onclicklistener-not-working-for-listview) , but have the solutions and it did not work – anil keshav Oct 07 '15 at 14:35
  • If you dont post an MVCE its hard to figure it out... – Jordi Castilla Oct 07 '15 at 14:43
  • @JordiCastilla , hope that was what you were asking for , have added the relevant codes. – anil keshav Oct 07 '15 at 15:02

1 Answers1

0

Well your click works, what it doesn't work on your code is the fact that you don't see the Toast right? You just forgot to call show() on Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG) which should be Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG).show()

Enjoy :p


Edit

List click 1 List click 2

Kalem
  • 1,132
  • 12
  • 33
  • sorry that was a typo, with `Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG).show();` as well it does not show anything ... – anil keshav Oct 07 '15 at 15:15
  • Mmmmmm... i was almost sure it was that because the focus problem on list views applies when you have focusable views (Button, CheckBox, ImageButton, etc...) on your custom layout which you don't (see http://stackoverflow.com/a/1562832/665823) – Kalem Oct 07 '15 at 15:25
  • yup exactly even i thought i should worry with focusable view but even a ImageView is causing it i guess, I am not sure 5 hours on this problem now, – anil keshav Oct 07 '15 at 17:20
  • Well i just "copy-paste" your code but instead of using a ArrayList i use ArrayAdapter for simplicity sake. I also deleted all the focus rubish on the layout xml and it does work (see edit). So there's something else that's causing your error. – Kalem Oct 08 '15 at 09:04