【问题标题】:I need help adding search functionality for my CSV file我需要帮助为我的 CSV 文件添加搜索功能
【发布时间】:2017-08-10 15:39:24
【问题描述】:

我有一个从 CSV 文件获取输入并由列表视图显示的数组列表。我想要做的是向该活动添加搜索功能。我看过其他几个教程,但我似乎无法让代码工作。

这是 CSV 数组列表的主要 java 类:

public class games extends Activity{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_games);

        InputStream inputStream = getResources().openRawResource(R.raw."filename");
        CSVFile csvFile = new CSVFile(inputStream);
        List<String[]> slots = csvFile.read();
        MyListAdapter adapter = new MyListAdapter(this, R.layout.list_item, R.id.text_view, filename);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);

    }

    private class CSVFile {
        InputStream inputStream;

        public CSVFile(InputStream inputStream){
            this.inputStream = inputStream;
        }

        public List<String[]> read(){
            //
            List<String[]> ArrayList = new ArrayList<String[]>();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] row = line.split(",");
                    ArrayList.add(row);
                }
            }
            catch (IOException e) {
                Log.e("Main",e.getMessage());
            }
            finally {
                try {
                    inputStream.close();
                }
                catch (IOException e) {
                    Log.e("Main",e.getMessage());
                }
            }
            return ArrayList;
        }
    }


}

我从另一个 stackoverflow 帖子中找到了代码,但我只是不知道如何将它真正应用到我的具体案例中,或者它是否适用:

 ArrayList<String> storedContent = new ArrayList<String>();
    ArrayList<String> contentArray = new ArrayList<String>();
    String searchText = "sometest";
    for(String each : storedContent){
        if (each.contains(searchText)){
            contentArray.add(each);
        }
    }

    ListView displaySearchResult = (ListView)findViewById(R.id.list_id);
    myListAdapter adapter = new myListAdapter(contentArray, this) ;
    displaySearchResult.setAdapter(myListAdapter);

任何解决方案将不胜感激,如果我遗漏了您可能需要帮助我的任何内容,请告诉我,我对 Java 场景非常陌生。

【问题讨论】:

    标签: java android csv


    【解决方案1】:

    以下是您的代码...我认为这是不对的。你需要实例化arraylist

    public List<String[]> read(){
    
    
    List<String[]> ArrayList = new ArrayList<String[]>();
    
    }
    

    相反,试试这个...

    public ArrayList<String> read(){
    

    ArrayList&lt;String&gt; objName = new ArrayList&lt;String&gt;();

    //create an String type arraylist "objName". you store all the string data from csv file into this "objName"
    
    return objName;
    
    }
    

    希望对你有帮助……

    【讨论】:

    • 我不明白这将如何帮助我创建可搜索的活动?显示列表的代码工作正常,我只是不知道如何启用搜索功能。
    猜你喜欢
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    相关资源
    最近更新 更多