【问题标题】:Include ID from MySQL to each item in ListView and pass to second activity将 MySQL 中的 ID 包含到 ListView 中的每个项目并传递给第二个活动
【发布时间】:2014-11-06 13:25:23
【问题描述】:

我有一个从 MySQL 填充的ListView。现在,当我单击此列表视图的项目以打开另一个活动并获取该项目(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 是空的。显然,这个 put and get extras 是错误的。我也不明白这是怎么做到的。我的意思是在我的桌子上我有idnametext。在列表视图中,我只显示name。在 SecondActivity(当我单击名称时)要加载 nametext

【问题讨论】:

标签: android mysql listview


【解决方案1】:

您似乎忘记在 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+"");

【讨论】:

  • 我在两个示例中的第一项上都得到了 id 0?
  • intent.putExtra("id", position);你确定位置是1了吗?
  • 没有?如何查看位置?
  • 这来自logcat 加载第一个活动时。 11-06 08:46:53.940: E/err(845): 1 rest1.jpg 我相信这个 1 之前的图像是我从 MySQL 获得的 ID。其他结果与 2...3...4..n 类似。
  • 啊..我想我现在明白了。这里我不管这个0是什么。。这是listview的位置。我关心留在这个0 后面的id,对吧?在这种情况下,0 将从 DB 中为id=1 加载内容?
【解决方案2】:
use this On SecondActivity.java


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

【讨论】:

  • 在这里,当我点击第一项时,我得到 ID=0 .. 我认为这是错误的。它必须是 1,因为在 DB 中是 1。
  • 不,这是正确的,它是列表视图的位置,您是否在(int 位置)上检查它 OnItemclickListner 以及您现在的列表视图位置,如 0、1、2、3,----
【解决方案3】:

有两种方式将数据传递/获取一个活动到另一个活动。

1.向意图添加数据。

怎么放:

intent.putExtra("id", position);

如何获得:

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

2.将数据添加到bundle并将bundle添加到intent。

怎么放:

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

如何获得:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多