1

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.

2 Answers 2

2

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()]);
    }
2
 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)));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.