Search code examples
androidandroid-listviewautocompletetextview

show AutoCompleteTextView using txt file data in android


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.


Solution

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