Get position in ListView - android

I use ListView to show several items.
My row.xml as below:
<TextView android:text="text"
android:id="#+id/tvViewRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button android:text="Click me!"
android:id="#+id/BtnToClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClick">
</Button>
And I define myClick in Activity as below:
public void myClick (View v) {
LinearLayout vwParentRow = (LinearLayout)v.getParent();
//How to get the position
}
How to know the position which buttom be clicked?
The position mean same as the method onListItemClick's.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}

Try
public void DetailClick(View v) {
ListView lv = getListView();
int position = lv.getPositionForView(v);
}

You can try like this.
Step 1: In your custom adapter
#Override
public view getView(int position, View convertView, ViewGroup parent){
.......//Perform your logic
convertView.findViewById(R.id.BtnToClick).setTag(position);
return convertView;
}
Step 2: In onclick listener
public void myClick (View v) {
LinearLayout vwParentRow = (LinearLayout)v.getParent();
position=(Integer) v.getTag();
}

Yes the position in the onListItemClick is same as the position of the item clicked in the list.

If I understand your question correctly, you have a button in each row of a ListView, and you want to know which row received the button click. How are you doing an setOnClickListener() on the button? The reason I ask this is - if you are setting the OnClickListener for each button, you already know the position of that button.

You should also read the documentation for ListView and even look through the available methods and their sample code.
Know your documentation.

Related

OnItemClickListener - changing the click area [duplicate]

So I have a custom ListView object. The list items have two textviews stacked on top of each other, plus a horizontal progress bar that I want to remain hidden until I actually do something. To the far right is a checkbox that I only want to display when the user needs to download updates to their database(s). When I disable the checkbox by setting the visibility to Visibility.GONE, I am able to click on the list items. When the checkbox is visible, I am unable to click on anything in the list except the checkboxes. I've done some searching but haven't found anything relevant to my current situation. I found this question but I'm using an overridden ArrayAdapter since I'm using ArrayLists to contain the list of databases internally. Do I just need to get the LinearLayout view and add an onClickListener like Tom did? I'm not sure.
Here's the listview row layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/UpdateNameText"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/UpdateStatusText"
android:singleLine="true"
android:ellipsize="marquee"
/>
<ProgressBar android:id="#+id/UpdateProgress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminateOnly="false"
android:progressDrawable="#android:drawable/progress_horizontal"
android:indeterminateDrawable="#android:drawable/progress_indeterminate_horizontal"
android:minHeight="10dip"
android:maxHeight="10dip"
/>
</LinearLayout>
<CheckBox android:text=""
android:id="#+id/UpdateCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
And here's the class that extends the ListActivity. Obviously it's still in development so forgive the things that are missing or might be left laying around:
public class UpdateActivity extends ListActivity {
AccountManager lookupDb;
boolean allSelected;
UpdateListAdapter list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lookupDb = new AccountManager(this);
lookupDb.loadUpdates();
setContentView(R.layout.update);
allSelected = false;
list = new UpdateListAdapter(this, R.layout.update_row, lookupDb.getUpdateItems());
setListAdapter(list);
Button btnEnterRegCode = (Button)findViewById(R.id.btnUpdateRegister);
btnEnterRegCode.setVisibility(View.GONE);
Button btnSelectAll = (Button)findViewById(R.id.btnSelectAll);
btnSelectAll.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
allSelected = !allSelected;
for(int i=0; i < lookupDb.getUpdateItems().size(); i++) {
lookupDb.getUpdateItem(i).setSelected(!lookupDb.getUpdateItem(i).isSelected());
}
list.notifyDataSetChanged();
// loop through each UpdateItem and set the selected attribute to the inverse
} // end onClick
}); // end setOnClickListener
Button btnUpdate = (Button)findViewById(R.id.btnUpdate);
btnUpdate.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
} // end onClick
}); // end setOnClickListener
lookupDb.close();
} // end onCreate
#Override
protected void onDestroy() {
super.onDestroy();
for (UpdateItem item : lookupDb.getUpdateItems()) {
item.getDatabase().close();
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
UpdateItem item = lookupDb.getUpdateItem(position);
if (item != null) {
item.setSelected(!item.isSelected());
list.notifyDataSetChanged();
}
}
private class UpdateListAdapter extends ArrayAdapter<UpdateItem> {
private List<UpdateItem> items;
public UpdateListAdapter(Context context, int textViewResourceId, List<UpdateItem> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.update_row, null);
} else {
row = convertView;
}
UpdateItem item = items.get(position);
if (item != null) {
TextView upper = (TextView)row.findViewById(R.id.UpdateNameText);
TextView lower = (TextView)row.findViewById(R.id.UpdateStatusText);
CheckBox cb = (CheckBox)row.findViewById(R.id.UpdateCheckBox);
upper.setText(item.getName());
lower.setText(item.getStatusText());
if (item.getStatusCode() == UpdateItem.UP_TO_DATE) {
cb.setVisibility(View.GONE);
} else {
cb.setVisibility(View.VISIBLE);
cb.setChecked(item.isSelected());
}
ProgressBar pb = (ProgressBar)row.findViewById(R.id.UpdateProgress);
pb.setVisibility(View.GONE);
}
return row;
}
} // end inner class UpdateListAdapter
}
edit: I'm still having this problem. I'm cheating and adding onClick handlers to the textviews but it seems extremely stupid that my onListItemClick() function is not being called at all when I am not clicking on my checkbox.
The issue is that Android doesn't allow you to select list items that have elements on them that are focusable. I modified the checkbox on the list item to have an attribute like so:
android:focusable="false"
Now my list items that contain checkboxes (works for buttons too) are "selectable" in the traditional sense (they light up, you can click anywhere in the list item and the "onListItemClick" handler will fire, etc).
EDIT: As an update, a commenter mentioned "Just a note, after changing the visibility of the button I had to programmatically disable the focus again."
In case you have ImageButton inside the list item you should set the descendantFocusability value to 'blocksDescendants' in the root list item element.
android:descendantFocusability="blocksDescendants"
And the focusableInTouchMode flag to true in the ImageButton view.
android:focusableInTouchMode="true"
I've had a similar issue occur and found that the CheckBox is rather finicky in a ListView. What happens is it imposes it's will on the entire ListItem, and sort of overrides the onListItemClick. You may want to implement a click handler for that, and set the text property for the CheckBox as well, instead of using the TextViews.
I'd say look into this View object as well, it may work better than the CheckBox
Checked Text View
use this line in the root view of the list item
android:descendantFocusability="blocksDescendants"

Click event not being captured for ListView items with HorizontalScrollView

I have a ListView with multiple elements for each row as follows:
list_element.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/global_container"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="#+id/scrollview"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
Fragment1.java is inflating the layout that contains the listview which has the elements above.
I need to be able to capture click events for each item to change the color of an element when the item is clicked and when another item is clicked restore it to its original color as the new item changes the element color and so on.
This means that I tried to capture OnItemClickListener inside Fragment1.java:
if (rootView != null) {
myList = (ListView) rootView.findViewById(R.id.my_list);
}
ArrayList l = new ArrayList();
//Bogus list
for (int i=0; i<20; i++) {
l.add(new Object());
}
adapter = new MyListAdapter(l, getActivity());
myList.setAdapter(adapter);
final MyListAdapter ad = adapter;
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
ad.setClickedPosition(position);
ad.notifyDataSetChanged();
}
});
But (I assume) all the Touch events of HorizontalScrollView are capturing the click/touch... event and I get no response. As you can see I tried android:descendantFocusability="blocksDescendants" and changing focusability back and forth with no results.
I also tried capturing different events inside the list adapter:
vi = inflater.inflate(R.layout.list_element, null, true);
vi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("aaa", "aaa");
}
});
And/OR
horizontalScroll = vi.findViewById(R.id.scrollview);
horizontalScroll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("aaa", "aaa");
}
});
Both options without any results. How shall I then capture a click event for an item on the list and keeping to be able to work with the HorizontalScrollView ?
Ok, I solved the problem as follows:
To be able to click on a list item this two things needed to be done.
First:
android:descendantFocusability="blocksDescendants"
needs to be present on the top-most element of the row element layout.
Second:
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
these 3 properties need to be present on the HorizontalScrollView.
The problem with that solution is that HorizontalScrollView won't work per se, so you need to activate it once the click event is detected. I do it on the getView() method of the adapter.
When the element is clicked I pass the position to the adapter:
fragment.java
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
//Private method inside the adapter
ad.setClickedPosition(position);
ad.notifyDataSetChanged();
}
});
adapter.java
public void setClickedPosition(int clickedPosition) {
this.clickedPosition = clickedPosition;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (clickedPosition == position)
holder.scrollview.setScrollingEnabled(true);
else
holder.scrollview.setScrollingEnabled(false);
}
CustomHorizontalScrollview.java
public void setScrollingEnabled (boolean enabled) {
this.isScrollEnabled = enabled;
}
//...
#Override
public boolean onTouchEvent(MotionEvent ev) {
//...
if (isScrollEnabled) {
return super.onTouchEvent(ev);
}
//...
}
Something to be considered is that this solution works for me since the behaviour "first click then swipe" is what I was looking for, so be aware this might not be exactly what you want but should help you in your quest to make that work.
try to change your list_element.xml as below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/global_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
</RelativeLayout>
hope this helps you

OnItemClickListener and OnClickListener in ListView

I have a custom view for each row in a custom ListAdapter and I am trying to perform onClick action and get the row position in the list where the click came from.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingLeft="10dip" android:id="#+id/itemRoot" android:clickable="false">
<TextView android:layout_width="wrap_content" android:text="TextView"
android:layout_height="wrap_content" android:id="#+id/itemTxt"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"></TextView>
<TextView android:layout_toRightOf="#+id/itemTxt" android:text="TextView"
android:layout_height="wrap_content" android:layout_alignBottom="#+id/itemTxt"
android:id="#+id/amountTxt" android:paddingLeft="6dip"
android:layout_centerVertical="true" android:layout_width="match_parent"></TextView>
<ImageView android:id="#+id/delBtn" android:layout_height="wrap_content"
android:src="#drawable/delete" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:paddingRight="10dip"
android:layout_centerVertical="true"></ImageView>
</RelativeLayout>
I want to figure out when TextView or ImageView is clicked, I also need to know what row in the list it came form. I have tried using OnItemClickListener and it works fine to get the row where the click comes from. However, once I register an OnClick listener for the views, the onItemClicked() method gets skipped and onClick() is executed straight away, but there is no way of getting the row position that the view is in(or at least not that I know if).
If I set clickable to false for the views, then onItemClicked get called and I tried manually calling performClick() on the given view. But this only works for the root element (RelativeLayout), and if click comes from TextView inside the layout the click doesn't propagate.
I can't really figure out how to get both position in the list and perform onClick action.
Any thoughts are welcome.
Alex
You could assign the proper OnClickListener to each ImageView and TextView from inside your ListAdapter's overridden getView method.
Inside that method you know the item's position, so you can pass it to the custom listener classes, and use it there as you want:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// TODO: instantiate the layout
// here I call a super method
final View view = super.getView(position, convertView, parent);
final TextView textView = (TextView)view.findViewById(R.id.itemTxt);
textView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.i("Click", "TextView clicked on row " + position);
}
});
final ImageView imageView = (ImageView)view.findViewById(R.id.delBtn);
imageView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.i("Click", "ImageView clicked on row " + position);
}
});
return view;
}
The other possible option to have the OnClickListener in the same class as the creating activity is to add to the activity implements OnItemClickListener.
public class DisplayListCustom extends Activity implements OnItemClickListener
Then set the custom list to listen
ListView list = (ListView)findViewById(R.id.custom_list);
list.setClickable(true);
list.setOnItemClickListener(this);
list.setAdapter(new CustomListAdapter(getApplicationContext(), listItems));
Finally in the onItemClick return, you can find the inner views by using resource ID
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
LinearLayout listItem = (LinearLayout) v;
TextView clickedItemView = (TextView) listItem.findViewById(R.id.name);
String clickedItemString = clickedItemView.getText().toString();
Log.i("DisplayListCustom", "Click detected " + clickedItemString + ", position " + Integer.toString(position));
This is the solution I went with. I used LinearLayout for my custom layout container, but I assume the same applies to RelativeLayout.
You need to extend your TextView etc, override the click method, do the appropriate actions and then the trick is to return false so it's then propagated to the listview.

ListView and Buttons inside ListView

I want to display a Button inside the ListView.
The goal should be to click on the ListView line or on the button.
Click on the line it shows more info.
Click on the button it shows at the bottom more buttons.
The same as the GMAIL app.
On the right there is a checkbox and after clicking on the checkbox at the bottom, the button bar appears.
My problem is after inserting the button into the ListView, the button is not clickable.
If I add the to the LinearLayout from the button llButton.setClickable() it works. But, only the button. The ListView itself doesn't react on clicks anymore!
I have tried this example.
The same issue as above...
Just to make this clear – and no one seems to have said something this simple – whilst one is not allowed to have a focusable button work in conjunction with the list view, there is a much simpler solution for this.
The accepted answer is a given - you should always do that when setting the click listener for list items, so that is silly that OP didn't know that.
If you are using an XML layout as your list item, simply set the button to have the following attribute and it will cause the list item to be clickable as well:
android:focusable="false"
Add the line below to your list item XML.
android:descendantFocusability="blocksDescendants"
Then your list item will be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content" >
// Your layout objects here
</RelativeLayout>
If you are using a custom Adapter the Button click inside a ListView will not work so you should try to use the following code to check for OnItemClickListener.
listId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
}
});
To have the event be triggered when either the button or the list item is clicked, you can do the following:
You handle only onItemClick:
mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// handle click here
}
);
In the adapter you modify the button to not be clickable/focusable (or do this in the xml file instead):
public class MyAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
.....
Button btn = view.findViewById(R.id.button);
btn.setFocusable(false);
btn.setClickable(false);
}
}
In my case i had to add this attribute in the listView :
<ListView
...
android:clickable="true"
...
</ListView>
And in the adapter just add on click listener in the button view.
wrapper.getButtonHi().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DebugUtils.logDebug("Clickeado :: "+ mContact.getUserId());
}
});
Its important to set final the variables:
public View getRowView(final int position, View convertView, ViewGroup parent) {
final BrowseContactItemWrapper wrapper;
final UserModel mContact = lstContact.get(position);
.....
}

Multiple choice list with custom view?

I've seen example com.example.android.apis.view.List11 from ApiDemos. In that example, each row takes the view android.R.simple_list_item_multiple_choice. Each such view has a TextView and a CheckBox.
Now I want each view to have 2 TextViews and 1 CheckBox, somewhat similar to the List3 example. I tried creating a custom layout file row.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="#+id/checkbox"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/text_name"
android:textSize="13px"
android:textStyle="bold"
android:layout_toLeftOf="#id/checkbox"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text_phone"
android:textSize="9px"
android:layout_toLeftOf="#id/checkbox"
android:layout_below="#id/text_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Then in Activity's onCreate(), I do like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Query the contacts
mCursor = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row,
mCursor,
new String[] { Phones.NAME, Phones.NUMBER},
new int[] { R.id.text_name, R.id.text_phone });
setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
The result kind of looks like what I want, but it looks like the list doesn't know which item of it is selected. Also, I need to click exactly on the CheckBox. In the List11 example, I only need to click on the item row.
So what do I need to do to make a multiple choice list with my custom view for each row? Many thanks.
You have to make your own RelativeLayout that implements the Checkable interface and have a reference to the CheckBox or to the CheckedTextView (or a list if it's multiple choice mode).
Look at this post:
http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/
The answer of Rahul Garg is good for the first time the list is loaded, if you want some rows to be checked depending on the model data, but after that you have to handle the check/uncheck events by yourself.
You can override the onListItemCLick() of the ListActivity to check/uncheck the rows
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ViewGroup row = (ViewGroup)v;
CheckBox check = (CheckBox) row.findViewById(R.id.checkbox);
check.toggle();
}
If you do so, do not set the ListView to CHOICE_MODE_MULTIPLE, because it makes strange things when calling the function.
To retrieve the list of checked rows, you have to implement a method yourself, calling getCheckItemIds() on the ListView does not work:
ListView l = getListView();
int count = l.getCount();
for(int i=0; i<count; ++i) {
ViewGroup row = (ViewGroup)l.getChildAt(i);
CheckBox check = (Checked) row.findViewById(R.id.ck1);
if( check.isChecked() ) {
// do something
}
}
Each such view has a TextView and a
CheckBox.
No, it doesn't. It has a CheckedTextView.
So what do I need to do to make a
multiple choice list with my custom
view for each row?
Try making the CheckBox android:id value be "#android:id/text1" and see if that helps. That is the ID used by Android for the CheckedTextView in simple_list_item_multiple_choice.
The solution is to create a custom View that implements the Clickable interface.
public class OneLineCheckableListItem extends LinearLayout implements Checkable {
public OneLineCheckableListItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
private boolean checked;
#Override
public boolean isChecked() {
return checked;
}
#Override
public void setChecked(boolean checked) {
this.checked = checked;
ImageView iv = (ImageView) findViewById(R.id.SelectImageView);
iv.setImageResource(checked ? R.drawable.button_up : R.drawable.button_down);
}
#Override
public void toggle() {
this.checked = !this.checked;
}
}
And create a custom layout for the list items using the new widget.
<?xml version="1.0" encoding="utf-8"?>
<ax.wordster.OneLineCheckableListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:background="#drawable/selector_listitem"
android:orientation="horizontal" >
<ImageView
android:id="#+id/SelectImageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/button_friends_down" />
<TextView
android:id="#+id/ItemTextView"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="#string/___"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/text_item" />
</ax.wordster.OneLineCheckableListItem>
Then create a new custom Adapter using the layout above.
It is possible by some trick
in your ListActivtyClass in method
protected void onListItemClick(ListView l, View v, int position, long id) {
//just set
<your_model>.setSelected(true);
}
now in you custom Adapter
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(textViewResourceId, parent, false);
}
if (<your_model>.isSelected()) {
convertView.setBackgroundColor(Color.BLUE);
} else {
convertView.setBackgroundColor(Color.BLACK);
}
return convertView;
}
this way you can customize the view in adapter when the item is selected in the list.
Simple example how to get a custom layout to work as custom checkbox:
private class FriendsAdapter extends ArrayAdapter<WordsterUser> {
private Context context;
public FriendsAdapter(Context context) {
super(context, R.layout.listitem_oneline);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rv = inflater.inflate(R.layout.listitem_oneline, parent, false);
rv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = friendsListView.isItemChecked(pos);
friendsListView.setItemChecked(pos, !checked);
}
});
WordsterUser u = getItem(position);
TextView itw = (TextView) rv.findViewById(R.id.ItemTextView);
itw.setText(u.userName + " (" + u.loginName + ")");
ImageView iv = (ImageView) rv.findViewById(R.id.SelectButton);
if (friendsListView.isItemChecked(position)) {
iv.setImageResource(R.drawable.downbutton);
} else {
iv.setImageResource(R.drawable.upbutton);
}
return rv;
}
}
I found it very useful this little code: http://alvinalexander.com/java/jwarehouse/apps-for-android/RingsExtended/src/com/example/android/rings_extended/CheckableRelativeLayout.java.shtml
It is a great addition to #ferdy182 's http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/ content.
Got the solution ... You can get the clicks on the views (like checkboxes in custom layouts of row) by adding listener to each of them in the adapter itself while you return the converted view in getView(). You may possibly have to pass a reference of list object if you intent to get any list specific info. like row id.
I want to confirm that the Pritam's answer is correct. You need an onClickListener on each list's item (define it in the adapter's getView()).
You can create a new onClickListener() for each item, or have the adapter implement onClickListener() - in this case the items must be tagged for the listener to know, which item it is operating on.
Relying on the list onItemClickListener() - as someone advised in another thread - will not work as the CheckBox will intercept the click event so the list will not get it.
And finally #Rahul and JVitella:
The situation is that the CheckBox on a list item must be clickable and checkable independently from the list item itself. Therefore the solution is as I just described above.

Categories

Resources