【发布时间】:2013-10-31 00:15:36
【问题描述】:
我在方法 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;
}
});
【问题讨论】:
-
您有一些主要的格式问题,导致您的问题难以阅读。
-
我在 setViewValue 中声明了一个变量 i。我在那里使用它。如何在方法 onItemClick 中使用此变量?