1

I have a simpleAdapter that get data from json and put it in a listview look like this:

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

I want to set color for textview txtStatusMeeting and the color decide by data from json: red color for Done and blue color for Incoming.

Is there anyway to do that without a custom adapter?

Update

I done it for now. Thank you guys. The final code below:

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;
                    }
                };          
gamo
  • 1,549
  • 7
  • 24
  • 36

2 Answers2

1

Use get view to change the Color :-

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

Like this way to give color for all text view in simple adapter or otherwise use Custom adapter class to customize the adapter.

Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • What's short_news in your code? I try your code with replace short_news by txtTitleMeeting. The item in adapter is still there but view is show up nothing. – gamo May 14 '14 at 07:07
1

go to the xml layout file which contains layout of "R.id.txtStatusMeeting"

add following line to it to give android predifined color

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

if you want to add custom colour add an xml file to the values folder in your project named,colors.xml

colors.xml

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

you can give any color you like by giving its # value instead of #a79c90

and add this line to the layout of control which you want to color

android:background="@color/status_meeting"
Fahad
  • 193
  • 1
  • 9
Ameen
  • 41
  • 5
  • I guess you miss something here. I want to set the color of textview base on the data I get from json. Your way is change all the items in adapter, right? – gamo May 14 '14 at 07:05