简体   繁体   English

自动完成编辑文本-我可以从CSV文件读取吗?

[英]Autocomplete Edittext - Can i read from CSV file?

I have a list of ItemCodes that are saved in a CSV file in my raw folder in my android application. 我在Android应用程序的原始文件夹中的CSV文件中保存了一个ItemCodes列表。

In order to eradicate human error, I would like it so that when the user starts typing the edittext will autocomplete based on the itemcodes in the CSV file. 为了消除人为错误,我希望这样做,以便用户开始键入时,edittext将根据CSV文件中的项目代码自动完成。

Does anyone know if this is possible? 有人知道这是否可能吗?

Here is what I have so far: 这是我到目前为止的内容:

 private void ReadItemCodes() {
    InputStream IS = getResources().openRawResource(R.raw.itemcodes);
    BufferedReader reader = new BufferedReader(new InputStreamReader(IS, Charset.forName("UTF-8")));

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            //Split by commas
            String[] tokens = line.split(",");

            //Read the data
            ItemCodes+=line;  
      }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

If you are just looking for the Suggestion for the AutoComplete Edittext then you can use the string-array , it is easier and if you are having some complex Data Structure then CSV is also good. 如果您只是在寻找有关AutoComplete Edittext的建议,则可以使用string-array ,它会更容易;如果您具有一些复杂的数据结构,那么CSV也是不错的选择。

if you are looking to do it by using CSV then please follow the needful steps to read the CSV file. 如果您希望通过CSV来做到这一点,请按照必要的步骤阅读CSV文件。

Here you can refer this post https://stackoverflow.com/a/38415815/5343866 在这里您可以参考这篇文章https://stackoverflow.com/a/38415815/5343866

For anyone who comes across this in the future. 对于将来遇到此问题的任何人。 This was the best and simplest approach that I found! 这是我发现的最好,最简单的方法!

 // Get a reference to the AutoCompleteTextView in the layout
    itemcodetextview = (AutoCompleteTextView) findViewById(R.id.itemcode);
    batchnumbertextview = (AutoCompleteTextView) findViewById(R.id.batchnumber);

    //READ FROM BATCH NUMBER CSV
    Scanner scanner = new Scanner(getResources().openRawResource(R.raw.batchnumbers));
    List<String> listbatchnumbers = new ArrayList<String>();
    while (scanner.hasNext()) {
        listbatchnumbers.add(scanner.next());
    }
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listbatchnumbers );
    batchnumbertextview.setAdapter(adapter);
    scanner.close();

    //READ FROM ITEM CODES CSV
    Scanner scanner2 = new Scanner(getResources().openRawResource(R.raw.itemcodesofficial));
    List<String> listitemcodes = new ArrayList<String>();
    while (scanner2.hasNext()) {
        listitemcodes.add(scanner2.next());
    }
    ArrayAdapter<String> adapter2 =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listitemcodes );
    itemcodetextview.setAdapter(adapter2);
    scanner2.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM