I want to add AutoCompleteTextView in my application. I have one txt file in that there are more than 2000 records are present. I want to use it for AutoCompleteTextView. Normally for small data we use array as:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,dataArray);
AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
actv.setThreshold(1);
actv.setAdapter(adapter);
But now how to use txt file for AutoCompleteTextView. Any suggestion will be appreciated.
How are you separating elements in your text file? Assuming that you have a new elements on each line you can use this to convert the file to an array, the use the array adapter as you have mentioned above.
String[] arr= null;
List<String> items= new ArrayList<String>();
try
{
FileInputStream fstream = new FileInputStream("text1.txt");
DataInputStream data_input = new DataInputStream(fstream);
BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input));
String str_line;
while ((str_line = buffer.readLine()) != null)
{
str_line = str_line.trim();
if ((str_line.length()!=0))
{
items.add(str_line);
}
}
arr = (String[])items.toArray(new String[items.size()]);
}
create one string resource file like "string_autocompletearray" and define your array.
**res/values/string_autocompletearray.xml**
string-array name="autocomplete_array">
<item>AutoCompleteText 1 </item>
<item>AutoCompleteText 2 </item>
-------------------------------
-------------------------------
<item>AutoCompleteText N </item>
</string-array>
**Now find this array and set to adapter**
ArrayList<String> list = new ArrayList<String> (Arrays.asList(getResources().getStringArray(R.array.autocomplete_array)));
Related
I am rather new to JSON at the moment, but I need to convert a JSON response that contains the same key, but different values to an ArrayList to use it with my spinner.
I tried it like here: Converting JSONarray to ArrayList
But i get the whole json string, but just need the value part.
I can't figure out how to do this and found no answer that worked for me :/
What I want would be a List like:
City1
City2
City3
But i have in my spinner:
{"city":"name1"}
{"city":"name2"}
{"city":"name3"}
Code I have is:
JSONArray obj = new JSONArray(response);
Spinner availableCitySpin;
availableCitySpin = (Spinner) findViewById(R.id.avCitySp);
List<String> cityValues = new ArrayList<String>();
if (jarr != null) {
for (int i=0;i< jarr.length();i++){
cityValues.add(jarr.getString(i).toString());
}
}
ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cityValues);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
availableCitySpin.setAdapter(cityAdapter);
availableCitySpin.setSelection(0);
Change your code to something like this:
...
for (int i=0;i< jarr.length();i++){
JSONObject cityObject = jarr.getJSONObject(i);
cityValues.add(cityObject.getString("city"));
}
...
Try this:
First use split
For example: String[] result = splits[0].split(":");
you will get two item in array result. result[0]= {"city" and result[1] = "name1"}
If you want to get the key use result[0]
remove sign from result[0] using replace. Example: data = result[0].replace("\"","").replace("{","");
use it in loop, should work
I need some help. Im quite new to android and java.
Right now i have a csv code reader. The code will read the csv and then puts it into a Spinner. the csv file i have is example like this
1,"Easy"
2,"Medium"
3,"Hard"
4, "Very Hard
My Code
try{
InputStreamReader csvStreamReader = new InputStreamReader(MainActivity.this.getAssets().open("Category.csv"));
CSVReader reader = new CSVReader(csvStreamReader);
Log.d("test", "reading csv");
String [] nextLine;
List<String> list = new ArrayList<String>();
while ((nextLine = reader.readNext()) != null) {
list.add(nextLine[0] + "," + nextLine[1]);} spinner1 =(Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner1.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
The code works well. It does appear in the spinner.
Now i want to go a little further, i want to create a for loop the check the array for a particular text example "Medium". When the word is found, it will be stored into another array. I really cant figure out the code. Try searching for ideas in this forum but fail. Can some one give me some pointers, preferred a sample code.
I'm using string array which I'm passing to ArrayAdapter for spinner item.
my array size(usually less than 10) and values are variable (I'm taking it from asset file)
if I'm using this :
String[] arr = new String[10];
protected void onCreate(Bundle savedInstanceState) {
breader = new BufferedReader(new InputStreamReader(getAssets().open(path)));
int length = Integer.parseInt(breader.readLine());
for(int i=0; i<length; i++){
arr[i]=breader.readLine();
initialize();
....
}
initialize(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> array = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, arr);
spinner1.setAdapter(array);
}
it shows NullPointerException at spinner1.setAdapter(array); line.
can I reassign array length. I think its not possible.
because of you add null when no data in last portion of array.
I mean there are 7 data in assest file and you move for loop 10 times so after 7 data Add there are null represent. so breader.readLine(); read null and add it to your string array and when you parse StringArray to Array Adapter there are null get on 8th position of array and nullpointer Exception fire.
So just check breader.readLine() != Null then add it to String of Array otherwise add some Temp text.
Or
You Also Use ArrayList instead of String[] Array.
ArrayList is dynamically Arraylist you can add data to list useing array.add(yourdata).
Thats it...
I am using data store in Assets folder like "reader.txt" this data get in using "InputStream" geting string value like this set to String value in textview. My question is how to store String in listview?
Here is my code:
try {
InputStream is = getAssets().open("reader.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer);
TextView tv = (TextView) findViewById(R.id.list);
tv.setText(text);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
There are two ways by which you can store strings in listView. Either you can use android:entries which takes an array or object resource as an input, or u can use ArrayAdapter to set entries in listView.I will show you an example.
String[] values={""}//whatever values you have shown in the TextView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
You can read text from file, then store it into String[] and then use ListAdapter for example ArrayAdapter<String> with ListView.
ListView list = (ListView) findViewById(R.id.someId);
your work with file ...
list.setAdapter(new ArrayAdapter<String>(Context, Layout, data));
// data is String[]
Here is similar example:
ArrayAdapter sample program in
Android
Difference with yours is that your String[] will be dynamically generated from file. So at first you will prepare data from file, store them into String[] and set them into Adapter.
Is there anyway I can save a List variable to the Androids phone internal or external memory? I know I can save primitive data, yet not sure about this one.
Thanks in advance.
Yes exactly you can only save primitives so you could something like this:
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
StringBuilder csvList = new StringBuilder();
for(String s : list){
csvList.append(s);
csvList.append(",");
}
sharedPreferencesEditor.put("myList", csvList.toString());
Then to create your list again you would:
String csvList = sharedPreferences.getString("myList");
String[] items = csvList.split(",");
List<String> list = new ArrayList<String>();
for(int i=0; i < items.length; i++){
list.add(items[i]);
}
You could encapsulate this "serializing" into your own class wrapping a list to keep it nice and tidy. Also you could look at converting your list to JSON.
I use ObjectOutputStream to save lists to storage.
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(your_file));
oos.writeObject(your_list);
oos.flush();
oos.close();
That should do it. It works for me
If you want to store/retrieve/delete some string value in your program, then List is better than Array. Here below I will show you that how to use it.
// Declaring List Variable
List<String> Mylist = new ArrayList<String>();
// Adding item to List
Mylist.add(list.size(), "MyStringValue"); //this will add string at the next index
// Removing element form list
Mylist.remove(list.size()-1); //this will remove the top most element from List