Bojan Ilievski
Bojan Ilievski

Reputation: 1421

ListView with custom adapter, inside a Fragment doesn't fire onItemClick event

I have a ViewProvider class for the Fragment content, which returns a ListView for one of the fragments. The ListView has a custom adapter extending the BaseAdapter class. When I try implement an item click listener, it just doesn't work. I have this code for the listener:

lv.setOnItemClickListener(new ListView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View vi, int pos,
                long id) {
        try {
            Object o = lv.getItemAtPosition(pos);
            Event obj_itemDetails = (Event) o;
            Toast.makeText(context, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
            Log.d("xxx", "output: "+obj_itemDetails.getName());
        } catch(Exception e) {
            Log.d(Log.TAG, "Something is wrong...");
        }
    }
});

...not even the exceptions fires. In the same ViewProvider class, I've added another list with a SimpleAdapter, and it works just fine. I initialize the list the same:

EventAdapter sta = new EventAdapter(context, events);
final ListView lv = new ListView( context );
lv.setTextFilterEnabled(true);
lv.setAdapter(sta);

and the other one:

final ListView v = new ListView( context );
String[] from = new String[] { "str" };
int[] to = new int[] { android.R.id.text1 };
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
for ( int i = 0; i < 20; i++ )...//set the items here
SimpleAdapter adapter = new SimpleAdapter( context, items,
        android.R.layout.simple_list_item_1, from, to );
v.setAdapter( adapter );

What am I missing, where should I look?

Here is my EventAdapter:

public class EventAdapter extends BaseAdapter {

private LayoutInflater mInflater;

private List<Event> items = new ArrayList<Event>();

Context ctx;

public EventAdapter(Context context, List<Event> items) {
    mInflater = LayoutInflater.from(context);
    this.items = items;
    this.ctx = context;
}

public int getCount() {
    return items.size();
}

public Event getItem(int position) {
    return items.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    final Event s = items.get(position);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.eventTitle);
        holder.timestamp = (TextView) convertView.findViewById(R.id.eventTime);
        holder.location = (TextView) convertView.findViewById(R.id.eventLocation);
        holder.pic = (ImageView) convertView.findViewById(R.id.eventLogo);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.name.setText(s.getName());
    holder.timestamp.setText(s.getTimestamp());
    holder.location.setText(s.getLocation());
    if (s.getImage() != null) {
        holder.pic.setImageBitmap(s.getImage());
    } else {
        // MY DEFAULT IMAGE
        holder.pic.setImageResource(R.drawable.ic_launcher);
    }
    return convertView;
}

static class ViewHolder {
    TextView name;
    TextView timestamp;
    TextView location;
    ImageView pic;
}

}

Upvotes: 1

Views: 5581

Answers (2)

Maulik
Maulik

Reputation: 3316

I would like to suggest you that Activity and Fragment are almost same but the difference is where we use "this" or "ActivityName.this" we have to replace it by getActivity() and declare variables by using view which inflates our class layout:

Make sure you are using ListView find view by id like this.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
   // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.yourlayoutfile, container,
                    false);

    listView = (ListView) v
                    .findViewById(R.id.your_listview_id);

    listView.setOnItemClickListener(YourFragmentClassName.this);
}

Try to use by default onItemClick listener by implementing onItemClickListener.

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        Log.d("OnItemClicked>>", "called");
 }

Hope it will help you.

Upvotes: 1

Remmyabhavan
Remmyabhavan

Reputation: 1719

ListView on item click listener does not fire if you have any focussable items inside the custom list row.That is if you have any button or progrees bar or any control that takes focus,onitemclick will not fire

Upvotes: 6

Related Questions