Listview go to main when i click in item - android

I have an app that has a ListView and when I click on some item, it adds the item in an array.
But when I click on some item I don't want change the view, then in Android 4.0 the ListViewgoing to first, but in Android 4.2.2 the ListView doesn't change. Why?
I always want the same result, the second option.
Is there any property for this?

Try making use of the getFirstVisiblePosition() and setSelection() methods of the listView. The first gets the first listview item present on the screen and the second moves the listview to the proper index when you comeback to this activity.
Please read the documentation for some more details about these functions.

Related

Android open ListView for multiple selection programmatically

I got a fragment with a ListView. The purpose of the list is simply checking items in that list, so I want to add something to the onCreateView method which will make the list enter its multiple selection mode automatically as the fragment shows, without the need for the user to long press an item. How can I do that?
to use longpress , you can do this way,
android:choiceMode="multipleChoiceModal"
or
setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL)
go through this for more information

List focus on a particular item in that list dynamically

In my project i have a ListView for handling the list of a product. In that i have a list of 100 Items in that List view. Each and Every time i have been seeing the first row of the ListView..
If i need to see the data in the ListView Means, i need to scroll down, Can i focus directly to a particular item by any function by passing the position(ID) of the ListView..
For Example this is my ListView that contains 11 items in it..
I'm seeing every time the ListView as the following,..
And my try is, If i give the ListView position as ID=5 means, can i get the Focus of the ListView as follow, in the opening itself...
Can we get this by ListFragment method or anything else?
Suggest me for the Best..
try this, use setselection
listview.setSelection(position);
Don't know about ListFragment but in listview, you can use listview.setSelection(position);
This method will help you to achieve this
for example: listview.setSelection(5);

Android first row not selected using setSelection(0) method

after multiple try's, i'm using "mListView.setSelection(0);" to select the first row in my ListView when the list appears. But dunno why its not working. So how can I let the first row always clicked? Because in my application i have a listview on left side and a webview on right side. So i need that when my app launchs, the first row always selected so the webview loads the details of the first element per default. The data of the listView are loaded using a cursor Adapter that communicates with an SQLite Database.
This is a part of my code :
code
Try setting up an OnGlobalLayoutListener to your ViewTreeObserver, which sets the selection as soon as the layout is set. (Remember to remove the listener as soon as it's called the first time to avoid repeated calls.)
Check the android sources whether setSelection(int) handles unpopulated lists right.
You can try using smoothScrollToPosition(int position) it should work for you.
mListView.smoothScrollToPosition(0);

how to keep check box selection in ListView

I have created a custom ListView.. every element of my ListView constructing from a layout.
My layout will have some TextViews and a ToggleButton. My ListView will have some 50 elemnts and my device screen can show 2 elements at a time.
Now if we select ToggleButton on a Item and scroll down my ListView , the selected item will be out of screen and when I come back to the previous selected item I am observinf that selection for that toggle button is gone.
I know that this is a normal behavior in android that It will construct the elements which is currently displaying.
But I want to keep that value.. Is there any way that we preserve selection. or can I tell ListView to donot construct every time.. memory is a not a issue for my application.
Please let me know if there's any good solution for this.
You could store the states of the checkbox in a map with the position as the key. So whenever the list reloads after scroll, it loads based on the hashmap state. You should write this logic in the getView()

Listview - list items updated with fetched data in background

Currently i am having listview with multiple items (First time, i fetch data with the use of AsyncTask), now i want to implement the below functionalities:
I am showing Option Dialog(With options Edit, Delete, Read) whenever user do long-click on any item, now if user select "Delete" option, at that time i want to remove item from the listview, listview should be displayed without that item meanwhile the data should also be fetched(in background) from the web.
Anybody knows, how do i implement such?
on the action of delete, delete the data from the source array, and call notifyDataSetChanged() on your adapter. and from the place where you call notifyDataSetChanged, you can do all the web stuff.
Use notifyDataSetChanged() over your Adapter
For display a dialog box when log press of listview item use onContextMenu() method in that onContextmenu u use the listview object and in that contaxt menu u write the code for display a dialog box with 3 options and next one remove item from listview when u click on the delete button in that dialog box is in delete button click listener remove the item from the listview and again set that remaining elements to that listview.

Categories

Resources