[英]List View with images and text
我正在嘗試做一個顯示圖像的List和一個描述圖像的簡單文本。 在我的互聯網搜索中,我發現了許多方法。 有些人使用ArrayAdapter,有些人使用SimpleCursorAdapter。 有一點我注意到,許多人正在創建繼承自ListActivity的類,並且在setListAdapter
方法中,他們正在插入從Array或SimpleCursor適配器派生的其他類。 第一個問題:這是最好的方法嗎?
我在里面創建了一個帶有ListView的LinearLayout。 為了插入行,使用ImageView
和TextView
創建了另一個布局。 第二個問題:這是對的嗎?
我對創建這種類型的組件感到困惑。 這是正確的方法嗎?
是的,這是正確的,雖然您需要使用CursorAdapter
而不是SimpleCursorAdapter
,因為SimpleCursorAdapter
是填充僅包含TextView
的行。
您將在CursorAdapter
上使用getView
方法擴展行布局:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) { // we don't have a recycled view
convertView = LayoutInflator.from(getContext()).inflate(
R.layout.row, parent, false);
}
// setup our row
TextView text = (TextView) convertView.findViewById(R.id.text_view);
text.setText( ... );
ImageView image = (ImageView) convertView.findViewById(R.id.image_view);
image.setImageBitmap( ... );
return convertView;
}
在設置視圖的文本和圖像時,可以使用getItem
等適配器方法來訪問所需的基礎數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.