【问题标题】:How to change background of a textview in a custom listview for each row如何在每行的自定义列表视图中更改文本视图的背景
【发布时间】:2013-10-29 22:57:08
【问题描述】:

我创建了一个用于两个人聊天的应用程序

  • 首先我必须通过 Jsonparser 从服务器获取所有数据

  • 有一个自定义列表视图,每行包含“shop,painter,datetime,comment,id”

  • 如果你的名字不为空,则表示评论是你的,而且是你的,反之亦然

  • 我想放一个气泡背景来表示画家或商店的评论

当我有一个包含多个文本视图的自定义列表视图时,我不知道如何使用 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);

这是我的代码,但我想动态更改评论文本视图的背景

这样的代码怎么放????

   if (strPainter.equals("null")) {
    tvComment.setBackgroundResource(R.drawable.bubble_green);
    }
   if (strShop.equals("null")) {
    tvComment.setBackgroundResource(R.drawable.bubble_yellow);
    }

【问题讨论】:

    标签: android listview android-listview listadapter android-adapter


    【解决方案1】:

    不要使用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;
        }
    ...
    }
    

    【讨论】:

    • 当我的资源是没有位置的hashmap时,我不知道如何使用getview(),请您提供代码吗?
    • @HosseinMansouri 除非您以随机顺序显示 cmets,否则我怀疑链中的某处存在有序数据结构。使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多