我刚接触到android。我非常熟悉客观c。 如你所见,我有一个从csv文件加载的arraylist。 我无法弄清楚为什么我无法读取我的" public void buttonClicked(View v)"对于这一行" for(String str:list)..."
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchButton = (Button) findViewById(R.id.findButton);
//searchButton.setOnClickListener(this);
mainSearchView = (EditText) findViewById(R.id.searchText);
spinner1 = (Spinner) findViewById(R.id.titleCategory1);
List<String> list = new ArrayList<String>();
try{
InputStreamReader csvStreamReader = new InputStreamReader(MainActivity.this.getAssets().open("Category.csv"));
CSVReader reader = new CSVReader(csvStreamReader);
Log.d("test", "reading csv");
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
list.add(nextLine[0] + nextLine[1]+ nextLine[2]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void buttonClicked(View v){
Log.d("test", "button clicked");
strFind2 = mainSearchView.getText().toString().toLowerCase();
/*
for (String str : list) {
if (str.contains(strFind2)) {
Log.d("test", "contains easy");
} else {
Log.d("test", "dont contains easy");
}*/
}
}
有人可以指导我吗?
答案 0 :(得分:1)
构建代码,我猜你正在访问同一个Activity中的列表。
所以请声明,
List<String> list;
作为班级成员字段,
并访问buttonClicked(View v)
private List<String> list
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchButton = (Button) findViewById(R.id.findButton);
//searchButton.setOnClickListener(this);
mainSearchView = (EditText) findViewById(R.id.searchText);
spinner1 = (Spinner) findViewById(R.id.titleCategory1);
list = new ArrayList<String>();
另外,我建议您阅读有关变量和方法的类和范围的基本Java编程教程。