0

我有一个名为的方法,它从我的数据库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.

以下是启动新活动OnItemClickListView项目,通过getAllDatausing传递信息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;
}

谢谢你的帮助

4

1 回答 1

1

与其循环遍历数据,不如执行以下操作:

public String getAllDataForRow(int row) {

    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);

    if(c.moveToPosition(row)) {
        String result = "";
        for(int columnIndex = 0; columnIndex<c.getColumnCount();columnIndex++) {
            result += c.getString(columnIndex)+" ";
        }
        return result;
    } else {
        throw new IllegalArgumentException("Row " + row + " does not exist");
    }
}

另外,如果您不知道:如果您想要所有列,您实际上不必在投影中指定它们,而可以只传递 null 而不是列。

希望有帮助。

于 2013-11-01T15:22:28.037 回答