AutoCompleteTextView using more than one resource string-array - android

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.

Related

Android: App response gets slow after several operations

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();

Update String Array with Subsequent Update of ListView in Android

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);

Out Of Memory Android while loading my pub vars

Block Array or multidimensional array Variables. For some reason my app is throwing out of memory on my loading of my pub vars class. This started happening when I started using Block Arrays. It works on actual devices but not on the emulators. Any thoughts? Should I set my block arrays to actual needs?
Current Block Arrays:
public static String[][] Name = new String[1000][1000];
however I only use maybe 10 or so. Is the device allocating space for the potential of the 1000 and is that why it is errors out? If so, how can I use these as the need may grow and I do not want to put a small limit on it. Thanks in advance for any thoughts.
With
public static String[][] Name = new String[1000][1000];
you are allocating 1 million strings (1000x1000) which is quite a bunch. If the information on this page is correct each string at least takes 40 bytes, so that would be around 39 Mbytes in your case and this can easily be too much memory on the heap for 1 activity. I would start there to refactor if you are only using 10. There is probably a better solution than your approach but without any more details on your code it's hard to give them. But of the top of my head, why not use a Set<String> or List<String> ?
Edit: So it's seems to me that you just want a Collection that scales dynamically. For that array is not the best choice. There are many of datatypes for that but one simple example whould be an ArrayList which also uses a array as backing datatype but by default will be instanciated with a capacity of 10 and expands dynamically if you continue to add elements
List<String> stringList = new ArrayList<String>();
stringList.add("string1");
stringList.add("string2");
...
If you want each element to have its own list of strings just create an object for that:
public class CompoundString {
private String key;
private List<String> stringList;
...
}
and use it like this
List<CompoundString> compoundStringList = new ArrayList<CompoundString>();
compoundStringList.add(new CompoundString("string1", new ArrayList<String>());
or just use a map:
Map<String,List<String>> stringMap = new HashMap<String,List<String>>();
stringMap.put("string1", new ArrayList<String>());
This is pretty basic concept in most programming languages and I would start to read some docs about the various collections:
http://docs.oracle.com/javase/7/docs/api/java/util/List.html
http://docs.oracle.com/javase/7/docs/api/java/util/Set.html
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
http://www.mkyong.com/java/what-is-the-different-between-set-and-list/

Flex ActionScript - ArrayList Issues

I am banging my head for the last couple days in order to get this done but Im unable to. Someone please help me out!
Let me not tell u the whole thing and will try to explain it simply n clearly.
Im having 1 ArrayList. I am trying to replicate that into another one and trying to delete an item at a particular index. But this not only deletes the item in the replicated ArrayList but also the original ArrayList.
For ex:
var DuplicateList:ArrayList = new ArrayList();
DuplicateList = OriginalList;
DuplicateList.removeItemAt(2);
The above not only deletes the "Item 3" at Index-2 in the DuplicateList but also in the OriginalList.
I just need some workaround with this approach as this is the only way by which whatever I typed inside the controls present in an ItemRenderer of a FLEX List control that uses the OriginalList as a dataProvider is RETAINED, when I change the dataProvider of the List Control from OriginalList to DuplicateList. The following approach does not retain all the data.
var DuplicateList:ArrayList = new ArrayList();
DuplicateList.addAll(OriginalList);
DuplicateList.removeItemAt(2);
ListCntrl.dataProvider = DuplicateList;
Thanks for your help in advance...
A very, very important thing to understand:
ActionScript3 uses references to objects. Because of that, the two variables in this line of code refer to the exact same instance of an ArrayList:
DuplicateList = OriginalList;
So, when you remove an item from one reference, it is gone from the next. If you want two separate instances of ArrayList, then you need to clone it like you are suggesting later in your code.
So far, so good... but why is your ListCntrl retaining the data from the OriginalList? That doesn't make any sense at all. If you remove an item from DuplicateList and then use it as the data provider, then that item shouldn't be there. I think there is more to this story...

I need help populating a listview from a remote source in android

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.

Categories

Resources