I'm trying to change one TextView in any view in my ListView,
And I already got one Adapter set so I can't use adapters for this.
I tried to do it in a "for" statement, using
mListView.getChildAt(i)
but it didn't worked.
when I tried to do it inside the OnItemClick method, using arg1 instead of
mListView.getChildAt(i), it worked for the item I clicked.
But i need it to happen without clicking on an item,
So my question is: What is the value of Arg1 and Arg2?
I know what they represent, but I want to know what value do they get when I click on an item.
Thanks.
you can fake a click through view.performClick() method if that is what you need
From Android documentation :
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters :
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.
Related
I have a list view where items have a text view and a button. I have managed to make list view onclick and button on click work together. The problem is that when I click the button I don't really know what list view item index it belongs too. Is there any way to know that? I need this to pass it to a "CRUD" for editing, etc...
You can use setTag and getTag here to get the position of the Button Clicked in the ListView,
Something like,
button.setTag(position); // in your getView() method
and then,
int cur_pos = (Integer)v.getTag(); // inside onClick of Button in getView() method
Some options:
You can have separate OnClickListener instances for each button.
You can call setTag() on your button to store arbitrary data (e.g. index or identifier) and retrieve it later with getTag()
An other solution would be to reproduce the button click on its parent, when the user tap on the button:
http://developer.android.com/reference/android/view/View.html#performClick()
I've got a Gridview, which gets populated with data from internet. One of the parameters is an id of an element - my goal is to store this id somehow in order to use it in onItemClickListener method.
At first in the activity I pull data with custom components, afterwards I use it adapter's method:
public View getView(int position, View convertView, ViewGroup parent)
Where and how should I keep relation between the grid's elements and actual data ids, so when I click on an grid element, for instance, activity with appropriate id parameter gets launched.
p.s. ID isn't showed in the gridview.
Make a custom object (class) and store all values inside the ArrayList, pass it to the custom adapter to display data inside the getView() method.
And on click method, you can easily have that clicked position object from the ArrayList.
I am modifying an adapter view. How do I implement the setSelection() of an AdapterView? What are the steps one must take when setSelection() method of an adapterview is called?
I tried browsing through ListView's source code, but it wasn't of much help.
Save the selected position and pass it to the BaseAdapter class. (You have to implement a custom Base Adapter class).
Then in getView() method, change as per your requirement by checking position==selectedPosition. (Note: you should call notifyDatasetChange() method to invoke getView() method again).
try List view onItemClick()
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
}
});
According to this and this you have to save selected item somewhere, show user that this item is selected(if you are not in touch mode), and you need to scroll view to this item.
Android-HorizontalListView is having HorizontalListView, which is an Android ListView widget which scrolls in a horizontal manner. In this class it's describes how to implement the setSelection() of an AdapterView.
I am trying to set item selected in OnItemClick event in ListView and it just wouldn't leave item selected. What am I doing wrong?
lView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(#SuppressWarnings("rawtypes") AdapterView parent, View clickedview, int position, long id)
{
clickedview.setSelected(true);
mItemsAdapter.select(position);
}
});
few things:
1. I am trying to implement Multiple Select on the list View.
2. I cannot extend from ListActivity because Activity extends from BaseActivity custom class already.
3. mItemsAdapter is a custom ItemsAdapter adapter that extends BaseAdapter.
4. I don't need a checkbox in there, just to be able to see the row selected is fine.
5. ItemsAdapter overrides getView() and sets the layout of the row by inflating xml
I could manage to get an item of a ListView to be set as selected when long-clicked:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
parent.requestFocusFromTouch(); // IMPORTANT!
parent.setSelection(position);
return true;
}
It only worked after I called requestFocusFromTouch().
I currently don't have much time. So I'll take a look again later this day.
Anyway take a look at my previous questions, I was struggling with the same:
Change ListView background - strange behaviour
Clickable ListView
In case the solution for you is not in there (I think it's in the first one) we will need more code, in order to help you.
Hope this helps a bit.
I have not had much luck using the built in functionality for selection.
I see you have your own custom adapter, which I am guessing means your inflating custom views as rows. If your row has anything more then a checkedtextview I don't think you will be able to correctly use setSelections.
I solved this problem by using my own models and functions. Each item in the list had data with it to determine it if was selected or not. I could then iterate though that array, toggle selections with and and even update the UI by changing values and calling notifydatasetchanged on the adapter (which used getView and checked against my selection model to draw checks).
I am using a ListView with a SimpleAdapter. Once data is populated into it I would like to set the first item in the list. This can be done by clicking on the list item on the screen. I want to just call the method directly after I populate the list so that when you see the UI its already done. My only problem is getting the View from the ListView. I noticed its children are all null but the SimpleAdapter has items in it. When I try to get those items they are not Views and I am not able to match the method call of
protected void onListItemClick(ListView l, View v, int position, long id)
because I cant get the right View. Any help would be appreciated.
After lots of searching the solution is not to make a manual call but to override the adapter getView method. That way I can change the background color and not have to manually call onListItemClick()