我有一个arraylist,它从CSV文件获取输入,并由listview显示。我尝试做的是为该活动添加搜索功能。我已经看过其他一些教程,但我似乎无法让代码工作。
这是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 :(得分:0)
以下是您的代码......我认为这是不对的。你需要实例化arraylist
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;
}
我希望它有所帮助...