0
below is my mainactivity java code
i have a json file which returns the foll values

[{"COMPONENT":"xxxx","FLAG":"T","SAMPLINGPOINT":"dddd"},{"COMPONENT":"yyyyy","FLAG":"F","SAMPLINGPOINT":"ggggg"},{"COMPONENT":"xxxx","FLAG":"F","SAMPLINGPOINT":"dddd"},{"COMPONENT":"xxxx","FLAG":"T","SAMPLINGPOINT":"dddd"}] On display if the json value FLAG:T then the listview color should be red else if FLAG:F then green...any help??

package com.webservice1;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONObject;
//import org.json.JSONArray;
import org.json.JSONException;
//import org.json.JSONObject;

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

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class MainActivity extends Activity {

    String TAG = "Response";
    TextView responseText;
    Button bt;
    EditText celcius;
    String getCel;
    SoapPrimitive resultString;
    String color, item;
    String str = "<font color=\"%s\">%s</font>";
   //private static final String name = "ID_NUMERIC";
   // private static final String test = "TEST";
    private static final String component = "COMPONENT";
   // private static final String units = "UNITS";
    private static final String value = "VALUE";
    private static final String flag = "FLAG";
    //private static final String color = "color";
    //private static final String samplingpoint = "SAMPLINGPOINT";
    JSONArray Response = null;
    String jsonObject_string ;
    String jArray;
    ListView list;
    TextView vers;
    TextView name;
    TextView flg;
    TextView api;
    ListAdapter adapter;
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

    public void calculate() {
        String SOAP_ACTION = "";
        String METHOD_NAME = "";
        String NAMESPACE = "";
        String URL = "";

        try {
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            Request.addProperty("SmapleID", getCel);

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport = new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);
            resultString = (SoapPrimitive) soapEnvelope.getResponse();

            //Log.i(TAG, "Result SmapleID: " + resultString);
        } catch (Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       // ListView list = (ListView) findViewById(R.id.list);
        responseText = (TextView) findViewById(R.id.responseText);
        bt = (Button) findViewById(R.id.bt);
        celcius = (EditText) findViewById(R.id.cel);
        list = (ListView) findViewById(R.id.list);


        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getCel = celcius.getText().toString();
                AsyncCallWS task = new AsyncCallWS();
                task.execute();


            }
        });

    }

    private class AsyncCallWS extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected String doInBackground(String... params) {
            Log.i(TAG, "doInBackground");
            calculate();
            return null;
        }



        @Override
        protected void onPostExecute(String result) {
            //calculate();

            SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,R.layout.list_child,new String[] {component, value, flag, color }, new int[] {R.id.name, R.id.vers, R.id.flg});
            list.setAdapter(adapter);    

             oslist.clear();

            // Making a request to url and getting response
            String data = "";
            String data1 = "";
            String data2 = "";
            String data3 = "";
            String data4 = "";
            String data5 = "";
            String jsonStr = resultString.toString();

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

            if (jsonStr != null) {

              try 

            {

              JSONArray jArray = new JSONArray(jsonStr);

            for(int i=0;i<jArray.length();i++){
                JSONObject json_obj = jArray.getJSONObject(i);

               // String name = json_obj.getString("ID_NUMERIC");
               // String test = json_obj.getString("    TEST");
                String component = json_obj.getString("COMPONENT");
                //String units = json_obj.getString("UNITS");
                String value = json_obj.getString("VALUE");
                String flag = json_obj.getString("FLAG");
               if ("F".equalsIgnoreCase(flag));{
                   color = "red";
               }
                //data = name;
               // data1 = test + ", ";
                data2 = component;
               // data3 = units;
                data4 =  value;
                data =  flag;

                HashMap<String, String> map = new HashMap<String, String>();
                map.put("COMPONENT", data2);
                map.put("VALUE", data4);
                map.put("FLAG", data);

                oslist.add(map);

        }
            }catch (JSONException e) {
                // handle exception
            }

        }
          // return null;
        }

    }}
guychouk
  • 671
  • 1
  • 8
  • 28
  • You are waaay better of using a custom adapter. Take a look at this please http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – TheRealChx101 Nov 16 '15 at 08:11
  • can you help me using the code above plzzz..I am a newbie...can you help me as to how and where i can put the custom adapter in the code above?? – Thunder Goli Nov 16 '15 at 08:13
  • I had a look at the link you sent above..guess it doesn't serve my purpose – Thunder Goli Nov 16 '15 at 08:24

0 Answers0