android - 将所选项目 ListView 的值传递给下一个 Activity

标签 android android-intent android-listview android-sqlite

当单击我的 ListView 中的一个项目时,我想打开第二个 Activity 并在 2 个不同的 TextView 中显示描述和原因。我不知道如何将所选项目的数据传递到 Intent 以及如何在 TextView 的(第二个 Activity )中显示值。作为记录,我使用的是 SQLite(通用软件)。

这可能是将整个记录作为对象发送而不是询问记录的每个元素(描述、原因)的最佳方式?

提前致谢!

我创建了一个类“OpenHelper”:

public class OpenHelper extends SQLiteOpenHelper {

public static final String NAME = "oef.db";
public static final int VERSION = 1; 

public OpenHelper(Context context) {
    super(context, NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    onUpgrade(db, 1, VERSION);  
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sqlCreateTodo = "CREATE TABLE TODOS (_id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT, reason TEXT)";
    db.execSQL(sqlCreateTodo);

    String sqlInsertTodo = "INSERT INTO TODOS (description, reason) VALUES(?,?)";
    db.execSQL(sqlInsertTodo, new Object[] { "Buy Christmas presents.", "Family" });
    db.execSQL(sqlInsertTodo, new Object[] { "Buy turkey.", "Family" });
    db.execSQL(sqlInsertTodo, new Object[] { "Buy beer.", "Me" });
    db.execSQL(sqlInsertTodo, new Object[] { "Buy more beer.", "Me" });
    db.execSQL(sqlInsertTodo, new Object[] { "Buy tree.", "Family" });  
}
}

主要 Activity :

public class MainActivity extends ListActivity implements LoaderCallbacks<Cursor>{

private static final int TEST_LOADER = 0;
private SimpleCursorAdapter adapter;

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Intent intent = new Intent(this, Detail.class);
    intent.putExtra("test", "test");
    startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] from = new String[] { "description" };
    int[] to = new int[] { android.R.id.text1 };
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to, 0);
    setListAdapter(adapter);

    getLoaderManager().initLoader(TEST_LOADER, null, this); 
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    if (id == TEST_LOADER){
    SQLiteCursorLoader cursorLoader = new SQLiteCursorLoader(this, new OpenHelper(this), "SELECT _id, description FROM TODOS", null);   
        return cursorLoader;
    }
    return null;
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    adapter.swapCursor(arg1);
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    adapter.swapCursor(null);
}
}

编辑:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] from = new String[] { "description" };
    int[] to = new int[] { android.R.id.text1 };
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to, 0);
    setListAdapter(adapter);

    getLoaderManager().initLoader(TEST_LOADER, null, this); 

    ListView List= (ListView) findViewById(R.id.list);  
    List.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapter, View view, int position, long id){
            Intent intent = new Intent(MainActivity.this, Detail.class);
            intent.putExtra("id",id);
            startActivity(intent);
        }
    });

}

编辑2:

01-14 11:12:24.418: E/AndroidRuntime(2319): FATAL EXCEPTION: main
01-14 11:12:24.418: E/AndroidRuntime(2319): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.howest.mad.examen.timtest/be.howest.mad.examen.timtest.MainActivity}: java.lang.NullPointerException
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.os.Looper.loop(Looper.java:137)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at java.lang.reflect.Method.invoke(Method.java:525)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at dalvik.system.NativeStart.main(Native Method)
01-14 11:12:24.418: E/AndroidRuntime(2319): Caused by: java.lang.NullPointerException
01-14 11:12:24.418: E/AndroidRuntime(2319):     at be.howest.mad.examen.timtest.MainActivity.onCreate(MainActivity.java:48)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.app.Activity.performCreate(Activity.java:5133)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-14 11:12:24.418: E/AndroidRuntime(2319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-14 11:12:24.418: E/AndroidRuntime(2319):     ... 11 more

最佳答案

我在我的应用程序中使用以下内容:

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id){
        Intent intent = new Intent(MainActivity.this, Detail.class);
        intent.putExtra("id",id);
        startActivity(intent);
    }
});

在细节类中:

Bundle b = new Bundle();
b = getIntent().getExtras();
long id = b.getLong("id");

关于android - 将所选项目 ListView 的值传递给下一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116475/

相关文章:

安卓 N : How to launch activity in current active window instead of second window when click on notification in split-screen?

Android 如何将标题与 ListView 项目对齐

android - SQLite。如何部分填充 ListView?

android - 相机和图库 Intent 获取图像并复制到android中的另一个文件夹中

Android:无法保存和读取 Bundle 中的所有值

java - Android中的SSL session 重用问题(J2SE正常工作)

android - SDK 管理器不显示示例包选项

android - 突出显示 "ListFragment"中的选定项目?

java - 无法将数据插入 fragment 内的数据库

java - 操作栏V7 : Why it doesn't work?