在simpleAdapter中为textview设置颜色

时间:2014-05-14 04:18:05

标签: android

我有一个simpleAdapter从json获取数据并将其放在listview中,如下所示:

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;
                    }
                };          

2 个答案:

答案 0 :(得分:1)

使用get view更改颜色: -

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;
        }
    };

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

答案 1 :(得分:1)

转到xml布局文件,其中包含&#34; R.id.txtStatusMeeting&#34;

的布局

为它添加以下行以提供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"