当我点击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
答案 0 :(得分:2)
我在我的应用程序中使用以下内容:
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);
}
});
在Detail类中:
Bundle b = new Bundle();
b = getIntent().getExtras();
long id = b.getLong("id");
答案 1 :(得分:0)
首先,你只是做了那个改变
列出到mList
然后更改onItemClick的代码
mList.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",adapter.getItem(position));
startActivity(intent);
}
});
答案 2 :(得分:0)
设置参数
Intent intent = new Intent(MainActivity.this, <Name-of-the-Activity>.class);
intent.putExtra("id",id);
startActivity(intent);
获取参数(快捷方式)
String id = getIntent().getExtras().getString("id");