onItemClickListener with custom adapter and listview
Asked Answered
A

4

7

I am trying to set an onItemClickListener with a custom adapter and listview setup. Can't seem to get the listener working. I don't think I am setting it up properly. Any help is appreciated. Thanks very much.

Adapter:

public class ModuleAdapter extends ArrayAdapter<Module> {

Context context;
int layoutResourceId;
Module data[];

public ModuleAdapter(Context context, int layoutResourceId,
        Module data[]) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

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

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

        holder = new ModuleHolder();
        holder.modJapTitle = (TextView)row.findViewById(R.id.moduleJapTitle);
        holder.modEngTitle = (TextView)row.findViewById(R.id.moduleEngTitle);
        holder.modComp = (TextView)row.findViewById(R.id.moduleCompletion);
        holder.modRating = (RatingBar)row.findViewById(R.id.moduleScore);

        row.setTag(holder);
    }
    else
    {
        holder = (ModuleHolder)row.getTag();
    }

    Module module = data[position];
    holder.modJapTitle.setText(module.moduleJapaneseTitle);
    holder.modEngTitle.setText(module.moduleEnglishTitle);
    holder.modComp.setText(module.moduleCompletionRate);
    holder.modRating.setRating(module.moduleRating);

    return row;
}

static class ModuleHolder
{
    TextView modEngTitle;
    TextView modJapTitle;
    TextView modComp;
    RatingBar modRating;
}

}

Implementation:

ModuleAdapter moduleData = new ModuleAdapter(this, R.layout.module_box_item, module_data);
    ListView listView1 = (ListView)findViewById(R.id.moduleListContainer);
    listView1.setAdapter(moduleData);
    listView1.setCacheColorHint(Color.TRANSPARENT);
    listView1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.v("Module Item Trigger", "Module item was triggered");
            Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
        }
    });

Also here is the XML Layout for one of the single items in the list:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/moduleBox"
android:id="@+id/moduleBoxSingle" >

<TextView
    android:id="@+id/moduleJapTitle"
    style="@style/moduleTitleJap" />
<TextView
    android:id="@+id/moduleCompletion"
    android:layout_above="@+id/moduleSepartor"
    style="@style/moduleCompletion" />
<View
    android:id="@+id/moduleSepartor"
    android:layout_below="@+id/moduleJapTitle"
    style="@style/moduleSeperator" />
<TextView
    android:id="@+id/moduleEngTitle"
    android:layout_below="@+id/moduleSepartor"
    style="@style/moduleTitleEng" />
<RatingBar
    android:id="@+id/moduleScore"
    android:layout_below="@+id/moduleSepartor"
    style="@style/moduleRating"
    android:layout_alignParentRight="true" />

</RelativeLayout>
Additory answered 29/6, 2012 at 16:42 Comment(3)
Everything appears to look good. Perhaps it's a UI problem? Why don't you write to logcat instead of popping up a Toast. Just to be 100% sure that your listener isn't being called.Mccomas
can you post your layout xml here.Aragon
Hello, thank you for your reply. I've added in the line to write to Logcat. And I've also added in the XML layout used for each single list item.Additory
C
18

I think it's the similar problem like this.

Add below code to your TextView in the XML

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

and try again.

Chamorro answered 30/6, 2012 at 6:53 Comment(1)
This did the trick! Thank you so much. Lots of hair saved. :)Additory
A
4

First of All for this you have to impliments the class with the following handler

implements  OnItemClickListener 

then add a function as given below onItemClick()

public void onItemClick(AdapterView parent, View v, int position, long id) {
  Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();   
}

And when we set the data adapter to the listview then add the following line of code

itemlist.setOnItemClickListener(this);

Hope this will work for you...

Alkylation answered 29/6, 2012 at 16:58 Comment(1)
Hello, thank you for your reply. I gave this a try, but the result was the same as the original code I have above. Maybe something else is wrong with my code that isn't firing that listener?Additory
V
2

I have same issue and I fix it by do like:(refer from https://github.com/sephiroth74/HorizontalVariableListView/issues/33 ) add this line in root view in layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/moduleBox"
android:id="@+id/moduleBoxSingle"
android:descendantFocusability="blocksDescendants" >
Voltammeter answered 20/9, 2016 at 9:45 Comment(0)
B
0

Try this...

ListView lv = (ListView)findViewById(R.id.Your_ListView_Id);  // If it extends Activity.

OR

ListView lv = getListView;  // If it extends ListActivity.
lv.setOnItemClickListener(this); // Make the Activity implement onItemClickListener
lv.setAdapter(your_adapter);

Edited part: Change made in Toast's Context, and Added SysOut

// The method that will handle the Events.

public void onItemClick(AdapterView parent, View v, int position, long id{
     Toast.makeText(Your_Activity_Name.this, "hello", Toast.LENGTH_SHORT).show();
     System.out.println("This is a Test");
}

CHECK YOU LOG.. DOES THE SYSOUT STATEMENT GETS PRINTED !!

Barricade answered 30/6, 2012 at 3:19 Comment(2)
Hello, thanks for your message. Unfortunately, this doesn't work either. The toast message is still not firing.Additory
Just a min.......try this.. Toast.make Toast.makeText(Your_Activity_Name.this, "hello", Toast.LENGTH_SHORT).show();Barricade

© 2022 - 2024 — McMap. All rights reserved.