简体   繁体   中英

I need help adding search functionality for my CSV file

I have an arraylist that gets inputs from a CSV file and is displayed by a listview. What I'm trying to do is add search functionality to that activity. I've seen a couple of other tutorials but i can't seem to get the code to work.

This is the main java class for the CSV arraylist:

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;
        }
    }


}

I found code from another stackoverflow post but i just don't know how to really apply it to my specific case or if it even applies:

 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);

Any solution would be greatly appreciated, let me know if I left anything out that you may need to help me, I'm pretty fresh to java scene.

the following is your code... I think it is not right. You need to instantiate the arraylist

public List<String[]> read(){


List<String[]> ArrayList = new ArrayList<String[]>();

}

instead, try this...

public ArrayList<String> read(){

ArrayList<String> objName = new ArrayList<String>();

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

return objName;

}

I hope it helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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