I have a small application, and I am trying to display list of youtube thumbnails using the ListActivity
public class ResultListActivity extends ListActivity
your imageview xml of baseadapter add this line
android:clickable="false"
android:focusable="false"
You need to add row.setOnClickListener(new OnItemClickListener(position));
in your ResultViewAdapter for getView()
For ListActivity its simple to call setOnItemClickListener
The corresponding code is below
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
System.out.println("LIST ITEM POSITION "+position);
}
});
In my ListView
I have an clickable TextView
and clickable ImageView
. onListItemClick
didn't work for me since I have more than one clickable items in the list row. So what I did is i implemented onClickListener
in my getView()
for the adapter. Here is my code:
public class SimpleArrayAdapter extends ArrayAdapter<String>{
private LayoutInflater mInflater;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public SimpleArrayAdapter(Context context, int textViewResourceId, String[] names, TypedArray icons) {
super(context, textViewResourceId, names);
// TODO Auto-generated constructor stub
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStrings = names;
mIcons = icons;
mViewResourceId = textViewResourceId;
}
public int getCount(){
return mStrings.length;
}
public View getView(final int position, View convertView, ViewGroup parent){
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
iv.setTag("colorChooserClicked");
iv.setOnClickListener( new OnClickListener() {
int pos = position;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("text", "Image clicked, row %d"+pos);
}
});
TextView tv = (TextView)convertView.findViewById(R.id.textview01);
tv.setText(mStrings[position]);
tv.setTag("textViewClicked");
tv.setOnClickListener(new OnClickListener() {
int textpos = position;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("text", "Title clicked %s"+mStrings[textpos]);
}
});
return convertView;
}
}
It happens since we have more than one clickable item and we are trying to use the ListActivity
. Else the other option is to extend Activity
class for your MainActivity
and then do it.
ListView lv = getListView();
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
Log.d("test","Inside onListItemClick");
}
});
if you have a class called ListActivity make sure you are extending from the right listActivity because when extending, both your class ListActivity and the super class ListActivity will show up