【发布时间】: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 场景非常陌生。
【问题讨论】: