How to Get The Selected Item in ListView? - android

I'm building a simple App with a ListView in it. In the Java code, I'm trying to figure out which item(s) is currently selected. From the ListView documentation at http://developer.android.com/reference/android/widget/ListView.html I see there's a method in the ListView class called
public void setSelection (int position)
I looked for a similarly named method named getSelection(), but there is none.
So, first, how do I determine which item in my ListView is currently selected? I already have the object for the ListView, so I just need the method to tells me which item the user clicked on.
ListView myLV = (ListView) findViewByID(R.id.myListView);
Also, can I define a "callback" function which is called whenever an item is selected?
Finally, how can I use the documentation to figure this out? This is a very basic question which I should be able to figure out on my own. I did what seems to be the right thing: I looked at the Official Developer pages for the ListView class, but it didn't provide the answer. How can I answer these (basic) questions for myself?

Try this
mylv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View v, int position, long lng) {
String selectedFromList =(String) (mylv.getItemAtPosition(position));
}
});

I looked for a similarly named method named getSelection(), but there is none. So, first, how do I determine which item in my ListView is currently selected?
The super class AdapterView has getSelectedItem(), along with getSelectedItemId() and others. AbsListView has getSelectedView() too. But these only tend to work when you have used setChoiceMode()
The OnItemClickListener lets you know which it is clicked as it is clicked.
Finally, how can I use the documentation to figure this out?
You were right to start in ListView's documentation, but go to the "Inherited Methods" section and check the super classes as well.

You can get the position by using onitem click listener.by using on item click listener you can get position of selected item in the list.And you can put this position in Toast

Related

If I use simple adapter to feed the listview and the listview contains textview and button so how can I perform operation on specific position?

i want to find the position of the view which is the part of the list-view's custom XML file. How can find the position of the views. Can i automatically perform onItemClick?
I face this difficulty. In my case when i click on textview which is part of list-view's custom XML file then the position is right, but when i click on the button which is the part of list-view's custom XML file then it always gives first position.
After i have to decided to perform automatic onItemClick and store all the values of clicked position into another declared variable which is below.
See the image i used kotlin language instead of java. I stored all the map values into another variable which is declared above with public access specifier.
[Now when i perform operation on Button click i use
lvShayari.performItemClick(v, lvShayari.getPositionForView(v), lvShayari.selectedItemId)
this statement in which lvShayari is listview and performItemClick() method perform the onItemClick automatically when we clicked on button. Here v is the view, lvShayari.getPositionForView(v) method gives the position of v in my case it is button.]2
check out below code , hope it will help you:
listView.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, position, id ->
Toast.makeText(this, "Click on: " + position, Toast.LENGTH_SHORT).show()
}
for more Details follow this link
#Suvidha Malaviya
Make your own custom adapter for your listview with one interface in which there is one method with three parameters namely View, Position, Model class of your list which you are passing in your adapter.
After making this interface initialize that interface in your adapter and set that interface method in your view click event.
Now set this interface in your activity or fragment where you are using your custom adapter and get the value which you want...
For example, see below link in which there is explain how to use custom listener in list view adapter. Here you have to just make your own click listener in which you have to pass three parameters which I have mention above.
Try it. I hope you will resolve your issue...

Deleting an item from a listView

in a project i'm doing for school, I need to have a list of items (each item is added by the user pressing a button and such).
Each item will hold several objects/views, one of them being a button.
The button is supposed to delete the entire item (list row) created.
the code I wrote is useless, cause i made too many changes after searching google for 4 hours.
I know how to create a listView using my custom arrayadapter, how to add items to the list and how to customize the list's items (custom view). I can not figure out how to make that button delete the item it is in.
If you want to remove the item, following line of code is needed
arrayAdapter.remove(objectListAdapterMadeFrom.get(indexOfItemToBeDeleted));
Also call
arrayAdapter.notifyDataSetChanged()
if somewhere you are using setNotifyOnChange(false);
first of all, thanks for your answers.
what i eventually did was this:
added "int pos;" in main activity.
added a method in main activity:
private OnItemClickListener listener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
pos = position;
}
};
then, inside the button i added this line:
alarmList.remove(pos);
that fixed it.
i still don't know how efficient is my solution, would love to hear your thoughts.
btw, although i continued my project and added some stuff, if you still wish for the code, i'll post it all here.

Extracting user input from ListView

I am working on a simple app in which a bar tender can do the beer pay off automatically. The articles (beer, wine, whisky etc) are listed in a database and I use a SimpleCursorAdapter to map all the articles from the database to a ListView. Works perfectly.
So now it displays a list of articles, prices and a textfield where the bartender can input the quantity of the order (ex 10 beer).
Problem: I don't know how I can get those quantities back from the ListView. Does anyone know how to do this?
Thanks in advance
If you want to use the OnItemClickListener of your ListView, you can use the View view Parameter to access Sub-Items of the List View Item. You could replace the TextView in the following example with your EditText. Replace android.R.id.text1 with your R.id.x.
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView testAccess = (TextView)(view.findViewById(android.R.id.text1));
Toast.makeText(getApplicationContext(), testAccess.getText()
, Toast.LENGTH_LONG).show();
//do something
}
});
One potential way to do it is to set a button click listener inside of your Adapter getView() method, upon click that button can pull the value out of the EditText. Since you are inside the getView() method you should still have a reference to the EditText that you need.

Display item on click on ListView

I need help on a specific task :
I have a ListView in my main activity with a custom Adapter associated, and an OnItemLongClick listener that displays a Dialog with many operations configured (change quantity, cancel item...) and it works like a charm.
My goal is to have a physical copy of the clicked item in the Dialog, to display immediately the changes made.
Since my ListView item is complex and designed by the Adapter, i can't just get the reference to the original object, i need a visual copy of the selected ListView item...
I thought about creating the same structure (ListView and Adapter) with only the selected item associated but it's kind of heavy...
Is there a simpler solution that can help me in this situation ? I mean real copy of the original ListView Item with changes made in my Dialog done to the original object...
Posting your relavent code may help us to guide you more clearly.
However you can get your object in onItemLongClick((AdapterView, View, int position, long) using position params i.e
YourObject objReference = yourActivityClassObjectList.get(position);
now when showing dialog, pass this objReference to your dialog and when changes made, change the contents of this objReference and call
adapter.notifyDataSetChanged();
instead of passing the objReference to dialog, you can pass int position and while changes made, get the reference to that object, change contents and notify data set changed to Adapter.
When you click on one listview item the onListItemClick callback should be fired. As you see in the doc the third parameter is position. You can call YourAdapter.getItem(int) to retrive the element hold by the listview at position

how to access ListView with CheckedTextView?

I'm now developing an application that uses a ListView with a
CheckedTextView on every item that managed by an ArrayAdapter to
support multiple chooses. The contents in my ListView are dynamic, that
means, can be changed during runtime. Now I try to use
ListView.getCheckedItemPositions() to get all checked items, Because I want
to save all the checked positions and auto-check them when user go back to
this page again. So I need to save checked results for every page.
For the first page everything works fine as expected. But when user goes to
another page and make some chooses, the result array that ListView returned
contains some positions that are never checked. I don't why ListView has
this strange behavior. Even for a page that in fact no checked happens but
ListView gives me a result that indicates there's one item has been checked.
could anyone who can teach me how to get the position of CheckedTextView
in its OnClickListener callback?
example code is appreciate.
Thanks in advance...
The listview recycles its views so when you go to a different page and then return to the previous page, the listview recalls the getView() function for its views. To make sure that the order of the checked views are not mixed up, create an arraylist that contains the check state of all the views before initializing the adapter. Then pass the arraylist as an argument for the adapter's constructor. There, in the getView() function, set the checked state of each checkable textview based on the arraylist. Then, return to the activity class and override the onItemClick() event. Using the view that is given to you when the function is called, do the following to get the checkable textview and set its checked state:
listView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View selectedView, int position , long id)
{
CheckedTextView tv = (CheckedTextView)selectedView.findViewById(R.id.textview);
if (tv.isChecked())
{
tv.setChecked(false);
checkStatesOfViews.get(position) = false;
}
else
{
tv.setChecked(true);
checkStatesOfViews.get(position) = true;
}
}
});

Categories

Resources