Tôi có một ứng dụng tìm nạp thông báo được lưu trữ dưới dạng tệp xml trên máy chủ và tải tiêu đề và tác giả của từng thông báo vào mục ListView. Những gì tôi cũng cần lưu trữ với mỗi mục là ID của mỗi thông báo nhưng tôi không thực sự cần phải hiển thị nó. Tôi nghĩ về việc lưu trữ ID trong bản đồ băm mà tôi sử dụng để điền vào danh sách và sau đó tìm ID được liên kết với tiêu đề được nhấp nhưng tôi cho rằng nó sẽ không an toàn để sử dụng vì hai thông báo có thể có cùng một tiêu đề (và tác giả và ngày) . Tôi cũng nghĩ về việc thêm một TextView vô hình cho mỗi mục để lưu trữ ID nhưng điều đó đã gây ra các vấn đề bố cục. Cuối cùng, tôi đã tìm kiếm xung quanh và tìm thấy setTag() và getTag() mà tôi nghĩ rằng sẽ hoàn hảo cho những gì tôi muốn làm nhưng tôi không thực sự chắc chắn làm thế nào để sử dụng chúng với SimpleAdapter (tôi là tương đối mới này ...). Nếu ý tưởng TextView là những gì tôi cần phải làm gì (mặc dù tôi nghi ngờ nó), đây là cách bố trí Tôi đang sử dụng cho từng hạng mục:Đính kèm dữ liệu ID vào danh sách xem các mục
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/items"
android:weightSum="100"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_weight="85"/>
<LinearLayout
android:orientation="vertical"
android:id="@+id/itemCB"
android:layout_weight="15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<CheckBox
android:id="@+id/cbSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Và tôi đang sử dụng bộ chuyển đổi sau đây để điền vào danh sách:
for(int i = 0; i < ann.length; i++)
{
map = new HashMap<String, String>();
map.put("line1", ann[i].getTitle());
map.put("line2", "Posted by: " + ann[i].getAuthor() + "\n" + ann[i].date.toLongString());
list.add(map);
}
String[] from = { "line1", "line2"};
int[] to = { android.R.id.text1, android.R.id.text2};
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.twoline_checkbox_id_listitem, from, to);
setListAdapter(adapter);
Cảm ơn bạn đã được trợ giúp!
Tôi nghĩ rằng đây là câu trả lời tốt nhất cho bạn: http://stackoverflow.com/a/3051729/7379190 – AleX
Tôi nghĩ [này] (http : //stackoverflow.com/a/3051729/7379190) là câu trả lời hay nhất cho câu hỏi của bạn. – AleX