[英]How to change background of a textview in a custom listview for each row
我創建了兩個人之間聊天的應用程序
首先,我必須通過Jsonparser從服務器獲取所有數據
有一個自定義listview,每行包含“ shop,painter,datetime,comment,id”
如果您的名字不為空,則表示您的評論是viseversa
我想在氣泡背景上留言以表明畫家或商店
當我有一個帶有多個textview的自定義listview時,我不知道如何使用getview,因為包含所有信息的資源數據來自服務器並將其存儲到hashmap數組中……hashmap的位置與getview中的位置不一樣方法...
....一些代碼...
// adding HashList to ArrayList
AllCommentsList.add(map);
adapter = new SimpleAdapter(getApplicationContext(),
AllCommentsList, R.layout.list_row_order_comments,
new String[] { TAG_COMMENT_ID, TAG_SHOP, TAG_PAINTER,TAG_COMMENT, TAG_DATETIME },
new int[] { R.id.tvIdComments, R.id.tvShopSender,R.id.tvPainterSender, R.id.tvComment,R.id.tvDateTimeComments });
// updating listview
listViewComment.setAdapter(adapter);
這是我的代碼,但我想動態更改Comment textview的背景
如何把這樣的代碼???
if (strPainter.equals("null")) {
tvComment.setBackgroundResource(R.drawable.bubble_green);
}
if (strShop.equals("null")) {
tvComment.setBackgroundResource(R.drawable.bubble_yellow);
}
不要使用SimpleAdapter
。 創建一個覆蓋getView
的自定義適配器,並根據當前項對布局,背景等進行所需的任何操作。
有關簡單示例,請參見Android SDK文件夾中的/samples/android-8/ApiDemos/src/com/example/android/apis/view/List5.java
(根據需要下載適當的示例):
private class MyListAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = (TextView) LayoutInflater.from(mContext).inflate(
android.R.layout.simple_expandable_list_item_1, parent, false);
} else {
tv = (TextView) convertView;
}
tv.setText(mStrings[position]);
return tv;
}
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.