2014-02-11 24 views
0

我想改變依賴於從PHP的JSONObject值文本的顏色。該程序已經可以獲取該值並能夠在每個文本視圖中顯示它。但我的問題是,我怎樣才能同時改變每個文本視圖的顏色,具體取決於Flimitstat,Vlimitstat和Climitstat。希望你能幫我!setTextColor在onpostexecute()從另一個XML(R.layout.list_item)的AsyncTask

這是我ThirdFragment.java

package com.example.RadarOperationMonitoringSystem; 

import android.app.ProgressDialog; 
import android.graphics.Color; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListAdapter; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 


public class ThirdFragment extends ListFragment { 

    String site1 = ""; 
    String freq1 = ""; 
    String curr1 = ""; 
    String volts1 = ""; 
    String fstat1 = ""; 
    String vstat1 = ""; 
    String cstat1 = ""; 

    String site2 = ""; 
    String freq2 = ""; 
    String curr2 = ""; 
    String volts2 = ""; 
    String fstat2 = ""; 
    String vstat2 = ""; 
    String cstat2 = ""; 

    String site3 = ""; 
    String freq3 = ""; 
    String curr3 = ""; 
    String volts3 = ""; 
    String fstat3 = ""; 
    String vstat3 = ""; 
    String cstat3 = ""; 

    String site4 = ""; 
    String freq4 = ""; 
    String curr4 = ""; 
    String volts4 = ""; 
    String fstat4 = ""; 
    String vstat4 = ""; 
    String cstat4 = ""; 

    String site5 = ""; 
    String freq5 = ""; 
    String curr5 = ""; 
    String volts5 = ""; 
    String fstat5 = ""; 
    String vstat5 = ""; 
    String cstat5 = ""; 

    String site6 = ""; 
    String freq6 = ""; 
    String curr6 = ""; 
    String volts6 = ""; 
    String fstat6 = ""; 
    String vstat6 = ""; 
    String cstat6 = ""; 

    String site7 = ""; 
    String freq7 = ""; 
    String curr7 = ""; 
    String volts7 = ""; 
    String fstat7 = ""; 
    String vstat7 = ""; 
    String cstat7 = ""; 

    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

    TextView freqa; 
    TextView voltsa; 
    TextView curra; 
    TextView freqb; 
    TextView voltsb; 
    TextView currb; 
    TextView freqc; 
    TextView voltsc; 
    TextView currc; 
    TextView freqd; 
    TextView voltsd; 
    TextView currd; 
    TextView freqe; 
    TextView voltse; 
    TextView curre; 
    TextView freqf; 
    TextView voltsf; 
    TextView currf; 
    TextView freqg; 
    TextView voltsg; 
    TextView currg; 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.third_frag, container, false); 

     freqa = (TextView)v.findViewById(R.id.frequency1); 
     voltsa = (TextView)v.findViewById(R.id.acvoltage1); 
     curra = (TextView)v.findViewById(R.id.accurrent1); 
     freqb = (TextView)v.findViewById(R.id.frequency2); 
     voltsb = (TextView)v.findViewById(R.id.acvoltage2); 
     currb = (TextView)v.findViewById(R.id.accurrent2); 
     freqc = (TextView)v.findViewById(R.id.frequency3); 
     voltsc = (TextView)v.findViewById(R.id.acvoltage3); 
     currc = (TextView)v.findViewById(R.id.accurrent3); 
     freqd = (TextView)v.findViewById(R.id.frequency4); 
     voltsd = (TextView)v.findViewById(R.id.acvoltage4); 
     currd = (TextView)v.findViewById(R.id.accurrent4); 
     freqe = (TextView)v.findViewById(R.id.frequency5); 
     voltse = (TextView)v.findViewById(R.id.acvoltage5); 
     curre = (TextView)v.findViewById(R.id.accurrent5); 
     freqf = (TextView)v.findViewById(R.id.frequency6); 
     voltsf = (TextView)v.findViewById(R.id.acvoltage6); 
     currf = (TextView)v.findViewById(R.id.accurrent6); 
     freqg = (TextView)v.findViewById(R.id.frequency7); 
     voltsg = (TextView)v.findViewById(R.id.acvoltage7); 
     currg = (TextView)v.findViewById(R.id.accurrent7); 

     new GetContacts().execute(); 
     return v; 
    } 


    public class GetContacts extends AsyncTask<Void, Void, Void> { 

     private ProgressDialog pDialog; 

     // URL to get contacts JSON 
     private static final String url = "http://10.0.2.2/radaroperations/energyreadings.php"; 

     private static final String TAG_SITENAME1 = "siteName1"; 
     private static final String TAG_FREQUENCY1 = "Frequency1"; 
     private static final String TAG_ACCURRENT1 = "AC_Voltage1"; 
     private static final String TAG_ACVOLTAGE1 = "AC_Current1"; 
     private static final String TAG_FSTAT1 = "Flimitstat1"; 
     private static final String TAG_VSTAT1 = "Vlimitstat1"; 
     private static final String TAG_CSTAT1 = "Climitstat1"; 

     private static final String TAG_SITENAME2 = "siteName2"; 
     private static final String TAG_FREQUENCY2 = "Frequency2"; 
     private static final String TAG_ACCURRENT2 = "AC_Voltage2"; 
     private static final String TAG_ACVOLTAGE2 = "AC_Current2"; 
     private static final String TAG_FSTAT2 = "Flimitstat2"; 
     private static final String TAG_VSTAT2 = "Vlimitstat2"; 
     private static final String TAG_CSTAT2 = "Climitstat2"; 

     private static final String TAG_SITENAME3 = "siteName3"; 
     private static final String TAG_FREQUENCY3 = "Frequency3"; 
     private static final String TAG_ACCURRENT3 = "AC_Voltage3"; 
     private static final String TAG_ACVOLTAGE3 = "AC_Current3"; 
     private static final String TAG_FSTAT3 = "Flimitstat3"; 
     private static final String TAG_VSTAT3 = "Vlimitstat3"; 
     private static final String TAG_CSTAT3 = "Climitstat3"; 

     private static final String TAG_SITENAME4 = "siteName4"; 
     private static final String TAG_FREQUENCY4 = "Frequency4"; 
     private static final String TAG_ACCURRENT4 = "AC_Voltage4"; 
     private static final String TAG_ACVOLTAGE4 = "AC_Current4"; 
     private static final String TAG_FSTAT4 = "Flimitstat4"; 
     private static final String TAG_VSTAT4 = "Vlimitstat4"; 
     private static final String TAG_CSTAT4 = "Climitstat4"; 

     private static final String TAG_SITENAME5 = "siteName5"; 
     private static final String TAG_FREQUENCY5 = "Frequency5"; 
     private static final String TAG_ACCURRENT5 = "AC_Voltage5"; 
     private static final String TAG_ACVOLTAGE5 = "AC_Current5"; 
     private static final String TAG_FSTAT5 = "Flimitstat5"; 
     private static final String TAG_VSTAT5 = "Vlimitstat5"; 
     private static final String TAG_CSTAT5 = "Climitstat5"; 

     private static final String TAG_SITENAME6 = "siteName6"; 
     private static final String TAG_FREQUENCY6 = "Frequency6"; 
     private static final String TAG_ACCURRENT6 = "AC_Voltage6"; 
     private static final String TAG_ACVOLTAGE6 = "AC_Current6"; 
     private static final String TAG_FSTAT6 = "Flimitstat6"; 
     private static final String TAG_VSTAT6 = "Vlimitstat6"; 
     private static final String TAG_CSTAT6 = "Climitstat6"; 

     private static final String TAG_SITENAME7 = "siteName7"; 
     private static final String TAG_FREQUENCY7 = "Frequency7"; 
     private static final String TAG_ACCURRENT7 = "AC_Voltage7"; 
     private static final String TAG_ACVOLTAGE7 = "AC_Current7"; 
     private static final String TAG_FSTAT7 = "Flimitstat7"; 
     private static final String TAG_VSTAT7 = "Vlimitstat7"; 
     private static final String TAG_CSTAT7 = "Climitstat7"; 

     // contacts JSONArray 
     JSONObject c = null; 




     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Showing progress dialog 
      pDialog = new ProgressDialog(getActivity()); 
      pDialog.setMessage("Please wait..."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 

     } 

     @Override 
     public Void doInBackground(Void... arg0) { 
      // Creating service handler class instance 
      ServiceHandler sh = new ServiceHandler(); 

      // Making a request to url and getting response 
      String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

      Log.d("Response: ", "> " + jsonStr); 

      if (jsonStr != null) { 
       try { 
        JSONObject c = new JSONObject(jsonStr); 

        // Getting JSON Array node 
        site1 = c.getString(TAG_SITENAME1); 
        freq1 = c.getString(TAG_FREQUENCY1); 
        curr1 = c.getString(TAG_ACCURRENT1); 
        volts1 = c.getString(TAG_ACVOLTAGE1); 
        fstat1 = c.getString(TAG_FSTAT1); 
        vstat1 = c.getString(TAG_VSTAT1); 
        cstat1 = c.getString(TAG_CSTAT1); 

        site2 = c.getString(TAG_SITENAME2); 
        freq2 = c.getString(TAG_FREQUENCY2); 
        curr2 = c.getString(TAG_ACCURRENT2); 
        volts2 = c.getString(TAG_ACVOLTAGE2); 
        fstat2 = c.getString(TAG_FSTAT2); 
        vstat2 = c.getString(TAG_VSTAT2); 
        cstat2 = c.getString(TAG_CSTAT2); 

        site3 = c.getString(TAG_SITENAME3); 
        freq3 = c.getString(TAG_FREQUENCY3); 
        curr3 = c.getString(TAG_ACCURRENT3); 
        volts3 = c.getString(TAG_ACVOLTAGE3); 
        fstat3 = c.getString(TAG_FSTAT3); 
        vstat3 = c.getString(TAG_VSTAT3); 
        cstat3 = c.getString(TAG_CSTAT3); 

        site4 = c.getString(TAG_SITENAME4); 
        freq4 = c.getString(TAG_FREQUENCY4); 
        curr4 = c.getString(TAG_ACCURRENT4); 
        volts4 = c.getString(TAG_ACVOLTAGE4); 
        fstat4 = c.getString(TAG_FSTAT4); 
        vstat4 = c.getString(TAG_VSTAT4); 
        cstat4 = c.getString(TAG_CSTAT4); 

        site5 = c.getString(TAG_SITENAME5); 
        freq5 = c.getString(TAG_FREQUENCY5); 
        curr5 = c.getString(TAG_ACCURRENT5); 
        volts5 = c.getString(TAG_ACVOLTAGE5); 
        fstat5 = c.getString(TAG_FSTAT5); 
        vstat5 = c.getString(TAG_VSTAT5); 
        cstat5 = c.getString(TAG_CSTAT5); 

        site6 = c.getString(TAG_SITENAME6); 
        freq6 = c.getString(TAG_FREQUENCY6); 
        curr6 = c.getString(TAG_ACCURRENT6); 
        volts6 = c.getString(TAG_ACVOLTAGE6); 
        fstat6 = c.getString(TAG_FSTAT6); 
        vstat6 = c.getString(TAG_VSTAT6); 
        cstat6 = c.getString(TAG_CSTAT6); 

        site7 = c.getString(TAG_SITENAME7); 
        freq7 = c.getString(TAG_FREQUENCY7); 
        curr7 = c.getString(TAG_ACCURRENT7); 
        volts7 = c.getString(TAG_ACVOLTAGE7); 
        fstat7 = c.getString(TAG_FSTAT7); 
        vstat7 = c.getString(TAG_VSTAT7); 
        cstat7 = c.getString(TAG_CSTAT7); 

        // tmp hashmap for single contact 
        HashMap<String, String> contact = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        contact.put(TAG_SITENAME1, site1); 
        contact.put(TAG_FREQUENCY1, freq1); 
        contact.put(TAG_ACCURRENT1, curr1); 
        contact.put(TAG_ACVOLTAGE1, volts1); 
        contact.put(TAG_FSTAT1, fstat1); 
        contact.put(TAG_VSTAT1, vstat1); 
        contact.put(TAG_CSTAT1, cstat1); 

        contact.put(TAG_SITENAME2, site2); 
        contact.put(TAG_FREQUENCY2, freq2); 
        contact.put(TAG_ACCURRENT2, curr2); 
        contact.put(TAG_ACVOLTAGE2, volts2); 
        contact.put(TAG_FSTAT2, fstat2); 
        contact.put(TAG_VSTAT2, vstat2); 
        contact.put(TAG_CSTAT2, cstat2); 

        contact.put(TAG_SITENAME3, site3); 
        contact.put(TAG_FREQUENCY3, freq3); 
        contact.put(TAG_ACCURRENT3, curr3); 
        contact.put(TAG_ACVOLTAGE3, volts3); 
        contact.put(TAG_FSTAT3, fstat3); 
        contact.put(TAG_VSTAT3, vstat3); 
        contact.put(TAG_CSTAT3, cstat3); 

        contact.put(TAG_SITENAME4, site4); 
        contact.put(TAG_FREQUENCY4, freq4); 
        contact.put(TAG_ACCURRENT4, curr4); 
        contact.put(TAG_ACVOLTAGE4, volts4); 
        contact.put(TAG_FSTAT4, fstat4); 
        contact.put(TAG_VSTAT4, vstat4); 
        contact.put(TAG_CSTAT4, cstat4); 

        contact.put(TAG_SITENAME5, site5); 
        contact.put(TAG_FREQUENCY5, freq5); 
        contact.put(TAG_ACCURRENT5, curr5); 
        contact.put(TAG_ACVOLTAGE5, volts5); 
        contact.put(TAG_FSTAT5, fstat5); 
        contact.put(TAG_VSTAT5, vstat5); 
        contact.put(TAG_CSTAT5, cstat5); 

        contact.put(TAG_SITENAME6, site6); 
        contact.put(TAG_FREQUENCY6, freq6); 
        contact.put(TAG_ACCURRENT6, curr6); 
        contact.put(TAG_ACVOLTAGE6, volts6); 
        contact.put(TAG_FSTAT6, fstat6); 
        contact.put(TAG_VSTAT6, vstat6); 
        contact.put(TAG_CSTAT6, cstat6); 

        contact.put(TAG_SITENAME7, site7); 
        contact.put(TAG_FREQUENCY7, freq7); 
        contact.put(TAG_ACCURRENT7, curr7); 
        contact.put(TAG_ACVOLTAGE7, volts7); 
        contact.put(TAG_FSTAT7, fstat7); 
        contact.put(TAG_VSTAT7, vstat7); 
        contact.put(TAG_CSTAT7, cstat7); 

        // adding contact to contact list 
        contactList.clear(); 
        contactList.add(contact); 



       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      }else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      } 

      return null; 
     } 
     @Override 
     public void onPostExecute(Void result) { 
      super.onPostExecute(result); 

      // Dismiss the progress dialog 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
      /** 
      * 
      */ 

      //RADAR 1 
      //Frequency 1 status 
      if (TAG_FSTAT1.equals("1")){ 
       freqa.setTextColor(Color.GREEN); 
      } 
      else if(TAG_FSTAT1.equals("2")){ 
       freqa.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Voltage 1 status 
      if (TAG_VSTAT1.equals("1")){ 
       voltsa.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_VSTAT1.equals("2")){ 
       voltsa.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Current 1 status 
      if (TAG_CSTAT1.equals("1")){ 
       curra.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_CSTAT1.equals("2")){ 
       curra.setTextColor(getResources().getColor(R.color.red)); 
      } 

      //RADAR 2 
      //Frequency 2 status 
      if (TAG_FSTAT2.equals("1")){ 
       freqb.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_FSTAT2.equals("2")){ 
       freqb.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Voltage 2 status 
      if (TAG_VSTAT2.equals("1")){ 
       voltsb.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_VSTAT2.equals("2")){ 
       voltsb.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Current 2 status 
      if (TAG_CSTAT2.equals("1")){ 
       currb.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_CSTAT2.equals("2")){ 
       currb.setTextColor(getResources().getColor(R.color.red)); 
      } 

      //RADAR 3 
      //Frequency 3 status 
      if (TAG_FSTAT3.equals("1")){ 
       freqc.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_FSTAT3.equals("2")){ 
       freqc.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Voltage 3 status 
      if (TAG_VSTAT3.equals("1")){ 
       voltsc.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_VSTAT3.equals("2")){ 
       voltsc.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Current 3 status 
      if (TAG_CSTAT3.equals("1")){ 
       currc.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_CSTAT3.equals("2")){ 
       currc.setTextColor(getResources().getColor(R.color.red)); 
      } 

      //RADAR 4 
      //Frequency 4 status 
      if (TAG_FSTAT4.equals("1")){ 
       freqd.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_FSTAT4.equals("2")){ 
       freqd.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Voltage 4 status 
      if (TAG_VSTAT4.equals("1")){ 
       voltsd.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_VSTAT4.equals("2")){ 
       voltsd.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Current 4 status 
      if (TAG_CSTAT4.equals("1")){ 
       currd.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_CSTAT4.equals("2")){ 
       currd.setTextColor(getResources().getColor(R.color.red)); 
      } 

      //RADAR 5 
      //Frequency 5 status 
      if (TAG_FSTAT5.equals("1")){ 
       freqe.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_FSTAT5.equals("2")){ 
       freqe.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Voltage 5 status 
      if (TAG_VSTAT5.equals("1")){ 
       voltse.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_VSTAT5.equals("2")){ 
       voltse.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Current 5 status 
      if (TAG_CSTAT5.equals("1")){ 
       curre.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_CSTAT5.equals("2")){ 
       curre.setTextColor(getResources().getColor(R.color.red)); 
      } 

      //RADAR 6 
      //Frequency 6 status 
      if (TAG_FSTAT6.equals("1")){ 
       freqf.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_FSTAT6.equals("2")){ 
       freqf.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Voltage 6 status 
      if (TAG_VSTAT6.equals("1")){ 
       voltsf.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_VSTAT6.equals("2")){ 
       voltsf.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Current 6 status 
      if (TAG_CSTAT6.equals("1")){ 
       currf.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_CSTAT6.equals("2")){ 
       currf.setTextColor(getResources().getColor(R.color.red)); 
      } 

      //RADAR 7 
      //Frequency 7 status 
      if (TAG_FSTAT7.equals("1")){ 
       freqg.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_FSTAT7.equals("2")){ 
       freqg.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Voltage 7 status 
      if (TAG_VSTAT7.equals("1")){ 
       voltsg.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_VSTAT7.equals("2")){ 
       voltsg.setTextColor(getResources().getColor(R.color.red)); 
      } 
      //AC_Current 7 status 
      if (TAG_CSTAT7.equals("1")){ 
       currg.setTextColor(getResources().getColor(R.color.green)); 
      } 
      else if(TAG_CSTAT7.equals("2")){ 
       currg.setTextColor(getResources().getColor(R.color.red)); 
      } 

      ListAdapter adapter = new SimpleAdapter(
        getActivity(), contactList, 
        R.layout.list_item, new String[] { TAG_SITENAME1, TAG_FREQUENCY1, TAG_ACCURRENT1, 
        TAG_ACVOLTAGE1,TAG_SITENAME2, TAG_FREQUENCY2, TAG_ACCURRENT2, 
        TAG_ACVOLTAGE2,TAG_SITENAME3, TAG_FREQUENCY3, TAG_ACCURRENT3, 
        TAG_ACVOLTAGE3, TAG_SITENAME4, TAG_FREQUENCY4, TAG_ACCURRENT4, 
        TAG_ACVOLTAGE4, TAG_SITENAME5, TAG_FREQUENCY5, TAG_ACCURRENT5, 
        TAG_ACVOLTAGE5, TAG_SITENAME6, TAG_FREQUENCY6, TAG_ACCURRENT6, 
        TAG_ACVOLTAGE6, TAG_SITENAME7, TAG_FREQUENCY7, TAG_ACCURRENT7, 
        TAG_ACVOLTAGE7}, 
        new int[] { R.id.sitename1, R.id.frequency1, 
          R.id.accurrent1, R.id.acvoltage1, R.id.sitename2, R.id.frequency2, 
          R.id.accurrent2, R.id.acvoltage2, R.id.sitename3, R.id.frequency3, 
          R.id.accurrent3, R.id.acvoltage3, R.id.sitename4, R.id.frequency4, 
          R.id.accurrent4, R.id.acvoltage4, R.id.sitename5, R.id.frequency5, 
          R.id.accurrent5, R.id.acvoltage5, R.id.sitename6, R.id.frequency6, 
          R.id.accurrent6, R.id.acvoltage6, R.id.sitename7, R.id.frequency7, 
          R.id.accurrent7, R.id.acvoltage7}); 
      // updating listviews 
      setListAdapter(adapter); 



     } 
    } 

    public static ThirdFragment newInstance(String text) { 

    ThirdFragment f = new ThirdFragment(); 
    Bundle b = new Bundle(); 
    b.putString("msg", text); 

    f.setArguments(b); 

    return f; 
} 
} 
+0

你可能需要做的,而不是使用'SimpleAdapter'你'ListAdapter adapter'您的自定義適配器(onPostExecute'的'最後一部分) – Aprian

回答

0

您可以檢查我的listadapter,我已經實現了相同的概念,設置文字的顏色有兩種方式

private class CustomerAdapter extends ArrayAdapter<RouteTrackerUser> 
{ 
    Context context; 
    int textViewResourceId; 
    private ArrayList<RouteTrackerUser> originalitems; 
    public CustomerAdapter(Context context, int textViewResourceId, ArrayList<RouteTrackerUser> items) { 
      super(context, textViewResourceId, items); 
      this.textViewResourceId = textViewResourceId; 
      this.context = context; 
      this.originalitems = items; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
       v = inflater.inflate(textViewResourceId, parent, false); 
      } 
      final RouteTrackerUser r = originalitems.get(position); 
      if (r != null) { 
        TextView textName= (TextView) v.findViewById(R.id.sname); 
        TextView postal=(TextView) v.findViewById(R.id.postal); 
        if(r.getVisited().equals("true")) 
        { 
        if(textName != null) 
        { 
         textName.setTextColor(getResources().getColor(R.color.lightpurple)); 
         textName.setText(r.getStorename()); 
        } 
        if(postal != null) 
        { 
         postal.setTextColor(getResources().getColor(R.color.lightpurple)); 
         postal.setText("Address: "+r.getPostalcode()); 
        } 
        } 
        if(r.getVisited().equals("false")) 
        { 
         if(textName != null) 
         { 
          textName.setTextColor(getResources().getColor(R.color.darkpurple)); 
          textName.setText(r.getStorename()); 
         } 
         if(postal != null) 
         { 
          postal.setTextColor(getResources().getColor(R.color.darkpurple)); 
          postal.setText("Address: "+r.getPostalcode()); 
         } 
        } 
        if(RouteTrackerApp.sharedPreference.getInt(RouteTrackerApp.SHARED_ACCESS_UTYPE, 0) == Constants.VAR_TYPE_SALES_PERSON){ 
         v.setOnClickListener(new OnClickListener() { 
         public void onClick(View v) { 
          // TODO Auto-generated method stub 
          viewdata(r.getUsername(),r.getStoreid(),r.getAccountid(),r.getUid(),r.getVisitplanid(),r.getVisited(),r.getStorename()); 
         }}); 
        } 
      } 
      return v; 
    } 
    } 

和PostExecute我叫這個適配器如

protected void onPostExecute(ArrayList<RouteTrackerUser> result) 
{ 
    if(result!=null) 
    { 
// System.out.println("User with result===>"+result); 
    /*for(RouteTrackerUser r :result){ 
     System.out.println("StoreName===>"+r.getStorename());}*/ 
     Intent intent=getIntent(); 
     TextView username=(TextView)findViewById(R.id.usrname); 
     final String sname=intent.getExtras().getString("user_name"); 
     username.setText("Welcome, "+sname); 
     TextView todayvisits=(TextView)findViewById(R.id.todaylist); 
     todayvisits.setText("TODAY'S STORE VISITS"); 
     userAdapter = new CustomerAdapter(ViewYourPlanList.this, R.layout.view_your_plan_list,result); 
     userList = (ListView) findViewById(R.id.listview); 
     userList.setItemsCanFocus(false); 
     userList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     userList.setAdapter(userAdapter); 
     dialog.dismiss(); 
     } 

其他 如果您想要impl列表片段!設計一個customadapter並設置文本顏色,如下所示,並在postexecute上調用這個customadapter。

public class CustomAdapter extends ArrayAdapter<Model> { 
private final LayoutInflater mInflater; 
public CustomAdapter(Context context) { 
    super(context, android.R.layout.simple_list_item_2); 
    mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

public void setData(List<Model> data) { 
    clear(); 
    if (data != null) { 
     for (Model appEntry : data) { 
      System.out.println("cust adptr set data: "+appEntry.getStorename()); 
      add(appEntry); 
     } 
    } 
} 

/** 
* Populate new items in the list. 
*/ 
@SuppressLint("SimpleDateFormat") 
@Override public View getView(int position, View convertView, ViewGroup parent) { 
    View view; 

    if (convertView == null) { 
     view = mInflater.inflate(R.layout.view_your_plan_weekly, parent, false); 
    } else { 
     view = convertView; 
    } 

    Model item = getItem(position); 
    int dayVal = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1; 
    ImageView arrow=(ImageView)view.findViewById(R.id.arrow_image); 
    arrow.setVisibility(View.INVISIBLE); 
    if(item.getTabId().equals(String.valueOf(dayVal))){ 
     arrow.setVisibility(View.VISIBLE); 
    } 
    if(item.getVisited().equals("true")) 
    { 
     ((TextView)view.findViewById(R.id.sname)).setText(item.getStorename()); 
     ((TextView)view.findViewById(R.id.postal)).setText(item.getPostal()); 
     ((TextView)view.findViewById(R.id.sname)).setTextColor(Color.parseColor("#D397D4")); 
     ((TextView)view.findViewById(R.id.postal)).setTextColor(Color.parseColor("#D397D4")); 
    } 
    if(item.getVisited().equals("false")) 
    { 
    ((TextView)view.findViewById(R.id.sname)).setText(item.getStorename()); 
    ((TextView)view.findViewById(R.id.postal)).setText(item.getPostal()); 
    ((TextView)view.findViewById(R.id.sname)).setTextColor(Color.parseColor("#9B419B")); 
    ((TextView)view.findViewById(R.id.postal)).setTextColor(Color.parseColor("#9B419B")); 
    } 
    return view; 
} 

}

+0

我的解決方案是非常有用的,或只是問嘗試此方法後,你需要什麼?> – arun

+0

看起來這是不是我想要的,因爲我只是想改變取決於應用程序從數據庫中獲得值特定文字的文本顏色。 – Lemuel

+0

其實我很抱歉,但我不明白你的代碼。以及我是一個新手。 :/希望你仍然可以幫助我。 – Lemuel

0

除去這部分的SimpleAdapter代替

ListAdapter adapter = new SimpleAdapter(
       getActivity(), contactList, 
       R.layout.list_item, new String[] { TAG_SITENAME1, TAG_FREQUENCY1, TAG_ACCURRENT1, 
       TAG_ACVOLTAGE1,TAG_SITENAME2, TAG_FREQUENCY2, TAG_ACCURRENT2, 
       TAG_ACVOLTAGE2,TAG_SITENAME3, TAG_FREQUENCY3, TAG_ACCURRENT3, 
       TAG_ACVOLTAGE3, TAG_SITENAME4, TAG_FREQUENCY4, TAG_ACCURRENT4, 
       TAG_ACVOLTAGE4, TAG_SITENAME5, TAG_FREQUENCY5, TAG_ACCURRENT5, 
       TAG_ACVOLTAGE5, TAG_SITENAME6, TAG_FREQUENCY6, TAG_ACCURRENT6, 
       TAG_ACVOLTAGE6, TAG_SITENAME7, TAG_FREQUENCY7, TAG_ACCURRENT7, 
       TAG_ACVOLTAGE7}, 
       new int[] { R.id.sitename1, R.id.frequency1, 
         R.id.accurrent1, R.id.acvoltage1, R.id.sitename2, R.id.frequency2, 
         R.id.accurrent2, R.id.acvoltage2, R.id.sitename3, R.id.frequency3, 
         R.id.accurrent3, R.id.acvoltage3, R.id.sitename4, R.id.frequency4, 
         R.id.accurrent4, R.id.acvoltage4, R.id.sitename5, R.id.frequency5, 
         R.id.accurrent5, R.id.acvoltage5, R.id.sitename6, R.id.frequency6, 
         R.id.accurrent6, R.id.acvoltage6, R.id.sitename7, R.id.frequency7, 
         R.id.accurrent7, R.id.acvoltage7}); 
     // updating listviews 
     setListAdapter(adapter); 

您使用customadapter。在CustomAdapter您可以設置文本顏色爲我以前的post.Just顯示我去通過類「公共類CustomAdapter擴展ArrayAdapter」

+0

感謝您的幫助,但我發現已經是我的問題的答案:通過將查看getview函數放在simpleadapter上。無論如何,感謝您提供想法和提示。 :) – Lemuel

+0

歡迎:) – arun