I'm a newcomer to Android world.
I'm facing an performance problem in my app under development.
I've one activity with one AutoCompleteTextView. I'm initializing it on onCreate() of activity with an adapter like below:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, keywords);
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
textView.setAdapter(adapter);
The "keywords" is an String[] which I'm reading from json file containing a almost 3000 entries every time on onCreate(). At the same time, I get values against "keyword" and adding to a HashMap
Because of this, loading time of this activity is slowing down.
By what means I can reduce:
Reading json file only one time
Initialize adapter one time
So next time onward I can load the activity faster [First time I can show some "loading..." dialog]
You can try the following
Parse json in a background thread and put a loading bar.
Once strings are parsed and displayed, you save them via in an instance(onSavedInstanceState). As long as activity is not destroyed displaying them will be faster.
Save strings in sharedpreferences or SQlite DB(this option depends on app, like what strings are you displaying).
Related
I am currently creating an app that fetches 2 Strings randomly from an arraylist.
This is what I did:
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("someString");
// continue arrayList.add() for another thousand times for different strings.
// I am adding the Strings manually as these are specific questions in a quiz app
Random random = new Random();
myTextView.setText(arrayList.get(random.nextInt(arrayList.size());
myTextView2.setText(arrayList.get(random.nextInt(arrayList.size());
The Question:
At any point in time, I only need 2 Strings to be displayed to the user. But I am populating a thousand element arrayList, and fetching only 2 Strings from there. Is there a better way to do this? For example, only populating 2 Strings at runtime, without populating the unnecessary Strings?
Or maybe it does not matter at all?
Thank you very much in advance!
My AutoCompleteTextView is working fine. I am using the AutoCompleteTextView to add data to the database. However, I fail to see the recently added data getting displayed in the dropdown list of the AutoCompleteTextView. Every time I need to restart the activity to display the data that was recently added to the database. Are there ways to display the very recently added data without restarting the activity?
Update:
String[] autoCompleteSupplierName = vivzHelper.getSupplierName();
ArrayAdapter<String> supplierNameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteSupplierName);
supplierName.setThreshold(1);// starts working from first char
supplierName.setAdapter(supplierNameAdapter);
I am adding new item (OR sorting) in listview at the top using below code
items.add(0, new EntryItem(first, second));
and it works too, but sometimes when two things are triggered at same time, it won't sort particularly. Is there any way to sort listitem based on timestamp. I am using preferences hence for me to fetch timestamp is little difficult and do comparison based on it. Any other suitable way to sort listitems in listview based on timestamp ?
I have a listview called history that I call every time that I so something significant (ie open or close the app or record some info) The problem I have is that it records things from top to bottom meaning the most recent information is on the bottom of the listview. The second problem I have is that the listview gets deleted after the app is turned off. How do people usually save their history? (ie shared preferences can only store one variable and thats not enough, I would like to save a maximum of 30 entries to the listview)
Thanks
One option I use is to JSON encode my history list and store the resulting string to SharedPreferences. You can then order it as well (simply add items to the List that backs the ListView with new events first) and save it and restore it from the SharedPreferences file.
EDIT: Try something like this:
org.json.JSONArray tracking_users = new org.json.JSONArray();
tracking_users.put("history 1");
tracking_users.put("history 2");
tracking_users.put("history 3");
You could also consider using a database to store the Log of these events, tends to be more efficient.
See one of these for an introduction to android databases
I have been scouring the Internet for a way to use an input box at the top of the page, and have an add & remove button.
I want the EditText to write to the ListView, which in turn writes the list to a .txt-file.
I have tried breaking this down into multiple small sections of just simple things like
getting the input from the EditText to post to a TextView.
Organizing a ListView
writing to a file and
reading from a file.
If you have any advice about how to get the ListView to go from the EditText greatly helpful.
The best way to do this is to have an ArrayList<String> to store your Strings from the TextView, and in turn have that ArrayList<String> feed into the ListView. For example, for your Add button handler:
// Declare our variables
ArrayList<String> myStringList = new ArrayList<String>();
// OnClickListener stuff for Add button
..
myStringList.add(myEditText.getText().toString());
myListView.setListAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, myStringList));
..
// Close OnClickListener stuff
Make sure you refresh your list adapter afterwards to show the new values. Likewise if you're deleting, use myStringList.remove(index) to remove that string from the list and refresh the array adapter. As for writing to a text file, have a look at this thread.