我有一个listview从SqLite Db获取数据。我需要获取列表视图中单击的数据,并在列表视图中的另一个活动中显示它。
我尝试过使用共享首选项,但我一次只能获取一个数据,并将其作为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)
我根据答案编辑了这个。我仍然无法让它运行。求助。
答案 0 :(得分:1)
单击ListView项时,使用Intent将包含数据的Extra包传递给下一个活动。
Intent myIntent = new Intent(currentActivity.this, secondActivity.class);
myIntent.putExtra("SQLITEDATA1", food.getText().toString());
myIntent.putExtra("SQLITEDATA2", calories.getText().toString());
startActivity(myIntent);
使用此数据,您应该通过制作自定义ListView适配器来填充ListView。
public class SimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public SimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
return rowView;
}
}
要在其他活动中填写ListView:
public class MyListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String food = myIntent.getStringExtra("SQLITEDATA1");
String calories = myIntent.getStringExtra("SQLITEDATA2");
String[] values = new String[] { food, calories }; //That you got from your intent bundle
SimpleArrayAdapter adapter = new SimpleArrayAdapter(this, values);
setListAdapter(adapter);
}
}