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
Related
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...
The problems is: I have a listview_products, for instance, and I have to select one of them per time and put it into another listview_sales, the listview_sales is going to hold all the products that I've been choosing for then make the proccess of sale completely. How could I achieve this behavior? Could you lend me a hand with this please!
You should have an ArrayList with object from where your first adapter gets data to draw.
Then you will need another arrayList with the objects of the first that will be drawn on the second ListView.
So, if you implement a OnItemClickListener in your first arraylist to get the selected items.
This is just a pseudocode example.
void OnItemClicked(int position) //This is not the real method definition
{
secondArray.put(firstArray.get(position));
}
Then when you want to show that second List just pass that data to its adapter
secondListView.setAdapter(new SecondAdapter(secondArray));
Hope this helps.
)
I found a tutorial about using the ListView in android. But I have a question about it. Here the tutorial:
http://www.vogella.com/articles/AndroidListView/article.html#listadvanced_interactive
You have to scroll to section 13.2.
The idea of the tutorial is on one hand you have a ListView (with checkboxes in each item), on the other hand you have an ArrayList (the items of the ArrayList are objects, which contain the information which are displayed in the items of the ListView, e.g. CheckBox checked or not, text etc.). The adapter schould keep both things equal. If you change the ArrayList, the ListView will be changed, too.
But now my question. If the user touch on one item of the List, the adapter will call the method "onCheckedChanged". But what happen there? An object will create there and get a tag from the the CheckBox. Ok. Now the method is done. The garbage collector will destroy the object or doesn't? What is when I need this infomation from there in my Activity. Imagine I have a button "Delete" under my list. So I have to transfer these information from the listener of the adapter to my Activity. How I can ensure that I use the same ArrayList with the right information in every class?
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
I hope you know what I mean. Can you explain it to me please?
Sorry for the language, but english isn't my mother tongue.
Bye
If I understand your question, the response is in the Chapter 14.1 of the tutorial you gaved...
If you want to do some treatment when delete is checked you add a listener in the ListView.
These functions normally have the position clicked and you can do whatever you want knowing the position clicked. For exemple:
list.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MyList.this,
"Item in position " + position + " clicked",
Toast.LENGTH_LONG).show();
// Return true to consume the click event. In this case the
// onListItemClick listener is not called anymore.
return true;
}
});
I'm not totally sure I understand you're question. There's a lot to unpack in there but I will give it a shot. To understand what's going on you need to understand the relationship between the ListView and the data that backs it which is contained in the ListAdapter.
The ListView displays the data contained in the ListAdapter. The ListAdapter gets its data from some other source, in this case its the ArrayList that you're referring to. Why use an ArrayList? In the tutorial its not entirely clear because the example uses a fixed set of data (e.g. a list of operating systems) that are written out as strings. In practice, however, often times you are using a dynamic list, like a list of users, places, whatever.
The ArrayList holds the objects that will eventually be displayed. This is the "models" class in the tutorial. The same applies for any other class of data, like a list of places. If you imagine a place as a class, that class would contain fields that describe the place (e.g. address, a description, some other unique features). As data is downloaded from some location, a new instance of place would be created for each location. Those objects would be collected in an ArrayList. When the downaloding process was finished, you would then begin the process of passing the contents of that ArrayList to the ListAdapter so that the ListView could eventually be updated.
How do you know you will always use the right ArrayList? Because your ArrayList is linked to you ListAdapter. The data the the ListAdapter processes, whether a list of models or users, is supplied by the ArrayList. You cannot use the wrong one. The same goes for the ListAdapter. Because you must set the adapter that the ListView is linked it it will always pull from the correct source. You cannot use the wrong data.
What's happening with the onCheckChanged listener, if you pay close attention, is that the user is really just updating a field contained in the model class. Each modle has an isChecked field. When the check box is clicked, that value for the object is updated. This is how you know what is checked and what is not.
Response to: "Nobody told the adapter "Hey it belongs to the list, which is your source"
Look at the documentation for ArrayAdapter
and for ListView
The constructors for the ArrayAdapter all take some list of object as an argument. This list would be the list of object that will be displayed within the ListView.
Additionally the ListView requires that you call setAdapter which takes some ListAdapter as its argument. This is what actually tells the ListView the source of the data it is going to display.
In that sense, the List, ListAdapter, and ListView are all linked together.
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;
}
}
});
I have a ListView displays information about an object. When I click a ListView Item, I open an Activity that let's me manipulate parameters of the object held in the adapter. These parameters are updated and stored remotely.
When I return to the ListView (via the back button), I want to update the ListView Item that I clicked originally by requesting the parameter values from the remote server.
I am currently doing this up updating the entire ListView by clearing it and rebuilding it.
How do I reference the ListView Item so that I can update the data for that item only?
Thanks,
Jason
Just set the data for your adapter and call notifyDatasetChanged. Android only draws the rows that are visible anyway, so it's pretty efficient.
Knowing the position of the view, this can be done.
View singleItemView = myListView.getChildAt(position);
ImageView icn = (ImageView) singleItemView.findViewById(R.id.icn_icon);
ProgressBar prg = (ProgressBar) singleItemView.findViewById(R.id.prg_icon);
icn.setVisibility(View.GONE);
prg.setVisibility(View.VISIBLE);
(EDIT)
This doesn't work because the "position" of the View could be changed upon returning from another Activity. After the onResume() fires, getView() is called and the position of the visible ListView Items are numbered starting with 0. So, the ListView items (id and position) become renumbered based on visiblity. As a result, I end up accessing the wrong object.