Get value of item on OnItemClick Listview - android

I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?

The parameter v is the current row. so use:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
(Or you could use getChildAt(position) but this would be slower.)
Understand you might be able to simplify this more depending on your layout.

Just Small Change is Required
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
Since you are using custom view you need to pass the View argument in your OnItemClickListener then you need to use that value to get the Details of TextViews present in that

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(ListOfAstrologers.this,AstroProForUser.class);
i.putExtra("hello",adapter.getItem(position).getUsername() );
startActivity(i);
}
});
Here username is a string field declared in a class and you must over ride getItem() method in adapter class to get the details of inflated row when you click.

I dont have much experience but I think if you want to pass a variable from an OnClick (which is an anonimous class) to the outside of the onClick and to the other activity, you want to pass it via Intent intent.putExtra... (it's quite easy)
Otherwise you might end up using "public static" variable, which might be a memory leak...

Related

How to pass data from onItemClick to a TextView in another method?

I'm using an SQLite database to populate a listview. I've made it so that the user can not pass the data to the listview until a TextView has been populated.
Now I'm trying to get the TextView, which is defined in XML, to be populated by having the user click a button that should then display text on the TextView. However, I do not know how I can pass it to the TextView when it's not made programmatically.
So how can I pass the data (packageName) to TextView?
If relevant, my onItemClick looks like this:
myGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Putting package name into packageName
final String packageName = mApps.get(position).activityInfo.packageName;
ResolveInfo info = mApps.get(position);
info.activityInfo.loadLabel(getPackageManager());
Toast.makeText(getApplicationContext(), "Clicked " + packageName,
Toast.LENGTH_SHORT).show();
}
});
You would do need to declare your text view then put this inside the OnItemClick method.
myTextView.setText(packageName);

Custom adapter setOnItemClickListener parent.getItemAtPosition() always null

I have a custom adapter giving custom views to a listview (a few fields and an image). I've changed something, and for some reason my clickListener method parent.getItemAtPosition(position) is always returning null. Not sure how the change I made has made this effect. When stepping through it, it fires correctly, but for some reason the value is always null. Is there anything you can see?
ListView airportResults = (ListView) findViewById( R.id.listAirportResults );
List<SearchResultsItem> items = getSearchResults(searchTerm);
ResultsAdapter = new SearchResultsAdapter(this, items);
airportResults.setAdapter(ResultsAdapter);
My getSearchResults(searchTerm) returns the items in the listview with the following type:
List<SearchResultsItem>
Then the onclick listener:
airportResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long duration) {
String sClickedItem = (String) parent.getItemAtPosition(position);
//do something with sClickedItem
}
});
From the above code, the position parameter is correct (I tap on the first item, position=0; I tap on the 4th item, position=3 etc). The parent and view objects all have values. However, the sClickedItem is always null.
Many thanks,
J
Solved. It was my implementation of my custom adapter.
airportResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long duration) {
TextView textViewIata = (TextView) view.findViewById(R.id.list_searchresults_iata);
String clickedIataValue = (String) textViewIata.getText();
As I wasn't using standard objects, I had to make my own implementation. Simply: my view was a cutsom XML layout with various textviews in it, so I just had to view.findViewById() (because the view is the currently selected item in the listview) and getText() that way instead.
Thanks for making me realise this
J

Get specific id from a listview

I have a ListView which is implemented using a customs adapter. For making the adapter i am using a holder class. The class has various TextViews and ImageViews as well as an Int variable id to store the id being fetched from the database. Now when i click the particular list i want to get the id so that using it I can further display the information on a new activity. The id is not meant to be displayed in the ListView. How can i get the id from onItemClickListener()
You can set a tag to a View (.setTag()) and then retrieve it, it happens on getView() method inside your custom Adapter
Here is a sample code:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String transactionId = ((TextView) view
.findViewById(R.id.tvTID)).getText().toString();
handler.getTransactionDetails(callback, transactionId);
}
});
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
YourObject selected = adapter.getItem(position);
Then you can send the selected list item (Object) to your new activity
Intent mIntent = new Intent(ThisActivity.this, NewActivity.class);
mIntent.putExtra("list_selected", selected);
startActivity(mIntent);
as well as an Int variable id to store the id being fetched from the
database.
You didn't provide more infomation about that id (i don't know exactly what it presents) but to make it simple you can set this id for each widget in ListAdapter via setTag() method and then simply retrieve it from your onItemClick() method.

android get position of clicked item in gridview

as we know using android grid view, we can do the following and get notified when item is clicked:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(PopularCapitActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
we also know that, if the cell in the grid contains a clickable item, say a button, the above won't get fired.
so currently i have a grid view, each cell has its own button, so now when user clicks on the button, it will have its own action based on the cell the button resides in, my question is, how can i access the cell position in the button handler?
thanks
Assuming you are using a custom adapter for the GridVIew, in the getView method you can simply add a tag to the Button object that contains the position passed into getView:
button.setTag(new Integer(position));
Then, in the onClickListener method, with the view that is passed in (the button) you can do:
Integer position = (Integer)view.getTag();
And then handle the position value from there.
EDIT:
It appears the best practice would be to do:
button.setTag(Integer.valueOf(position));
rather than using the Integer constructor.
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String a = String.valueOf(position);
Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
}
});

list view issue in android

I am using a custom list view. I have tried to get the value of the row which the user clicked.
Can anybody tell me how to get the value?
Thanks
You may want to implement a new method in your class, specifically onItemClick:
[...]
private ListView lv;
[...]
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
String itemValue = lv.getItemAtPosition(position);
[... do something ...]
}
Then you can do whatever you want with with itemValue.
Hope this helps.

Categories

Resources