0

我有一个从 SqLite Db 获取数据的列表视图。我需要获取在列表视图中单击的数据并将其显示在列表视图的另一个活动中。

我曾尝试使用共享首选项,但我一次只能获取一个数据并将其作为文本视图显示在另一个活动中。

我不知道如何在列表视图中全部显示所选数据或从所选数据创建列表视图,需要帮助。

我的搜索活动

  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); 
  } 

这是我的美食日志活动

  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)

我根据答案对此进行了编辑。我仍然无法让它运行。请帮忙。

4

1 回答 1

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);
  }

} 

我建议阅读整个教程

于 2013-03-03T20:09:31.353 回答