Reputation: 987
I have a listview and custom layout consisting of TextView, ImageView, ImageButton. I want my image button to behave like RadioButton. But my onListItemClick event is not getting invoked.
public class MyListAdapter extends BaseAdapter {
Context context;
ArrayList<String> text;
ArrayList<String> Count;
int count;
public MyListAdapter(Context ctx, ArrayList<String> text, ArrayList<String> Count) {
this.context = ctx;
this.textAnswers = text;
this.optionCount = Count;
}
@Override
public int getCount() {
return Count.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.custom_layout, null);
}
ImageButton choice = (ImageButton)convertView.findViewById(R.id.ImageButton);
if(imageChoice != null) {
choice.setImageResource(R.drawable.radio_unselected);
}
TextView txt = (TextView)convertView.findViewById(R.id.TextView);
txt.setText(Count.get(position));
TextView txtText = (TextView)convertView.findViewById(R.id.TextViewAnswer);
txtText.setText(textAnswers.get(position));
return convertView;
}
}
And my onListItemClick as,
@Override
protected void onListItemClick(ListView listView, View v, int position, long id) {
super.onListItemClick(listView, v, position, id);
ImageButton choice = (ImageButton)v.findViewById(R.id.ImageButton);
choice.setImageResource(R.drawable.radio_selected);
}
Now plz any one help to give my imagebutton the behavior like radiobutton. Most important i missed is that my onListItemClick is not getting invoked. Also I have set the android:focusable="false" android:focusableInTouchMode="false" android:clickable="false" of all controls on my custom_layout. Thanks in advance.
Upvotes: 3
Views: 4485
Reputation: 11
You might need android:descendantFocusability="blocksDescendants"
set in your list item view. That solved it for me.
...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants">
...
Upvotes: 1
Reputation: 189
Set these values in the XML for the ImageButton, then it should work.
android:focusable="false"
android:focusableInTouchMode="false"
Upvotes: 0
Reputation: 1726
If your activity is a ListActivity and your list view has the id = @android:id/list than your onListItemClick will be called without you having to set the listener.
Upvotes: 2
Reputation: 724
You need to add a setOnItemClickListener to your ListView in the main activity, if you do not, you'll never get a call to the itemClick.
for instance:
OnCreate() Method:
ListView lv = (ListView) findViewById(R.id.listview1);
lv.setOnItemClickListener(this);
// Create the method below and let the activity implement onitemclicklistener
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//set action here
}
that should work :)
Upvotes: 2