【问题标题】:Set color for textview in simpleAdapter在 simpleAdapter 中为 textview 设置颜色
【发布时间】:2014-05-14 04:18:04
【问题描述】:

我有一个 simpleAdapter,它从 json 获取数据并将其放在一个列表视图中,如下所示:

adapter = new SimpleAdapter(
                        // Updating listview
                        getActivity(), arrMeetings,
                        R.layout.meeting_item, new String[] {mtTAG_MEETINGNAME, mtTAG_DATE, mtTAG_TIME, mtTAG_LOCATION, mtTAG_MEETINGSTATUS},
                        new int[] {  R.id.txtTitleMeeting, R.id.txtDatetime, R.id.txtTime, R.id.txtLocation, R.id.txtStatusMeeting});

我想为 textview txtStatusMeeting 设置颜色,颜色由 json 中的数据决定:Done 为红色,Incoming 为蓝色。

如果没有自定义适配器,有没有办法做到这一点?

更新

我现在已经完成了。谢谢你们。 最终代码如下:

adapter = new SimpleAdapter(
                        // Updating listview
                        getActivity(), arrMeetings,
                        R.layout.meeting_item, new String[] {mtTAG_MEETINGNAME, mtTAG_DATE, mtTAG_TIME, mtTAG_LOCATION, mtTAG_MEETINGSTATUS},
                        new int[] {  R.id.txtTitleMeeting, R.id.txtDatetime, R.id.txtTime, R.id.txtLocation, R.id.txtStatusMeeting}){
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        View v = convertView;
                        for(int i=0;i<arrMeetings.size();i++){
                            if(v == null){
                                LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                v=vi.inflate(R.layout.meeting_item, null);
                            }
                            TextView txtTitleMeeting = (TextView) v.findViewById(R.id.txtTitleMeeting);
                            TextView txtDatetime = (TextView) v.findViewById(R.id.txtDatetime);
                            TextView txtTime = (TextView) v.findViewById(R.id.txtTime);
                            TextView txtLocation = (TextView) v.findViewById(R.id.txtLocation);
                            TextView txtStatusMeeting = (TextView) v.findViewById(R.id.txtStatusMeeting);

                            txtTitleMeeting.setText(arrMeetings.get(position).get(mtTAG_MEETINGNAME));
                            txtDatetime.setText(arrMeetings.get(position).get(mtTAG_DATE));
                            txtTime.setText(arrMeetings.get(position).get(mtTAG_TIME));
                            txtLocation.setText(arrMeetings.get(position).get(mtTAG_LOCATION));

                            if(arrMeetings.get(position).get(mtTAG_MEETINGSTATUS).equals("Today")){
                                txtStatusMeeting.setText(arrMeetings.get(position).get(mtTAG_MEETINGSTATUS));
                                txtStatusMeeting.setTextColor(getActivity().getResources().getColor(R.color.Today));
                            }else if(arrMeetings.get(position).get(mtTAG_MEETINGSTATUS).equals("Incoming")){
                                txtStatusMeeting.setText(arrMeetings.get(position).get(mtTAG_MEETINGSTATUS));
                                txtStatusMeeting.setTextColor(getActivity().getResources().getColor(R.color.Incoming));
                            }else if(arrMeetings.get(position).get(mtTAG_MEETINGSTATUS).equals("Done")){
                                txtStatusMeeting.setText(arrMeetings.get(position).get(mtTAG_MEETINGSTATUS));
                                txtStatusMeeting.setTextColor(getActivity().getResources().getColor(R.color.Done));
                            }                       
                        }
                        return v;
                    }
                };          

【问题讨论】:

  • meeting_item.xml中设置颜色
  • 我想你可能不需要使用自定义适配器 ....
  • 如果您在 xml 中设置颜色,该颜色将用于所有项目的文本视图。如果要根据值更改颜色,请使用自定义适配器。为什么不自定义适配器? stackoverflow.com/questions/20611123/…
  • 我知道自定义适配器可以在这里使用,但我想尝试不使用它。

标签: android


【解决方案1】:

使用获取视图更改颜色:-

adp_list_news = new SimpleAdapter(getActivity(), arrMeetings,R.layout.meeting_item,new String[] {mtTAG_MEETINGNAME, mtTAG_DATE, mtTAG_TIME, mtTAG_LOCATION, mtTAG_MEETINGSTATUS}, new int[] {R.id.txtTitleMeeting, R.id.txtDatetime, R.id.txtTime, R.id.txtLocation, R.id.txtStatusMeeting}){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = convertView;
            if(v== null){

                LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=vi.inflate(R.layout.meeting_item, null);
            }

            TextView txtTitleMeeting = (TextView) v.findViewById(R.id.txtTitleMeeting);
            short_news.setTextColor(Color.parseColor("#ffffff"));;

            return v;
        }
    };

像这样为简单适配器中的所有文本视图赋予颜色,或者使用自定义适配器类来自定义适配器。

【讨论】:

  • 您的代码中的 short_news 是什么?我用 txtTitleMeeting 替换 short_news 来尝试你的代码。适配器中的项目仍然存在,但视图没有显示。
【解决方案2】:

转到包含“R.id.txtStatusMeeting”布局的xml布局文件

添加以下行以赋予 android 预定义颜色

 android:background="@android:color/SELECT_THE_COLOR_PRESENT_IN ANDROID_LIBRERy"

如果你想添加自定义颜色 将 xml 文件添加到名为 colors.xml 的项目中的 values 文件夹中

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <color name="status_meeting">#a79c90</color>
</resources>

你可以通过给出它的#值而不是#a79c90来给出你喜欢的任何颜色

并将这一行添加到要着色的控件布局中

android:background="@color/status_meeting"

【讨论】:

  • 我猜你错过了一些东西。我想根据从 json 获得的数据设置 textview 的颜色。你的方法是改变适配器中的所有项目,对吧?
猜你喜欢
  • 2012-09-13
  • 1970-01-01
  • 2011-03-18
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多