打开 activity 显示列表视图中每一行的详细信息

Open activity displaying the detail of each row in listview

我想打开对象的列表视图并单击列表行,它会打开另一个 activity 显示该行的新闻详细信息。到目前为止,我可以打开一个新的 activity,它在单击列表中的行时显示行的名称。 但我不知道如何让每一行的 activity 显示一些不同的信息,例如每行的地址、联系信息的开放时间。

这是我第一个 activity 显示列表视图

 // storing string resources into Array
 String []Buildings_halls = getResources().getStringArray(R.array.halls);

    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.hallstudent, R.id.listhall, Buildings_halls));

 }

 protected void onListItemClick(ListView lv, View v, int position, long id){
     super.onListItemClick(lv, v, position, id);     

Intent intent = new Intent();
intent.setClass(StudentHall.this, StudentHallSelect.class);

intent.putExtra("position", position);

// Or / And
intent.putExtra("id", String.valueOf(id));
intent.putExtra("description", getResources().getStringArray(R.array.hallsdescription));
startActivity(intent);

        }               

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
}
this is my second code package 





 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selecthall);
        // TODO Auto-generated method stub


        Intent intent = getIntent(); 
        int position = intent.getIntExtra("position", 0);
        String halldetails=intent.getStringExtra("description");

        TextView TextView1 = (TextView) findViewById(R.id.select_details);
        TextView1.setText(String.valueOf(halldetails));
        // Here we turn your string.xml in an array
        String[] myKeys = getResources().getStringArray(R.array.halls);

        TextView myTextView = (TextView) findViewById(R.id.selecthalllist);
        myTextView.setText(myKeys[position]);


    }
}



 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/select_details"
        android:padding="10dp"
        android:text="@string/news_description"
        android:textAppearance="?android:attr/textAppearanceMedium" />



</LinearLayout>

我在我的新 activity 中得到了列表的名称和 Null,但没有我存储在我的 array.xml 文件中的详细信息。 谢谢

将列表中选中的对象传递到详情界面的例子,请看这里: How to view detailed data of a specific list view item

或者,如果您的数据在 SQLite 数据库中,您将:

  1. 使用 CursorAdapter
  2. 传递选中项的id参数
  3. 在您的详细信息屏幕中使用 ID 参数查询所选对象

这取决于这些额外信息存储在何处以及如何存储。我更喜欢使用位置从第二个 activity 中检索此信息,但如果这个不适合你,还有其他方法。

您可以使用与职位相同的方法传递所有需要的信息。您可以在同一意图中添加不同的数据类型,例如 intStringfloatlongbytebooleanArrays 以及 ParcelableSerializable 元素。

只需在第一个 activity 中使用 intent.putExtra("key", value); 并在第二个 activity 中使用 getIntExtra("key", defaultValue)getLongExtra("key", defaultValue)getFloatExtra("key", defaultValue)...取决于每个额外的类型。