我有一個列表視圖從SqLite Db中獲取數據。我需要在列表視圖中獲取正在單擊的數據,並將其顯示在另一個列表視圖中的活動中。將數據從ListView傳遞到另一個Listview
我嘗試過使用共享偏好設置,但我一次只能獲取一個數據,並將其作爲textview顯示在另一個活動中。
我不知道如何全部顯示在列表視圖中選擇的數據或從選定的數據創建列表視圖,需要幫助。
我的搜索活動
listContent1.setAdapter(cursorAdapter);
listContent1.setOnItemClickListener(listContentOnItemClickListener);
private ListView.OnItemClickListener listContentOnItemClickListener = new ListView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_FOODNAME));
String item_content2 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CALORIES));
arlene = arlene + Integer.parseInt(item_content2);
String item = String.valueOf(" Food Name: " + item_content1) + "\n" +
" Calories: " + item_content2;
Toast.makeText(Search.this, item, Toast.LENGTH_LONG).show();
food.setText(item_content1);
calories.setText(String.valueOf(arlene));
Intent myIntent = new Intent(Search.this, Foodlog.class);
Bundle bundle = new Bundle();
bundle.putString("SQLITEDATA1", food.getText().toString());
bundle.putString("SQLITEDATA2", calories.getText().toString());
myIntent.putExtras(bundle);
startActivity(myIntent);
}
這裏是我的foodlog活動
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.foodlog);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
foodnum = (TextView)findViewById(R.id.foodNum);
remaining = (TextView)findViewById(R.id.remainingNum);
totalnum = (TextView)findViewById(R.id.totalNum);
foodlog = (ListView)findViewById(R.id.list);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
String food = bundle.getString("SQLITEDATA1");
String calories = bundle.getString("SQLITEDATA2");
String[] values = new String[] { food, calories }; //That you got from your intent bundle
LVAdapter adapter = new LVAdapter(this, R.layout.foodlist, values);
setListAdapter(adapter);
}
希望你能幫助我
03-04 23:40:18.572: E/AndroidRuntime(451): FATAL EXCEPTION: main
03-04 23:40:18.572: E/AndroidRuntime(451): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.mojica.caloriewatch/me.mojica.caloriewatch.Foodlog}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.os.Looper.loop(Looper.java:123)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-04 23:40:18.572: E/AndroidRuntime(451): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 23:40:18.572: E/AndroidRuntime(451): at java.lang.reflect.Method.invoke(Method.java:521)
03-04 23:40:18.572: E/AndroidRuntime(451): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-04 23:40:18.572: E/AndroidRuntime(451): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-04 23:40:18.572: E/AndroidRuntime(451): at dalvik.system.NativeStart.main(Native Method)
03-04 23:40:18.572: E/AndroidRuntime(451): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.ListActivity.onContentChanged(ListActivity.java:245)
03-04 23:40:18.572: E/AndroidRuntime(451): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.Activity.setContentView(Activity.java:1647)
03-04 23:40:18.572: E/AndroidRuntime(451): at me.mojica.caloriewatch.Foodlog.onCreate(Foodlog.java:22)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-04 23:40:18.572: E/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
我已經根據答案編輯此。我仍然無法運行。請幫助。
附加一個「捆綁」與「意圖」進行第二次活動。 – 2013-03-03 19:53:34
我嘗試過,但我怎麼能從它創建一個列表視圖?請幫幫我。 – 2013-03-03 19:55:15
我會幫助你,你首先將你的問題放在哪裏? – 2013-03-03 19:57:44