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;
}
}
});
Related
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.
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
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.
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
I'd like to make a list adapter that formats views like this:
I want to be able to fire a different onClick when the user clicks the image. I have defined the onClick on the image itself in the getView() override, but how do I then get the position of the line that was clicked so I can update the record in the database to record the action?
First, you need the ListView which represents the adapter. If you store this somewhere already, great; if not, you can take the View which is passed to onClick() and call its getParent() method twice (or more, if the image is nested deeper within the clicked item's view) to get the ListView.
From there, call ListView.getPositionForView() on the View passed into onClick(). This will give you an int representing the position of the clicked item within the list adapter. From there, you can do whatever you want with it.
For example:
public void onClick(View v){
ListView lv = (ListView)(v.getParent().getParent()); // you may need more getParent()s and/or extra casting
int position = lv.getPositionForView(v);
/* Do whatever database stuff
* You want to do
*/
}