I have a custom BaseAdapter
for my ListView
in which i implements AdapterView.OnItemClickListener
.
The problem is the onItemClick(AdapterView<?>, View, int, long)
method is never called. I guess I need to implements that interface
in my main Activity
and not in my ListView
custom adapter.
MyListViewAdapter.java:
public class MyListViewAdapter extends BaseAdapter implements AdapterView.OnItemClickListener
{
private Context context;
private ArrayList<MyListViewRow> rowsList = new ArrayList<MyListViewRow>(); // All one row items
private TextView a;
public MyListViewAdapter(Context context,ArrayList<MyListViewRow> rowsList)
{
this.context = context;
this.rowsList = rowsList;
}
@Override
public int getCount()
{
return this.rowsList.size();
}
@Override
public Object getItem(int position)
{
return this.rowsList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
/**
* Returns a new row to display in the list view.
*
* Position - position of the current row.
*
*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/**
* Inflating the root view and all his children and set them to a View Object.
*
*/
View row = layoutInflater.inflate(R.layout.list_view_row,null);
// Get all the views in my row
this.a = (TextView) row.findViewById(R.id.a_id;
MyListViewRow myListViewRow = rowsList.get(position);
// Set values to all the views in my row
this.a.setText(myListViewRow.getA());
return row;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(context, "onItemClick LV Adapter called", Toast.LENGTH_LONG).show();
}
} // End of MyListViewAdapter class
Am I right?
Why would you have an OnItemClickListener inside the Adapter?! Either you have OnClickListener inside the Adapter, or the best practice is to set OnItemClickListener to the ListView or whatever AdapterView you use in your activity/fragment. The way you are doing it right now, it won't work for several reasons: