0

I have this class and XML

public class MyClass extends SherlockListActivity implements OnTouchListener

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:smoothScrollbar="true"
        android:cacheColorHint="#fff"
        android:fastScrollEnabled="false" 
        android:clickable="true"
        android:layout_weight="1"
        android:dividerHeight="1sp" /> 

I'm not able to detect a click. The only way to register a click is to drag. I don't understand what the problem is.

getListView().setOnTouchListener(this);

Here is my onTouch:

public boolean onTouch(View v, MotionEvent event)
    {
        Log.i("Click", "MAIN");
        switch (event.getAction() & MotionEvent.ACTION_MASK) 
        {
        case MotionEvent.ACTION_DOWN:
...

The only way to get any log is (or action) is by dragging. I tried click and long click and none works. Any idea how to fix this and keep onTouch?

Edit: The list view uses a different ArrayAdapter implementation (my own):

public class ContentDisplayListAdapter extends ArrayAdapter<DataContentsInfo> {
    private List<DataContentsInfo> items;
    private DataContentsInfo o; 

    public ContentDisplayListAdapter(Context context, int textViewResourceId, List<DataContentsInfo> items) {
        super(context, textViewResourceId, items);
        this.items = items;     
    }

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

        if(v == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.content_display_row, null);
        }

        o = items.get(position);

        if(o != null) {
            TextView content = (TextView) v.findViewById(R.id.content);
            TextView DataNo = (TextView) v.findViewById(R.id.title_no);

            ContentDisplayAdapter.updateTitles(getContext(), o, content, DataNo, items.size());
        }

        return v;
    }
}
xBlue
  • 523
  • 2
  • 7
  • 15
  • 2
    Why are you trying to make the `ListView` itself clickable? That will interfere with the operation of the `ListView`, which needs to process clicks on its child views (the list rows). What are you trying to accomplish? – Ted Hopp Aug 21 '14 at 03:29
  • Override `onItemClickListener` – Aniruddha Aug 21 '14 at 03:45
  • The ListView is used only to display some text items, none if which is clickable or has any other functions other. What I'm trying to do is to have double click recognized, but it's irrelevant where you double click. – xBlue Aug 21 '14 at 03:46
  • @xBlue you mean double click on item of the listView? – Rod_Algonquin Aug 21 '14 at 03:54
  • So if you're moving away from the ListView's primary feature (detecting list item touches as @Tedd Hopp suggests), could you nest it inside a LinearLayout or FrameLayout and put the ontouch listener on that? It's hacky, but it looks like it's what you're trying to do... – CodeMonkey Aug 21 '14 at 03:54
  • If double click is what you are trying to accomplish, you should state that in the question. – Matthew Pape Aug 21 '14 at 04:03
  • @Rod_Algonquin no, just click on the ListView. I still don't understand why onTouch doesn't register. – xBlue Aug 21 '14 at 04:31

4 Answers4

3

This will work for you :)

listView.setOnItemClickListener(new OnItemClickListener() {

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

                       // ListView Clicked item index
                       int itemPosition     = position;

                       // ListView Clicked item value
                       String  itemValue    = (String) listView.getItemAtPosition(position);

                        // Show Alert 
                        Toast.makeText(getApplicationContext(),
                          "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                          .show();

                      }

                 });

enter image description here

Vishal
  • 537
  • 3
  • 13
  • I tried this and no error, but nothing happens. I don't think the code is ever reached. – xBlue Aug 21 '14 at 04:30
  • I can provide the full working code if u want..I just wanted you to explore a bit. Reply if you want the complete working code. – Vishal Aug 21 '14 at 04:33
  • So your initial example wasn't working because of the ArrayAdapter. With yours it works. I forgot to mention that I'm using my own (I didn't know it was relevant). I added the code to the main post. That's the ArrayAdapter I set on the listView. Do you have any idea what's wrong? Thanks a lot. – xBlue Aug 21 '14 at 22:44
  • Hey, Let me know that u want simple listview as shown in my answer or a custom listview with images and other things.. Simple listview can be created by my way..but ur code is trying to make the adapter, it is not trying to populate the listview.. we extend a class with ArrayAdapter, only when we want to create a custom/complex listview.. – Vishal Aug 22 '14 at 03:27
  • I've seen other examples. Unfortunately I need to use what I have right now because of the design. Do you have any other ideas? – xBlue Aug 22 '14 at 21:05
1

This is the workin code..use it..cheers.. :)

enter image description here

    public class ListViewAndroidExample extends Activity { 
ListView listView ; 
@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_view_android_example);

            // Get ListView object from xml
            listView = (ListView) findViewById(R.id.list);

            // Defined Array values to show in ListView
            String[] values = new String[] { "Android List View", 
                                             "Adapter implementation",
                                             "Simple List View In Android",
                                             "Create List View Android", 
                                             "Android Example", 
                                             "List View Source Code", 
                                             "List View Array Adapter", 
                                             "Android Example List View" 
                                            };

            // Define a new Adapter
            // First parameter - Context
            // Second parameter - Layout for the row
            // Third parameter - ID of the TextView to which the data is written
            // Forth - the Array of data

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, android.R.id.text1, values);


            // Assign adapter to ListView
            listView.setAdapter(adapter); 

            // ListView Item Click Listener
            listView.setOnItemClickListener(new OnItemClickListener() {

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

                   // ListView Clicked item index
                   int itemPosition     = position;

                   // ListView Clicked item value
                   String  itemValue    = (String) listView.getItemAtPosition(position);

                    // Show Alert 
                    Toast.makeText(getApplicationContext(),
                      "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                      .show();

                  }

             }); 
        }

    }
Vishal
  • 537
  • 3
  • 13
0

Use setOnItemClickListener() that way list is also clickable whichever the clicked item do your work as it can be recocnized that list is clicked

NavinRaj Pandey
  • 1,674
  • 2
  • 26
  • 40
0

If you are not concerned with single clicks on any element in the listview, or the listview itself, then you can use the click listener for this. Something like the following...

First we need a subclass of OnItemClickListener that only triggers on double clicks:

public abstract class DoubleClickListener implements OnItemClickListener {
    // time between clicks before it is no longer considered a double click
    private static final long DOUBLE_CLICK_TIME_DELTA = 200;
    private long mLastClickTime = 0;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // check if this click occurred shortly after a previous click
        long clickTime = System.currentTimeMillis();
        if (clickTime - mLastClickTime < DOUBLE_CLICK_TIME_DELTA) {
            onDoubleClick(view);
        }

        // save the time of this click for comparing against future clicks
        mLastClickTime = clickTime;
    }


    // override this function with desired behavior
    public abstract void onDoubleClick(View view);
}

Now set an instance of our new class as the click listener for our listview:

ListView listView = getListView();
listView.setOnItemClickListener(new DoubleClickListener() {
    @Override
    public void onDoubleClick(View v) {
        // TODO - handle our double click here!
    }
});
Matthew Pape
  • 1,681
  • 1
  • 22
  • 25
  • I tried this solution and I got an exception: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead. To get the listView, I did: ListView listView = getListView(); – xBlue Aug 21 '14 at 04:22
  • Because `ListView` extends `AdapterView`, you will want to use an `OnItemClickListener` instead of an `OnClickListener`. I updated the code to show how you could do this. – Matthew Pape Aug 21 '14 at 15:19
  • Thanks for your help. It's still not working, but I'm closer. It's seems the issue is with my ArrayAdapter. It works with a regular one, but not with mine (see in main post). – xBlue Aug 21 '14 at 22:44