I am trying to update a String in a ListView, and have the update be reflected in the ListView. Here are the basics of what's going on:
testStrings = {"String", "String", String"};
ListView l = (ListView) findViewById( R.id.listView1 );
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, testStrings);
l.setAdapter(adapter);
Later, testStrings is update like so:
testStrings[1] = "new string";
Note, just the text is changing, not the cardinality of the String array.
On a Samsung Galaxy Nexus with Android 4.3, the text in the ListView is updated as expected, with no additional calls to the adapter.
On a Google Nexus 7 running Android 4.4.2, nothing happens. On the Nexus 7, I've tried calling notifyDataSetChanged(); on the adapter, and that doesn't do anything either.
I've seen a number of questions about ListViews not updating, but those examples seem to be doing something more complicated than what I'm trying to do. Why is one device working fine, but not the other? How do I get the ListView on the Nexus 7 updating correctly?
I think you need to be more direct about modifying the adapter. When I walk through the Android code that constructs ArrayAdapter using a String array, it first makes an ArrayList from your String[], and then makes the ArrayAdapter from the ArrayList. While the source does seem to point to the ArrayList and then the array, it does seem a risky proposition to expect ArrayAdapter to be aware that you have modified the String[] deep inside it. It is interesting that calling notifyDataSetChanged works on one device and not another. But I think a much better approach is to remove and then add the strings from the adapter. This way the adapter will know that something is happening to it.
adapter.remove(adapter.get(1));
adapter.insert("new string", 1);
Related
I am trying to understand what other alternative I have if I can't write this code:
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,bluetoothDevices);
recyclerView.setAdapter(arrayAdapter);
I have started to learn Java and I am taking a freshman class that requires us to write a mobile app, but there is no instruction on how to do this. This class is not a programming class, it's a project based course so everyone has to learn everything on their own. I have literally been introduced to android a week ago.
My other question is how does android store text files?
ArrayAdapter isn't meant to be used with RecyclerView. If you want to keep your ArrayAdapter, you can change your RecyclerView to a ListView:
// Change to a ListView
// (Remember to change it in your layout as well)
ListView myListView = findViewById(R.id.my_list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myStringItems);
myListView.setAdapter(adapter);
This will work fine for simple data but, if you need to display more complex data or if you require more customization or performance, using RecyclerView is the recommended approach.
My wanna do the search thing on my app, and I'm using a AutoCompleteTextView that use more than one string-array stored on resources, I'm doing this way:
List<String> list = new ArrayList<>();
String[] array1 = getResources().getStringArray(R.array.array1);
String[] array2 = getResources().getStringArray(R.array.array2);
...
list.addAll(Arrays.asList(array1));
list.addAll(Arrays.asList(array2));
...
AutoCompleteTextView autoView = (AutoCompleteTextView) findViewById(R.id.auto_complete);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
autoView.setAdapter(adapter);
It works nicelly, but I'm in doubt when performance and memory of my app, 'cause are many itens on each array (altogether more than 400), so I'm wondering if create a list to add all itens (like I'm doing) can take up too much memory?
This way I'm doing is wrong or it's right? Can harm the app performance?
Anyone knows?
Thx
It doesn't create much problem. It just consume a little memory. I am using approx 8000 items in the list with Auto Complete TextView, Which works fine for me.
If you are worried about this thing, just check your memory usage on device monitor with the both cases.
You can do this like this:
Connect your device via ADB.
Run your App.
Open Logcat/Android Moniter.
Go to monitor tab.
In the monitor tab, you can see CPU, Memory & Network Usages.
Hope this will help.
I am developing a POS app for a restaurant. Things work fine, except that after some time the app gets very sluggish. The response appears to be 2 seconds late, and then we need to restart the application.
Here is how it looks like:
Each time when a food category is selected, e.g COLD DRINK, I remove the previous food items below it and add new items according to the selected category.
So, to me it seems that when I frequently add and remove views from the panel (GridLayout), the app gets slower and slower.
I simply do gridLayout.removeAllViews(); to remove all views.
Can anyone tell me how to resolve this please?
You are removing the views, but it don't necessary destroy then, so they continue occupying memory resources.
It's better to use some kind of list with an adapter, like ListView or RecyclerView, witch you can add or remove the views and they will only occupy memory when showing (or all that is showing more two).
You can do something like :
ArrayList<String> listItems = new ArrayList<>();
listaItems.add(yourString); // call that ever you have to add new itens
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(itemsAdapter);
to remove do something like :
listaItems.remove([INDEX]);
itemsAdapter.notifyDataSetChanged();
I am really desperate, please help me.
In my main Activity I want to remove all items from the listview then add other data. I am able to delete the data and refresh the listview, but when I add new data, something strange happens. I insert the data in my Array. I call notifydatasetchanged on my adapter after every insertion. The adapter gets those items (I have confirmed this in debug mode). But the listview will not refresh. And now comes the VERY STRANGE thing: When I tap the back button on my device, the application quits (this part is ok), but for a moment before quitting it refreshes the listview with the correct data.
Here are some parts of my code:
For deleting the data I use:
newsArrayList = new ArrayList<NewsClass>();
Adapter = new NewsAdapter(this, newsArrayList);
lv.setAdapter(Adapter);
"lv" is my listview. The listview correctly loses all items and refreshes. I have also tried this:
newsArrayList.clear();
Adapter.notifyDataSetChanged();
Also worked like expected. Then I use a "for" loop to add new items to my array, where I have the following lines:
newsArrayList.add(news[i]);
Adapter.notifyDataSetChanged();
As I wrote, when running in debug mode, everything seems fine. I have spent all day working on this problem. I have read several posts about similar issues on stackoverflow, I even watched a video on youtube. I am calling everything from the main UI thread.
I can work around this problem by restarting my activity, but I want to do it better than that. I have tried invalidate and such things, but nothing helped. Please note the VERY STRANGE thing, I wrote...
I think, that I have to force the activity to redraw its views, or something like that, but even refreshDrawableState(); on my listview did not help.
Thank you in advance!
Basically If you want to clear list data then just call
newsArrayList.clear();
lv.setAdapter(null);
Again add what ever your data in ArrayList and set adapter
newsArrayList = new ArrayList<NewsClass>();
Adapter = new NewsAdapter(this, newsArrayList); // considering list with some elements
lv.setAdapter(Adapter);
If you are getting outofmemory error/exception then there is problem with your Adapter Class inside getView method
I've searched far and wide for this answer and can't seem to find it.
I'm looking to populate a very simple 3 line listview, no more then 5-6 words per line at the most inside of my android app.
I'm currently using a base adapter and a string array to enable the actual text to show up on the screen.
I want to have the ability to update the information inside of my listview remotely using
some sort of means whether that's xml, SQLite, plain text, etc and then have that hosted file populate my listview.
Can anyone here help me to figure out how to do this? I'm still pretty new to android development so please go easy on me. Hopefully this question wont be too hard answer and also not too difficult to enable for a newbie like myself.
If the most you're going to ever have in there is just 3 lines of text, I think a SQLite DB may be a bit much for your situation. I'd look into using a Typed Array.
Here's a link to the Android Dev Guide on this subject:
http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray
Here's a code sample:
public class YourListActivity extends ListActivity {
String[] mTestArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter to contain your items
ArrayAdapter<String> adapter;
mTestArray = getResources().getStringArray(R.array.yourArray);
// Assign your array to an adapter with your layout file
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mTestArray);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
EDIT
Just realized that your data will be on a remote server, so this approach may not work for you, but it can still give you an idea of how to take your data once received from your remote server and place it into a ListView.