2017-08-10 7 views
0

私はCSVファイルから入力を取得し、リストビューで表示されるarraylistを持っています。私がしようとしているのは、そのアクティビティに検索機能を追加することです。私は他のチュートリアルをいくつか見たことがありますが、コードを動作させることができません。私は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; 
     } 
    } 


} 

私は別の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のシーンにはかなり新鮮です。

答えて

0

あなたのコードは次のとおりです。私はそれが正しくないと思います。あなたは

public List<String[]> read(){ 


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

} 

代わりのArrayListにインスタンス化する必要があり、「私はドンだ

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

...これを試してみてください検索可能なアクティビティをどのように作成するのに役立ちますか?リストを表示するコードは正常に動作しますが、検索機能を有効にする方法はわかりません。 –

関連する問題