我有一个名为的方法,它从我的数据库getData
中填充一个限制为表中指定列的方法:listView
public ArrayList<String> getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_MODULE_CODE,
KEY_MODULE_NAME, KEY_LECTURE_PRACTICAL,
KEY_LECTURE_PRACTICAL_SHORT, KEY_LECTURE_DAY,
KEY_LECTURE_DAY_SHORT, KEY_START_TIME, KEY_END_TIME,
KEY_LOCATION, ADDITIONAL_INFO };
Cursor c = ourDatabase.query(DATABASE_MODULES, columns, null, null,
null, null, null);
ArrayList<String> results = new ArrayList<String>();
int indexModCode = c.getColumnIndex(KEY_MODULE_CODE);
int indexLectPracShort = c.getColumnIndex(KEY_LECTURE_PRACTICAL_SHORT);
int indexLectDayShort = c.getColumnIndex(KEY_LECTURE_DAY_SHORT);
int indexLectStart = c.getColumnIndex(KEY_START_TIME);
int indexLectLoc = c.getColumnIndex(KEY_LOCATION);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results.add(c.getString(indexModCode) + " "
+ c.getString(indexLectPracShort) + " "
+ c.getString(indexLectDayShort) + " "
+ c.getString(indexLectStart) + " "
+ c.getString(indexLectLoc));
}
return results;
}
这工作正常,这是我填充列表视图的方式(ModuleDatabaseHandler
是管理数据库的类):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_modules_test);
ModuleDatabaseHandler info = new ModuleDatabaseHandler(this);
info.open();
ArrayList<String> data = info.getData();
info.close();
l = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,data);
l.setAdapter(adapter);
l.setOnItemClickListener(this);
}
我希望能够单击该listView
项目,并查看与记录有关的所有信息(listView
其中仅显示某些信息)。我试图在ModuleDatabaseHandler
名为getAllData
.
以下是启动新活动OnItemClick
的ListView
项目,通过getAllData
using传递信息putExtra
:
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView temp = (TextView) view;
Toast.makeText(this, temp.getText() + " " + i, Toast.LENGTH_SHORT)
.show();
ModuleDatabaseHandler info = new ModuleDatabaseHandler(this);
info.open();
String module_info = info.getAllData(); // METHOD TO GET ALL DATA
info.close();
Intent fullModuleDetails = new Intent();
fullModuleDetails.setClassName("com.example.collegetimetable",
"com.example.collegetimetable.ModuleDetails");
fullModuleDetails.putExtra("list1", module_info);
startActivity(fullModuleDetails);
}
这是从数据库getAllData
中检索所有信息的方法。如何更改此方法,以仅listView
检索与用户单击的特定项目相关的行的信息?
public String getAllData() {
String[] columns = new String[]{ KEY_ROWID, KEY_MODULE_CODE, KEY_MODULE_NAME, KEY_LECTURE_PRACTICAL, KEY_LECTURE_DAY, KEY_START_TIME,KEY_END_TIME, KEY_LOCATION, ADDITIONAL_INFO};
Cursor c = ourDatabase.query(DATABASE_MODULES, columns, null, null, null, null, null);
String results = "";
int indexRow = c.getColumnIndex(KEY_ROWID);
int indexModCode = c.getColumnIndex(KEY_MODULE_CODE);
int indexModName = c.getColumnIndex(KEY_MODULE_NAME);
int indexLectPrac = c.getColumnIndex(KEY_LECTURE_PRACTICAL);
int indexLectDay = c.getColumnIndex(KEY_LECTURE_DAY);
int indexLectStart = c.getColumnIndex(KEY_START_TIME);
int indexLectEnd = c.getColumnIndex(KEY_END_TIME);
int indexLectLoc = c.getColumnIndex(KEY_LOCATION);
int indexLectInfo = c.getColumnIndex(ADDITIONAL_INFO);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
results = results + ( c.getString(indexModCode) + " " + c.getString(indexModName) + " " + c.getString(indexLectPrac)
+ " " + c.getString(indexLectDay) + " " + c.getString(indexLectStart) + " " + c.getString(indexLectEnd) + " "
+ c.getString(indexLectLoc) + " " + c.getString(indexLectInfo));
}
return results;
}
谢谢你的帮助