Reputation: 3328
I have a ListView where each row item has EditText and CheckBox elements. You can see the sample image below.
What I intend to do is
I want to implement both OnItemClickListener (for ListView) and OnClickListener (for EditText and CheckBox) i.e. I want Task A to be done on clicking the ListView items (outside EditText and CheckBox) and I also want to do individual tasks on clicking EditText and CheckBox respectively.
My Problem
If no attribute is set on ListView, EditText or CheckBox to control the focus, then the default behaviour is that ListView row items won't listen to item click, but EditText and CheckBox are getting the focus
On playing around with these attributes and there values (eg - )
// to ListView
android:descendantFocusability="blocksDescendants"
// to EditText and CheckBox
android:focusable="false"
android:focusableInTouchMode="true"
Listeners are working either for ListView or for the EditText. CheckBox click listener seems to work in all cases (strange). I am new to this and any kind of help would be appreciated. I cannot post the source right now, for that please bear with me. Any solution, suggestion or explanation would do a great help.
Upvotes: 1
Views: 889
Reputation: 3328
I am glad that I happen to solve this problem. What I wanted to do was (let me clear it out again) I wanted the EditText to be focusable and additionally I should be able to click on each row of the ListView to perform some specific task. Well we won't be needing these attributes:
android:focusable=""
android:focusableInTouchMode=""
android:descendantFocusability=""
What simple needed to be done is
In you custom ListAdapter class implement OnClickListener for each items (say EditText, CheckBox, etc.) and implement OnClickListener of rootView (i.e. for ListView row item) too.
For Example:
public class CustomListAdapter extends ArrayAdapter<YOUR_OBJECT> {
Context context;
ArrayList<YOUR_OBJECT> itemlist = new ArrayList<YOUR_OBJECT>();
Integer resourceID;
ViewHolder holder;
public ServicesListAdapter(Context context, int resourceId, ArrayList<YOUR_OBJECT> itemlist) {
super(context, resourceId, itemlist);
this.itemlist.addAll(itemlist);
this.context = context;
this.resourceID = resourceId;
}
public class ViewHolder {
TextView name;
EditText editText;
CheckBox checkBox;
}
public View getView(final int position, View rootView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (rootView == null) {
rootView = mInflater.inflate(resourceID, null);
holder = new ViewHolder();
holder.name = (TextView) rootView.findViewById(R.id.name);
holder.editText = (EditText) rootView.findViewById(R.id.port);
holder.checkBox = (CheckBox) rootView.findViewById(R.id.check_box);
// Add Listeners for EditText and CheckBox here
rootView.setTag(holder);
} else {
holder = (ViewHolder) rootView.getTag();
}
// Click Listener for the ListView row item
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Clicked" + position, Toast.LENGTH_SHORT).show();
}
});
// Do what rest you need to do with your ViewHolder
}
}
Upvotes: 1