我在方法 setViewValue 和 setOnItemClickListener 中使用变量 i 时遇到问题。所以我需要检查表中行的值,然后将其添加到另一个类。但是对于这种检查,我需要在两种方法中都使用 i 或者以另一种方式进行。
你能帮我吗?
String[] from = new String[] { DB.COLUMN_MON, DB.COLUMN_YEAR };
int[] to = new int[] { R.id.textMonth, R.id.textYear };
scAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to);
scAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
for (int i=0; i < 98; i++){
if (cursor.getString(cursor.getColumnIndex(DB.COLUMN_MON)).equals(dataMonths[0]) && cursor.getString(cursor.getColumnIndex(DB.COLUMN_YEAR)).equals(dataYears[i])){
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Main_month.this, month.class);
intent.putExtra("year", dataYears[i]);
intent.putExtra("month", dataMonths[0]);
startActivity(intent);
finish();
}
});
}
}
return false;
}
});
list.setAdapter(scAdapter);
谢谢你的帮助!我这样解决我的问题:
scAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Main_month.this, month.class);
intent.putExtra("year", cursor.getString(cursor.getColumnIndex(DB.COLUMN_YEAR)));
intent.putExtra("month", cursor.getString(cursor.getColumnIndex(DB.COLUMN_MON)));
startActivity(intent);
finish();
}
});
return false;
}
});