michelle.mo
michelle.mo

Reputation: 53

Pass data from a ListView to another Listview

I have a listview fetching data from SqLite Db. I need to get the data that is being clicked in the listview and show it in another activity in a listview.

I have tried using shared preferences but i could only get one data at a time and display it in another activity as a textview.

I have no idea how to all show the selected data in a listview or create a listview from the selected data, need help.

my search activity

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

here is my foodlog activity

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

       }

hope you can help me

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)

i have edited this based on the answers. still i cant get it running. help please.

Upvotes: 0

Views: 3325

Answers (1)

Edward van Raak
Edward van Raak

Reputation: 4910

You pass an Extra bundle with data to your next activity using Intent when you click the ListView item.

Intent myIntent = new Intent(currentActivity.this, secondActivity.class);
myIntent.putExtra("SQLITEDATA1", food.getText().toString()); 
myIntent.putExtra("SQLITEDATA2", calories.getText().toString()); 
startActivity(myIntent); 

Using this data you should fill the ListView by making a custom ListView adapter.

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

To fill the ListView in your other Activity:

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

} 

I recommend reading this entire tutorial.

Upvotes: 1

Related Questions