将row_id从一个Activity传递给另一个Activity,这样我就可以根据row_id删除活动中的行

时间:2013-11-01 17:42:32

标签: android android-listview android-sqlite android-cursor

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

简短摘要:

  1. 我的数据库填充了listview
  2. 用户可以单击列表视图和 查看有关新活动的更多详细信息
  3. 我想删除 来自这项新活动的记录
  4. CODE

    我有一个名为ModuleDatabaseHandler的类来管理数据库。在此,我有一个名为getData()的方法,将记录填充到ListView中的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

    读入

    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的{​​{1}}类中有另一种方法,允许用户按下getAllData()项并阅读有关新活动的项目的完整详细信息{{1} }:

    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转移到ActivityViewAll

    ActivityFullDetails

    回到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_ID中的listView,然后传递到ModuleDatabaseHandler,以便删除记录?

    解决

    感谢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页面,并使用{{1像往常一样)..然后删除方法可以使用ActivityFullDetails

1 个答案:

答案 0 :(得分: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);

}