将MySQL中的ID包含到ListView中的每个项目并传递给第二个活动

时间:2014-11-06 13:25:24

标签: android mysql listview

我有一个ListView,它是从MySQL填充的。现在我正在尝试点击此列表视图的项目以打开另一个活动并获取该项目(ID)的更多信息。我有意图尝试过。在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);
}

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

}

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>

当我打开SecondActivity时为空。显然这放,并得到额外的错误。我也不明白如何做到这一点。我的意思是在我的表格中我有idnametext。在列表视图中,我只显示name。在SecondActivity中(当我点击名称时)想要加载nametext

3 个答案:

答案 0 :(得分:2)

您似乎忘记在textview中设置ID:

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

如果上述代码仍无效,请尝试更改:

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

答案 1 :(得分:2)

use this On SecondActivity.java


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

答案 2 :(得分:2)

有两种方式将一项活动传递给另一项活动。

1.将数据添加到意图中。

如何放:

intent.putExtra("id", position);

如何获得:

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

2.添加数据以捆绑并将包添加到意图。

如何放:

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

如何获得:

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