Goro
Goro

Reputation: 499

Include ID from MySQL to each item in ListView and pass to second activity

I hava a ListView which is populated from MySQL. Now I'm trying when I click on item of this listview to open another activity and grab some more info for that item(ID). I've tried with intent. In FirstAActivity.java

public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
       Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
       intent.putExtra("id", position);
       intent.putExtra("text", text);                
       startActivity(intent);
}

And SecondActivity.java

String text;
int id;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);

    textView = (TextView) findViewById(R.id.id);
    textView = (TextView) findViewById(R.id.text);

    Intent intent = getIntent();
    if (intent.getExtras() != null) {
        id = (Integer) intent.getExtras().get("id");
        text = (String) intent.getExtras().get("text");
        textView.setText(id+"");
    }

}

And the second_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="fill_parent"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="10dp"
    android:background="@drawable/restaurants_buttons"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/id"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingTop="15dp" />
    <TextView
        android:id="@+id/text"
        android:layout_width="280dp"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="" 
</LinearLayout>

When I open SecondActivity is empty. Apparently this put and get extras are wrong. Also I don't understand how this can be done at all. I mean in my table I have id, name, text. In listview I show only name. In SecondActivity (when I clicked on name ) want to load name and text.

Upvotes: 0

Views: 968

Answers (3)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

There is two way pass/get data one activity to another activity.

1.add data to intent.

how to put :

intent.putExtra("id", position);

how to get :

String id = getIntent().getStringExtra("id");

2.Add data to bundle and add bundle to intent.

how to put :

Bundle bundle = new Bundle();
bundle.putString("id", position);
intent.putExtras(bundle);

how to get :

String id = getIntent().getExtras().getString("id");

Upvotes: 2

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

use this On SecondActivity.java


int id;
textView = (TextView) findViewById(R.id.id);  
id= getIntent().getExtras().getInt("id");
textview.setText(id+"");

Upvotes: 2

Blaze Tama
Blaze Tama

Reputation: 10948

You seem to forget to set the id in the textview :

textView = (TextView) findViewById(R.id.id);        
Intent intent = getIntent();
if (intent.getExtras() != null) {
id = (Integer) intent.getExtras().get("id");
textview.setText(id+"");

If the above code still not working, try to change it like this :

textView = (TextView) findViewById(R.id.id);        
Bundle b = getIntent().getExtras();
if (b != null) {
id = b.getInt("id");
textview.setText(id+"");

Upvotes: 2

Related Questions