我想使用意图将Listview单击的项目ID传递给其他活动。我该怎么做?
答案 0 :(得分:0)
您获得ID,并使用相应的(长号)Intent(带有合适的键(第一个参数))将ID作为附加项添加到putExtra中。
在其他活动中,您将获得Intent,然后使用getLongExtra使用相同的键(第一个参数)从Extra中获取值,请注意默认值应为将不是ID。
以下是一个有效的示例:-
public class DBOpenHelper extends SQLiteOpenHelper {
public static final String DBNAME = "notes.db";
public static final int DBVERSION = 1;
public static final String TBL_NOTE = "note_table";
public static final String COL_NOTE_ID = BaseColumns._ID;
public static final String COL_NOTE_TEXT = "note_text";
private final String create_table = "CREATE TABLE IF NOT EXISTS " + TBL_NOTE + "(" +
COL_NOTE_ID + " INTEGER PRIMARY KEY, " +
COL_NOTE_TEXT + " TEXT " +
")";
private static final String drop_table = "DROP TABLE IF EXISTS " + TBL_NOTE;
public DBOpenHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(create_table);
}
public void onUpgrade(SQLiteDatabase db, int version_old, int version_new) { }
public long addNote(String note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("NOTE_TEXT",note);
return db.insert("note_table",null,cv);
}
public Cursor getAllNotes() {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(TBL_NOTE,null,null,null,null,null,null);
}
}
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private SimpleCursorAdapter MSCA;
private Cursor mCsr;
private DBOpenHelper mDBHelper;
public static final String INTENTEXTRAKEY_NOTE_ID = "iek_noteid";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHelper = new DBOpenHelper(this);
mListView = findViewById(R.id.listView);
addSomeTestData(); //Adds a row every time the App is run (for testing)
ViewData();
}
private void ViewData(){
mCsr = mDBHelper.getAllNotes();
if (MSCA == null) {
MSCA = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
mCsr,
new String[]{DBOpenHelper.COL_NOTE_TEXT},
new int[]{android.R.id.text1},
0);
mListView.setAdapter(MSCA);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(MainActivity.this,OtherActivity.class);
i.putExtra(INTENTEXTRAKEY_NOTE_ID,id);
startActivity(i);
}
});
} else {
MSCA.swapCursor(mCsr);
}
}
private void addSomeTestData() {
if(DatabaseUtils.queryNumEntries(mDBHelper.getWritableDatabase(),DBOpenHelper.TBL_NOTE) < 1) {
mDBHelper.addNote("My Note");
mDBHelper.addNote("Another Note");
mDBHelper.addNote("Yet Another Note");
mDBHelper.addNote("And so on.");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mCsr.close();
}
@Override
public void onResume()
{
super.onResume();
ViewData();
}
}
此活动:-
注释
在这种情况下,CursorAdpater(SimpleCursorAdapter)非常适合,因为侦听器会将 id 传递给 onItemClick 方法。 注意,这需要一列,专门命名为 _id (根据 BaseColumns._ID ),并且该列是 rowid的别名列。
onDestroy 方法已被覆盖,以在完成处理后(即,在活动被分散后)关闭游标。这在初始活动中并不那么重要,因为它的生命周期通常对于整个应用程序而言(在级别较低的“活动”中关闭游标更为重要)。
ViewData方法管理适配器的单个实例(即仅在尚未实例化时创建它),并刷新ListView。因此,为什么要在覆盖的 onResume 方法中调用它。即可能会添加其他活动。删除或更新数据库,以便ListView在返回时进行更新。
public class OtherActivity extends AppCompatActivity {
TextView mNoteId;
Button mDone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
mNoteId = this.findViewById(R.id.noteid);
mDone = this.findViewById(R.id.done);
mDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
Intent i = this.getIntent();
long id = i.getLongExtra(MainActivity.INTENTEXTRAKEY_NOTE_ID,-1);
mNoteId.setText(String.valueOf(id));
}
}