【发布时间】:2014-01-14 14:49:39
【问题描述】:
单击我的 ListView 中的项目时,我想打开第二个活动并在 2 个不同的 TextView 中显示描述和原因。我不知道如何将所选项目中的数据传递到 Intent 以及如何在 TextView(第二个活动)中显示值。作为记录,我使用的是 SQLite(commonsware)。
这可能是将整个记录作为对象发送而不是询问记录的每个元素(描述、原因)的最佳方式?
提前致谢!!
我创建了一个“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" });
}
}
MainActivity:
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);
}
});
}
EDIT2:
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
【问题讨论】:
-
传递 id,然后使用 id 进行查询。
-
对你调用的意图的捆绑使用额外的:Intent(context,NewActivity.class).putExtra("action", value);然后在您的活动的 onCreate() 上获取它们: String action = intent.getStringExtra("action");
-
你有"ListView List=",使用"ListView list="
标签: android android-intent android-listview android-sqlite