How to update a custom listview in android - android

I have an application developed to get all the third party applications installed and to display them on a list view! this is been done by an extended baseAdapter. In this list I do an uninstallation of a selected application using this code :
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + Packname));
startActivity(intent);
Now what I want is to update the list view with the changed data so that the user can get updated application list. how can I achieve this. I found that notifyDatasetchanged method but it can only be used for simple listviews! what are the options that I have and please let me know of any tutorials to achieve my outcome!
thank you.

What do you mean only for simple listviews? How is yours different? You should be able to call notifyDataSetChanged on your custom adapter class after deleting one of your items. You can create a function to do it...
private ArrayList<Object> list;
public void deleteItem(int index){
list.remove(index);
notifyDataSetChanged();
}

you will have an adapter (something that extends BaseAdapter probably) that you set into the ListView using setAdapter(...). add a method to your adapter implementation class that sets the underlying objects that are backing the list. make sure to call notifyDataSetChanged() at the end. something like,
public class MyAdapter extends BaseAdapter<String> {
private List<String> strings;
...
void setModel(List<String> strings) {
this.strings = strings;
this.notifyDataSetChanged();
}
}
simply calling your adapter's setModel(...) will cause the ListView to update.

Related

How to create a listarray globally in Android Studio and show the content in a list view?

I want to create an ArrayList, add values to it from different other activities and then show it as a listview in some another activity. I know how to create a list view but I am don't know how to create an ArrayList globally and add values to it via other activities. It would be really helpful if you can provide me with a sample code for the full problem. I don't want to go for any advanced method, as it is for a beginner college project and I want to explain my implementation method properly.
Basically, it's a shopping app where I am storing the name of bought products in an ArrayList on the click of a button. And then showing the final list in listview on the cart activity.
First, create a class to hold the ArrayList globally:
public class ListHolder {
public static List<String> list = new ArrayList<>();
}
Use the array list in any activity you want:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
... // Init views
// Obtain the list
List<String> list = ListHolder.list;
// Maybe add something to it
list.add("sample");
// Set the list to your adapter to render it by your ListView
yourAdapter.setData(list);
yourAdapter.notifyDataSetChanged();
}
}

Android ArrayAdapter sort() method on Listview keeps old row data unchanged

I have a ListView in my android app that does sorting. For example, when I clicked Name, it sorts by name.
Here is the code:
private class MyListAdapter extends ArrayAdapter<Person> {
private final ArrayList<Person> people;
//.......
public void sort(SortBy x) {
switch (x) {
case NAME:
this.sort(new OrderByName());
sortedBy = "NAME";
break;
Here is the Comparator:
private static class OrderByName implements Comparator<Person> {
#Override
public int compare(Person s1, Person s2) {
return s1.getName().compareTo(s2.getName());
}
}
When I click Name to sort the listview by name.. I got the list sorted but the old row of data still remains.. and associates with another row...
I am fairly new to Android... And I really need some help on debugging this issue. Thanks in Advance!
After sorting the list, just use the notifyDataSetChanged() method to your ArrayAdapter object. It will refresh the list. I'm assuming, you're not creating more than one ArrayAdapter object.
//notify that the list changed adapter.notifyDataSetChanged();
First check whether your ArrayAdapter have latest values in it.If it is updated with latest values used below :
adapter.notifyDataSetChanged(); // notify the listview to update items in the list.
Hope these helps you.

Android RecyclerView infinity scrolling: how to add more items to dataset

I have implemented my RecyclerView and even added an onscrolllistener to support infinity scrolling and now I'm stuck with a, hopefully, easy problem: How can I add the newly loaded data to the existing dataset?
My current approach: I create a new array with the length of the existing dataset + the length of the newly loaded data. I System.arraycopy my existing dataset and add the new content with a for-loop.
This works but the list is always reset (scrolls back to the top) and I assume my way to add additional content is overly complicated/wrong, though the tutorials I have looked at seem to pass over this "detail".
Update: I'm currently calling "scrollToPosition" on the UI-Thead after the data has been loaded, but I doubt this is the correct way of doing this or am I wrong?
You shouldn't be adding stuff to your dataset, you will sooner or later run out of memory. What you can do is return a big number (I used Short.MAX_VALUE) item in getItemCount inside your adapter and in the method that requests a view for postion you should do position % list.size();
It is not a truly endless RecyclerView this way, but good enough. I will paste some code tomorrow, I don't have it here now :/
I think you have to add items inside your adapter. Let`s say
class Adapter extends Recycler.Adapter<Recycler.ViewHolder>{
List<YourCustomObject> list;
public Adapter(){
list = new ArrayList<>();
}
public void addItem(YourCustomObject item){
list.add(item);
notifyItemDateSetChanged(); //This method for adapter to notice that list size have been changed
}
// Here your views
}
There is implementation of Your fragment or Activity where you retrieve data from internet.Let` say
class MainActivity extends AppCompactActivity{
Adapter adapter = new Adapter();
List<YourCustomObjects> objects;
public void onCreateView(){
//////// Something yours
}
public void onLoadMore(){
///// Your operation to retrieve data and init it to your list objects
for(YourCustomObject object : objects){
adapter.addItem(object);
}
}
}

Best way to update data with a RecyclerView adapter [duplicate]

This question already has answers here:
How to update RecyclerView Adapter Data
(16 answers)
Closed 5 years ago.
When I have to use a classic adapter with a ListView, I update my data in the ListView like this:
myAdapter.swapArray(data);
public swapArray(List<Data> data) {
clear();
addAll(data);
notifyDataSetChanged();
}
I would like to know what is the best practice for a RecyclerView. Because in a RecyclerView adapter you can't do a clear and addAll as in ListView.
So I tried just with a notifyDataSetChanged, but it didn't work.
Then I tried with a swapAdapter on my view:
List<Data> data = newData;
MyRecyclerAdapter adapter = new MyRecyclerAdapter(data);
// swapAdapter on my recyclerView (instead of a .setAdapter like with a classic listView).
recyclerViewList.swapAdapter(adapter, false);
But with this last solution, I still have to create a new instance of my adapter and I feel like it's not the best solution. I should be able just to change my data without a new MyRecyclerAdapter.
RecyclerView's Adapter doesn't come with many methods otherwise available in ListView's adapter. But your swap can be implemented quite simply as:
class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<Data> data;
...
public void swap(ArrayList<Data> datas)
{
data.clear();
data.addAll(datas);
notifyDataSetChanged();
}
}
Also there is a difference between
list.clear();
list.add(data);
and
list = newList;
The first is reusing the same list object. The other is dereferencing and referencing the list. The old list object which can no longer be reached will be garbage collected but not without first piling up heap memory. This would be the same as initializing new adapter everytime you want to swap data.
#inmyth's answer is correct, just modify the code a bit, to handle empty list.
public class NewsAdapter extends RecyclerView.Adapter<...> {
...
private static List mFeedsList;
...
public void swap(List list){
if (mFeedsList != null) {
mFeedsList.clear();
mFeedsList.addAll(list);
}
else {
mFeedsList = list;
}
notifyDataSetChanged();
}
I am using Retrofit to fetch the list, on Retrofit's onResponse() use,
adapter.swap(feedList);
DiffUtil can the best choice for updating the data in the RecyclerView Adapter which you can find in the android framework. DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.
Most of the time our list changes completely and we set new list to RecyclerView Adapter. And we call notifyDataSetChanged to update adapter. NotifyDataSetChanged is costly. DiffUtil class solves that problem now. It does its job perfectly!
Found following solution working for my similar problem:
private ExtendedHashMap mData = new ExtendedHashMap();
private String[] mKeys;
public void setNewData(ExtendedHashMap data) {
mData.putAll(data);
mKeys = data.keySet().toArray(new String[data.size()]);
notifyDataSetChanged();
}
Using the clear-command
mData.clear()
is not nessescary

How can i hide a listview item in android?

Hi friends i have a listview and the contents are fetched from a webservice call. In that webservice call, there are fields like
"OGType": "ORG" and "OGType": "GROUP"
If click a button, the listview must shows the item having "OGType": "ORG", and hide the item having "OGType": "GROUP". Hope you understand what i meant. Please anyone help me for that. Thanks in Advance.
Try to set new data (only with ORG) to adapter and then call
adapter.notifyDataSetChanged();
You can do it in your getView Method in your Adapter Class. That's the header
public View getView(int pos, View convertView, ViewGroup, parent)
There you can properly hide the element(s) you want, you know, using the method setVisibility()
For more help you can take a look here
You can create a custom adapter and pass data to it in the form of Array or ArrayList (ArrayList is better when dealing with Custom Adapters). Whenever you need to add or remove the data from ListView, just add or remove the item to or from you ArrayList and call notifyDataSetChanged() on your custom adapter and it will update the ListView automatically.
In your case, whenever you click a button, edit you ArrayList and call your custom adapter's method called notifyDataSetChanged() and that's it. You'll see every time you call this method ListView will refresh itself if you have made any changes to the data. Hope it helps.
NOTE - CUSTOM ADAPTER IS NOT COMPULSORY. ANY ADAPTER CAN BE USED e.g SimpleAdapter, ArrayAdapter etc.
You can use a visible list and filters lists. You should use "visible" for complete the BaseAdpter as always, then, you can change the pointer of visible to other list (all, filter...)
Don't worry by the memory, are pointers, you only have each element only once.
public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> visible;
private ArrayList<MyItem> all;
private ArrayList<MyItem> filter;
public MyAdapter(ArrayList<MyItem> items) {
all = items;
visible = all; //Set all as visible
filter = new ArrayList<Item>();
for (Item i : items)
if (i.getType().equals("ORG"))
filter.add(i);
}
//Complete adapter using "visible"
public void showOnlyOrg() {
visible = filter;
notifydatasetchanged();
}
}
The non hackish way will be to remove the items from your Collection which you use to generate the listview and then call notifyDataSetChanged();

Categories

Resources