1

我正在尝试从我的数据库中删除一条记录,

简短的摘要:

  1. 我的数据库填充了一个列表视图
  2. 用户可以单击列表视图并查看有关新活动的更多详细信息
  3. 我希望能够从这个新活动中删除记录

代码

我有一个名为ModuleDatabaseHandler管理数据库的类。在此,我有一个名为的方法getData()将记录填充到ListViewin ActivityViewAll

ModuleDatabaseHandler

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

此数据由以下人员读取ActivityViewAll

活动查看全部

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

}

我在ModuleDatabaseHandler类中有另一种方法,getAllData()它允许用户按下一个ListView项目并阅读有关新活动中该项目的完整详细信息ActivityFullDetails

    public String getAllData(int specified_position) {
        // TODO Auto-generated method stub
        int position = specified_position;

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

}

数据从 listView 传输ActivityViewAllActivityFullDetailsusing onItemClick

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(i);
    info.close();

    Intent fullModuleDetails = new Intent();
    fullModuleDetails.setClassName("com.example.collegetimetable",
            "com.example.collegetimetable.ModuleDetails");

    fullModuleDetails.putExtra("list1", module_info);

    startActivity(fullModuleDetails);

}

回到ModuleDatabaseHandler课堂上,我创建了一个方法deleteEntry来尝试删除一行:

public void deleteEntry(long row){
    ourDatabase.delete(DATABASE_MODULES,  KEY_ROWID + "=" + row, null);

}

问题

如何ROW_IDModuleDatabaseHandler类中的 传递给 listView ,ActivityViewAll然后再传递给 ,ActivityFullDetails以便删除记录?

解决了

感谢Serge谁帮助我解决了这个问题,方法是创建一个getRowId在接受的答案中看到的方法。尽管我确实必须在已接受的答案中未显示的方法中添加游标查询,即:

    public int getRowID(int specified_position) {
    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);
// rest of method as shown below
}

此方法在ActivityViewAll活动页面中从ModuleDatabaseHandler类的实例中调用,使用 , 传递给ActivityFullDetails页面putExtra(并getIntent().getExtras像往常一样使用接收).. 然后删除方法可以使用rowid.

4

1 回答 1

1

添加功能

public int getRowID(int specified_position) {
    int position = specified_position;
    if(c.moveToPosition(specified_position)) {
        return c.getInteger(c.getColumnIndex(KEY_ROWID));
    } else {
        throw new IllegalArgumentException("Row " + specified_position + " does not exist");
    }

}

然后让它使用

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(i);
int rowID = info.getRowID(i);
info.close();

Intent fullModuleDetails = new Intent();
fullModuleDetails.setClassName("com.example.collegetimetable",
        "com.example.collegetimetable.ModuleDetails");

fullModuleDetails.putExtra("list1", module_info);
fullModuleDetails.putExtra("rowid", rowID);

startActivity(fullModuleDetails);

}
于 2013-11-01T18:14:58.657 回答