0

I saw many variation of this question in SO but none of the answers fix my problem so I guess my implementation is a bit different than the rest. (I put all the recommendation like

android:descendantFocusability="blocksDescendants" 

etc.) I am trying to listen to a click event of a listView item. I managed to listen to longClick event but I couldn't replace it by regular click.
Here is what I did, (I tried to put down here the relevant parts to the problem only):

The ListView Definition (saved as my_list_row.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:orientation="vertical"
    tools:context="gm.activities.ViewAllActivity">
  <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello"/>
</LinearLayout>

The listItem definition:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants"
    android:longClickable="true"
    android:clickable="true">

    <TextView
        android:id="@+id/productNameText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="fill_parent"        
        android:background="@color/unselected_row"
        android:focusable="false"
        android:longClickable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>
    <TextView
        android:id="@+id/expiryDate"
        android:layout_width="100dp"
        android:layout_height="fill_parent"        
        android:background="@color/unselected_row"
        android:focusable="false"
        android:longClickable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>
</LinearLayout>

In my activity to set the ListView:

public class ViewAllActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener {

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_all_activity);
        ...
        ListView listView=(ListView)findViewById(R.id.list);
        listView.setAdapter(new DataListAdapter(dataList));
        listView.setClickable(true);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override                
        public void onItemClick(AdapterView<?> arg0, View row,
                                           int pos, long id) {

                Log.d("ViewAllActivity", "Item clicked pos: " + pos);
        });
  }

  class DataListAdapter extends BaseAdapter {

        private List<GuaranteeObj> GaList;

        DataListAdapter(List<GuaranteeObj> gaList) {
            GaList = gaList;
        }

        @Override
        public int getCount() {
            return GaList.size();
        }

        @Override
        public Object getItem(int position) {
            return GaList.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = getLayoutInflater();
            View row;
            row = inflater.inflate(R.layout.my_list_row, parent, false);        
            return row;


        }
    } 

The problem is I don't see that the ListItem get clicked - I don't see the expected log message. Anyone has idea why? )-:

GyRo
  • 2,586
  • 5
  • 30
  • 38
  • add `@Override` before `public void onItemClick` – M D Mar 02 '15 at 11:18
  • OK did that, but of course it's not solving the problem – GyRo Mar 02 '15 at 11:23
  • Try to remove listView.setClickable(true); – IshRoid Mar 02 '15 at 11:23
  • It appears that you already override the OnItemClickListener's onItemClick method. So you are saying nothing happens when you click an item? Here is the interface link http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html – Eenvincible Mar 02 '15 at 11:23
  • Eenvincible - I am using the interface's method and yes nothing happens – GyRo Mar 02 '15 at 11:31
  • You need to solve it by the process of elimination as it seems more a "typo-somewhere-issue", so I would suggest first not using your layout and adapter, just use ArrayAdapter and the simple layout. If item click won't work, then at least you will know to look at your activity and listview. – Alex.F Mar 02 '15 at 11:52
  • Did you try my answer? and also remove `android:text="hello"` from `` – Apurva Mar 02 '15 at 11:55

3 Answers3

1

Change Your listview item layout/view like below and you are done.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">

<TextView
    android:id="@+id/productNameText"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="fill_parent"        
    android:background="@color/unselected_row"
    android:focusable="false"       
    android:focusableInTouchMode="false"/>
<TextView
    android:id="@+id/expiryDate"
    android:layout_width="100dp"
    android:layout_height="fill_parent"        
    android:background="@color/unselected_row"
    android:focusable="false"       
    android:focusableInTouchMode="false"/>
</LinearLayout>

For more Info Check My Answer

Also you can set both onitemclicklistener and onlongitemclicklistner on listview like below:

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            // TODO Auto-generated method stub

            Log.v("long clicked","pos: " + pos);

            return true;
        }
    }); 

you just have to return true instead of false.

Community
  • 1
  • 1
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
  • 2
    Dev - it worked. so to summarize, if I understand well, the problem was that I shouldn't set the descendants of ListView with clickable=true. to make it work... Thanks! – GyRo Mar 02 '15 at 11:58
0

As i undersood your code try to remove or add below code

Remove from the root layout of listItem definition:

android:clickable="true"

Then remove listView.setClickable(true); from the code

Now if you want to use longClick or ItemClick in same listView then

Implement longClick listener for listView and

implement onClick listener in Adapter

as

row = inflater.inflate(R.layout.my_list_row, parent, false); 

    row.setOnclickListener(.....)
IshRoid
  • 3,696
  • 2
  • 26
  • 39
  • Ishrat - as you can see in this related question http://stackoverflow.com/questions/2468100/android-listview-click-howto it is possible to use 'listView.setOnItemClickListener' and not row.setOnclickListener, this is more straightforward and provides the position inside the list in the callback. I'd rather use 'setOnItemClickListener' then – GyRo Mar 02 '15 at 11:40
  • Then just remove just remove android:clickable="false" from everywhere in your layout and use setOnItemClickListener or setOnItemLongClickListener as you want. – IshRoid Mar 02 '15 at 11:55
0
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

            clickId = Integer.parseInt(userId.get(position));

            Intent c = new Intent(Messages.this,New_message.class);
            Bundle b = new Bundle();
            b.putInt("Secondkey", clickId);
            c.putExtras(b);

            startActivity(c);

//userId is String ArrayList,before you have to add your IDs in it.

Caner Yılmaz
  • 201
  • 3
  • 6