ListView: onItemLongClick Toasts Object - android

I want to toast Text of selected list item in ListView. I'm using Custom Adapter. I tried this code.
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,final int pos,
final long id) {
String text=String.valueOf(lv.getItemAtPosition(pos));
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
}
But it's Toasting Object name like com.demo.CustomList#413ffb10. Can anyone tell me how to Toast respective item?
Note: The above code works for Simple Adapter,but not for Custom Adapter

Assuming that the list view has objects of the type CustomList as per your code it is displaying the default toString() method of your CutsomList class. You should instead access the variables that you would like to display and toast it.
For example:
public class CustomList {
String title;
public String getTitle(){
return name;
}
}
And in the onClick you can do the following:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,final int pos,
final long id) {
CustomList customList= (CustomList )lv.getItemAtPosition(pos);
Toast.makeText(getActivity(), customList.getTitle(), Toast.LENGTH_SHORT).show();
}
This is exactly what you are looking for. Hope that helps.

I don't know the structure of your custom list view. But if you want to toast TextView value from your custom list view then do something like this :
String text= (TextView)view.findViewById(R.id.YOUR_LIST_ITEM_TEXT_VIEW).getText();
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();

Try this
String text=parent.getItemAtPosition(pos).toString();
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();

The getItemAtPosition() method returns an object belonging to the data structure the adapter works with, not an instance of the graphic widget

Related

How to get a variable that is inside onItemSelected method on Android?

I'm trying to get the value of an item that is inside a Spinner, and then I want to send it to a database.
However, the code:
String variable = spinner_name.getSelectedItem().toString() does not work at all
So it seems the best way is through of the method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_LONG).show();
}
But how do you get the value of this variable called text, which is inside the above method, to insert into another method?
Create a CustomAdapter for your spinner, pass a callback as parameter when creating the customAdapter. Once you click on a item on spinner, because of callback, you'll have the item on your frag/activity.
From there you can call any method you want.

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);

Get value of item on OnItemClick Listview

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...

How to get the selected item from ListView?

in my Android app I have created a ListView component called myList, and filled it with objects of my own custom type:
class MyClass{
private String displayName;
private String theValue;
... //here constructor, getters, setters and toString() are implemented
}
I used the ArrayAdapter to bound the ArrayList theObjects with myList:
ArrayAdapter<MyClass> adapter=
new ArrayAdapter<MyClass>(this, R.layout.lay_item, theObjects);
myList.setAdapter(adapter);
This works fine, the list is populated and etc., but when I'm trying to access the selected item, i receive a Null object. I've done this using
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) myList.getSelectedItem(); //
String value= selItem.getTheValue(); //getter method
}
What seems to be the problem? Thank you
By default, when you click on a ListView item it doesn't change its state to "selected". So, when the event fires and you do:
myList.getSelectedItem();
The method doesn't have anything to return. What you have to do is to use the position and obtain the underlying object by doing:
myList.getItemAtPosition(position);
You are implementing the Click Handler rather than Select Handler. A List by default doesn't suppose to have selection.
What you should change, in your above example, is to
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass item = (MyClass) adapter.getItem(position);
}
Since the onItemClickLitener() will itself provide you the index of the selected item, you can simply do a getItemAtPosition(i).toString(). The code snippet is given below :-
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s = listView.getItemAtPosition(i).toString();
Toast.makeText(activity.getApplicationContext(), s, Toast.LENGTH_LONG).show();
adapter.dismiss(); // If you want to close the adapter
}
});
On the method above, the i parameter actually gives you the position of the selected item.
On onItemClick :
String text = parent.getItemAtPosition(position).toString();
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) adapter.getItem(position);
}
}
Using setOnItemClickListener is the correct answer, but if you have a keyboard you can change selection even with arrows (no click is performed), so, you need to implement also setOnItemSelectedListener :
myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
MyObject tmp=(MyObject) adapterView.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// your stuff
}
});
In touch mode, there is no focus and no selection. Your UI should use a different type of widget, such as radio buttons, for selection.
The documentation on ListView about this is terrible, just one obscure mention on setSelection.
Though I am using kotlin, the following code answered your question. This return selected item:
val item = myListView.adapter.getItem(i).toString()
The following is the whole selecteditem Listener
myListView.setOnItemClickListener(object : OnItemClickListener {
override fun onItemClick(parent: AdapterView<*>, view: View, i: Int,
id: Long) {
val item = myListView.adapter.getItem(i).toString()
}
})
The code returns the item clicked by its index i as shown in the code
MyClass selItem = (MyClass)
myList.getSelectedItem(); //
You never instantiated your class.

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