2017-08-10 15 views
0

我有一個從CSV文件獲取輸入並由列表視圖顯示的數組列表。我想要做的是將搜索功能添加到該活動。我見過一些其他的教程,但我似乎無法讓代碼工作。我需要幫助爲我的CSV文件添加搜索功能

這是CSV ArrayList中的主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; 
     } 
    } 


} 

我發現代碼從另一個計算器的文章,但我只是不知道如何真正將其應用到我的特殊情況下,或者如果它甚至適用:

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場景。

回答

0

以下是您的代碼...我認爲這是不對的。您需要實例化數組列表

public List<String[]> read(){ 


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

} 

相反,試試這個...

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; 

} 

我希望它可以幫助...

+0

我不要」瞭解這將如何幫助我創建可搜索的活動?顯示列表的代碼工作正常,我只是不知道如何啓用搜索功能。 –