I have ListActivity with CustomArrayAdapter. My extension of ArrayAdapter allows me to do add/remove operations with the rows. It works fine...inside ListActivity. But then I need to add/edit rows in my list from different Activity and troubles begin. How can I do that? All I need inside my EditActivity is CustomArrayAdapter object. I made it static and tried. It worked, but is it a right thing?
Thanks for the answers, masters!
What comes in my mind:
One way could be to make your objects in the ArrayList serializable you could pass then the actual list to the next activity via intent and use it there for the adapter.
Other way would be: if you know the ID of the element which you want to delete, pass it as intent, delete it from the ArrayList and set adapter.notifyDataSetChanged();
I don't think to have a static ArrayAdapter class is the right way. This means you affect both activities at the same time. Because none of them have an own object.
Hope this helps
Related
There is a need of sorting record in recycler view adapter, based on the platform chosen from buttons in fragment.
I can't use interface as I already used in Adapter to listen in fragment. if I use interface now it will cause cyclic redundancy.
Can you please let me know the procedure for this. Thanks!
I'm sorry I'm only allowed to upload image links, Thank you very much for all your support and looking forward for the solution!!
you get a variable from the fragment using intent, later you pass that variable to the recycler view through the adapter (create a method to update a variable inside the adapter class) and I'll say to update the adapter everytime your fragment variable changes.
if you provide a screenshot of your code I can give a better answer, it depends how you implemented it.
I need to populate a ListView in a Homescreen Widget. I know I use RemoteViewsService.RemoteViewsFactory as a sort of Adapter.
I also know how to populate the rows in getViewAt() method to show this.
However, I need to populate an ArrayList of Objects from the Network and pass that into the getViewAt() I am not sure where or how to go about doing this?
In a normal adapter, I simply pass this in the constructor. Would it be the same here?
my problem is the next one:
I want to use a ListActivity with recursion, so that it is called "x" times, and everytime i click in one article on the list, it is re-calling again the same Activity but it is loading different data, cause what I wanna do is to make menu and sub-menus, and the expandableList is not enough for me because there are gonna be n-levels(i will know dinamically.....).
Anyone has an idea how can I implement it??
Thanks!
Store every data in some kind of N level ArrayList. Write your Adapter class to accept a level of this list. On the item click, it passes to the next level of your ArrayList and you call a notifyDataSetChanged() on your list.
I have an ArrayList containing data for displaying. The content changes asynchronously.
I want to display the data in two Activities, both are using an ArrayAdapter (not the same class).
The problem is that ArrayAdapter provides synchronized access and notifications through add, insert and remove. So synchronized access is only possible through one and not two adapers.
The ArrayAdapter itself is not resusable since diffent views are used.
So the question is: what is the recommended architecture for having one ArrayList with multiple ArrayAdapter's?
Update
I would like to clarify. At the moment I have only one ArrayAdapter.
the data is stored in an ArrayList
a service is updating the data in the background via the ArrayAdapter
both Activity and service are accessing the ArrayList via the ArrayAdapter (multithreading synchronisation issues), but this is no issue because ArrayAdapter does the locking
Now I have another activity which should also display the same ArrayList and I don't know what to do. Clearly I need another ArrayAdapter, because the second activity has another layout. This means that two activities and a service are accessing the same ArrayList. The synchronisation of the ArrayAdapter is not sufficient any more, because the locking is in the ArrayAdapter, which means if service and activity 1 are using ArrayAdapter 1, ArrayAdapter 2 will still access and modify the ArrayList.
The content changes asynchronously.
I'm not sure I understand how you use the ArrayList between the two activities. (static field?)
The problem is that ArrayAdapter takes ownership for the array (it
duplicates it).
I don't believe it duplicates it. It stores a reference to it. (correct me if I am wrong)
When Activity2 is activated and creates ArrayAdapter2, the ArrayList
still belongs to ArrayAdapter1.
Both ArrayAdapters should have a reference to the same ArrayList at this point. This means that a change in the ArrayList would be reflected in both adapters.
Use one global array adapter for two activities and observe you async data.
It is not a problem. After changes in arrayList through one adapter, call notifyDataSetChanged() for the other one. That is all you need, IMHO. At least, it is enough for my ArrayList that is also in different Activity with its adapter. And I change it "by hand" and notify the adapter later, too.
The serious problem will be if you'll want to have one ListView with two adapters. But that is a senselees task, I think.
Updating the answer up to the updated question:
So, the problem is not in the two activities that are using the same arraylist by different ArrayAdapters, but in three components using possibly two different arrayadapters. But if you are using the second adapter only for displaying of the same list, as you are writing here, you needn't any additional synchronization at all. Simply call adapter2.notifyDataSetChanged() after every significant change.
The problem could arise only if you are doing simultaneous changes through two different adapters. Each wouldn't be notified in time on the changes made by the other one. As for synchronization between Activities in the case of probable simultaneous writing, you can reach it by notifying after every change focus from one Activity to another.
But writing synchronization between service+adapter1 and activity+adapter2 could be reached only if you'll find youself some important points in your code where the mutual notifications and locking/unlocking should be made.
i would like to know how to send an ArrayAdapter that i'm filling in one activity, to a ListActivity display it.
The program itself searches for bluetooth devices around and fills the ArrayAdapter with it's names, then i would like to sent the created list to a ListActivity so the user can select one.
Thanks
Check this How do I pass data between Activities in Android application?. It will be better not to send the ArrayAdapter via Intent but to send only the data (ArrayList or even String[]) and then to construct the ArrayAdapter in the new Activity.
You can take a look at this which explains how to pass a int[] across activities through Intent.putExtra(). Another option is that you would make the data that you want accessible by the other activity as public static so the other activity could just access it freely.