Listview - list items updated with fetched data in background - android

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.

Related

Refresh listview in fragment

In my android application, I have one spinner box and two edit text box for adding item.When I click on add button all the data will store in database and display in below Item list (listview display in my first image) by refreshing listview.
My problem is, when I click on add button at a time only one list is display and listview heading is also gone.
May be this problem is raised due to refreshing of listview.
I searched on google and found notifyDataSetChanged() method is used for refreshing list view but I don't know how notifyDataSetChanged() method is used in fragment?
Does anybody know how to use this method in fragment?
I am new to fragment. In my app all the java code is developed using fragment activity, and xml file is simple layout.(LinearLayout/RelativeLayout)
You can see my screen here:
1)First image show list heading.
2)Second image show List item.
You need to setAdapter again to your listview after adding new data. it will show new data int listview.
if not get, post your code where you setAdapter to listview.
You have to first add item in Arraylist/List which you passed in Adapter then after you can call below code..
adapter.notifyDataSetChanged();
listView.invalidateViews();

Android: How to dynamically replace a list view with another list view like Instagram with onClick?

I'm building an android app and I'm trying to replace a list view with another list view if you click a button, like the notifications page on Instagram. On that page, if you click on the top "following" button it will show you a listview of what your followers have liked. If you click on the "you" button it will show you a listview of what people have liked your photos.
Any help would be greatly appreciated!
You can do it by following ways,
1. add Two listviews and can change the visibility as per your requirement.
2. On button click you can load the other data into same list view and can update your adapter in the same list view.
in 1 you have to load two list view at first which will consume more time if data is larger sure you can write a login in asynctask to load list views in background thread.
in 2 you have to update your adapter at the button so you will have to provide some progress bar of dialog for user while you list view is getting update.
You can use either of this whichever suites you best.
Simply , You don't need to switch Listview , you only need to switch adapters .
eg, you can switch to mFollowingAdapter when clicked on Following button and switch to mYouAdapter when you select "You" tab. that's it.
You should write a list, that has a custom adapter. This adapter will be able to display BOTH views you want to display.
If the data to be displayed is the same format (ie. both have an imageview next to a textview), you are in good shape.
When you want to switch to a different list, get the information you would like to display, replace the data in your the collection backing your list, then notify the list that the data has changed, and it should redraw.
So, this might look like:
create ArrayList() with data A
setup List, with this data and display
replace the ArrayList() with data B
call listView.notifyDataSetChanged
You can still do this if the Data A, and Data B have different views, in this case, you would need to handle this logic in your custom adapter.

Listview go to main when i click in item

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.

Delete Button on Listview

In my android app, I have a listview with an option to delete items from it
on the same screen. The delete button lies at the bottom of the list view
and there's a function written for deleting items and refreshing the list view.
This function is being called in onClick() of the List View.
This is where the problem lies.
In delete button's onClick I have first made a check for knowing which item to delete -
if (ListView.INVALID_POSITION != mListView.getSelectedItemPosition()) {
//delete the selected item
}else{
//do nothing
}
So whenever user touches delete button the focus from list view is removed and we get invalid as the list view position and hence the item is not deleted.
I also tried to store the value of selected item in a constant and update it in list view's onItemSelected() method and then remove the condition from Delete Button's onClick
.This works but causes another problem - If the user taps into empty area then focus from list view is removed and it appears that nothing is selected, but if u press delete button and then it deletes the last selected item as it is coming from a constant.
This is the problem I am facing.
Please suggest what to do.
Selection is useful only in keyboard mode, its turned off in touch mode so, getSelectedItemPosition() is not always reliable.
Read do's and don't in this developer blog entry.
If you want to use a single button, Set ListView choice mode to Single/Mutliple, then, on button click, get the checked items and delete them, refresh ListView after that.
Try to implement the onitemclickListener() and get the item id and delete the item clicked in the arrayadapter by implementing the onclick() for the button and next call adapter.notifyDataSetChanged();

Using checkbox in each listview item row, need to show selected item(s) name into another activity

I am using checkboxes in each listview item row, now I need to show selected item(s) name into another activity,here I am fetching data using a JSON parser.
I don't know how to get selected listview item name to another activity, I am using a submit button once the user has finished with checkbox selection, he/she just needs to click on submit and need to show selected item names into another activity. I know how to use the intent to send data to another activity.
I think you looking for a solution like I do before. Read this Tutorial, it will help ypu :-)

Categories

Resources