0

I want to show background color inside row in listview I send color code from PHP server for particular a row which is check condition and shows the background color on a row in the listview. I'm trying and spend 2 days but I didn't get anything.

public class PFLFragment extends Fragment {
ArrayList<StatusList> statusList = new ArrayList<>();
ListView listv;
TextView idview1;
TextView textview2;
TextView textview3;
TextView textview4;
String customer_id = " ";
AdapterList adap;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View vv = inflater.inflate(R.layout.mystatus_left_tab, null);
    listv = (ListView) vv.findViewById(R.id.listleft);
    listv.setDivider(null);
    listv.setDividerHeight(0);
  statusList.add(new StatusList("customerusername", "customername",  "enrolldate", "package", "color"));
    adap = new myadap();
    listv.setAdapter(adap);

    try {
        JSONObject jobj = new JSONObject(apath.getLoginData());
        customer_id = jobj.getJSONObject("userdata").getString("customer_id");
        new StatusListTask().execute("my url");


    } catch (Exception e) {
        Log.e("error", e.toString());
    }
    return vv;
}

class AdapterList extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return statusList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vv = getActivity().getLayoutInflater().inflate(R.layout.mystatus_left_tab_list, null);
        LinearLayout ll = (LinearLayout) vv.findViewById(R.id.resultadaplayout);
        idview1= (TextView) vv.findViewById(R.id.customerid);
        textview2 = (TextView) vv.findViewById(R.id.customername);
        textview3 = (TextView) vv.findViewById(R.id.enrolldate);
        textview4 = (TextView) vv.findViewById(R.id.packages);


        StatusList res = statusList.get(position);

        //int color = Color.rgb(234, 80, 0);
        int color = getActivity().getResources().getColor(R.color.startcolor);
        //    int color1 = getActivity().getResources().getColor(R.color.textcolor);
        // int color2 = getActivity().getResources().getColor(R.color.redcolor);
        // int color=Color.parseColor("color");

        if (position == 0) {

            ll.setBackgroundColor(color);
            idview1.setGravity(Gravity.CENTER);
            idview1.setPadding(20, 10, 20, 10);
            idview1.setTextColor(Color.WHITE);
            idview1.setBackgroundColor(color);
            idview1.setTextSize(13);

            idview1.setText(res.getCustomerid());

            textview2.setGravity(Gravity.CENTER);
            textview2.setPadding(20, 10, 20, 10);
            textview2.setTextColor(Color.WHITE);
            textview2.setBackgroundColor(color);
            textview2.setTextSize(13);

            textview2.setText(res.getCustomername());

            textview3.setGravity(Gravity.CENTER);
            textview3.setPadding(20, 10, 20, 10);
            textview3.setTextColor(Color.WHITE);
            textview3.setBackgroundColor(color);
            textview3.setTextSize(13);
            textview3.setText(res.getEnrolldate());

            textview4.setGravity(Gravity.CENTER);
            textview4.setPadding(20, 10, 20, 10);
            textview4.setTextColor(Color.WHITE);
            textview4.setBackgroundColor(color);
            textview4.setTextSize(13);
            textview4.setText(res.getPakage());

        } else {
            int a = 12;
            ll.setBackgroundColor(Color.WHITE);

            idview1.setGravity(Gravity.CENTER);
            idview1.setPadding(10, 10, 10, 10);
            idview1.setTextSize(a);
            idview1.setText(res.getCustomerid());

            textview2.setGravity(Gravity.CENTER);
            textview2.setPadding(10, 10, 10, 10);
            textview2.setTextSize(a);
            textview2.setText(res.getCustomername());

            textview3.setGravity(Gravity.CENTER);
            textview3.setPadding(10, 10, 10, 10);
            textview3.setTextSize(a);
            textview3.setText(res.getEnrolldate());

            textview4.setGravity(Gravity.CENTER);
            textview4.setPadding(10, 10, 10, 10);
            textview4.setTextSize(a);
            textview4.setText(res.getPakage());


        }
        return vv;
    }

}

void showMessage(String msg) {
    final AlertDialog alert;
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    alert = builder.create();
    alert.setCancelable(false);
    View vv =  getActivity().getLayoutInflater().inflate(R.layout.ordersuccess, null);
    Button btncontinue = (Button) vv.findViewById(R.id.btn_forgot);
    final TextView message = (TextView) vv.findViewById(R.id.message);
    message.setText(msg);
    btncontinue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alert.cancel();
        }
    });
    alert.setView(vv);
    alert.show();
}

class StatusListTask extends AsyncTask<String, Void, String> {
    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(getActivity());
        pd.setTitle("loading...");
        pd.setCancelable(false);
        pd.show();

    }

    @Override
    protected String doInBackground(String... params) {
        String result = "";
        try {

            JSONObject jobj = new JSONObject();

            jobj.put("uid", customer_id);
            URL object = new URL(params[0]);
            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestMethod("POST");

            OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
            Log.e("jdata", jobj.toString() + "");
            wr.write(jobj.toString());
            wr.flush();
            con.connect();

            StringBuilder sb = new StringBuilder();
            int HttpResult = con.getResponseCode();
            if (HttpResult == HttpURLConnection.HTTP_OK) {
                InputStream is = con.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, "utf-8");
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                isr.close();
                br.close();
                result = sb.toString();
                con.disconnect();
            } else
                Log.e("Httpresponse", HttpResult + "");


        } catch (Exception e) {
            Log.e("Error mission", e.toString());
        }

        return result;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pd.cancel();
        Log.e("result mission", s + "      ===");
        try {
            JSONObject jobj = new JSONObject(s);
            if (!jobj.optString("output").equalsIgnoreCase("")) {
                JSONObject jo = jobj.optJSONObject("output");
                JSONArray leftarr = jo.optJSONArray("leftuser");
                for (int i = 0; i < leftarr.length(); i++) {
                    JSONObject obl = leftarr.optJSONObject(i);
                    statusList.add(new StatusList(obl.optString("customerusername"), obl.optString("customername"),
                            obl.optString("enrolldate"), obl.optString("package"), obl.optString("color")));

                }

                adap.notifyDataSetChanged();
            } else {
                showMessage("Data not exist here");
            }
        } catch (Exception e) {
            Log.e("ER post mission", e.toString());
        }
    }
}

}

M_sahoo26
  • 1
  • 1
  • in what format and value do u recieve color from server, is it in RGB format ? – Ak9637 Oct 28 '16 at 07:09
  • no, it's string format and sorry i didn't mention about json i got like that from server {"customerusername":"444erd43","customerid":"788","customername":"SATISH KKR","enrolldate":"2015-09-14","package":"Small","color":"green"} – M_sahoo26 Oct 28 '16 at 07:17
  • 2
    you shoud use RGB value to set color , instead of the string ; or you need to have a method , converting the string to its corresponding RGB value,or you can even use strings.xml to save the values of the color u expect to get from server and according to your string value you should get the color values which you need to have saved in your colors values xml value , and then set your background – Ak9637 Oct 28 '16 at 07:19
  • Currently, I guess you are using static color from Color.xml file and setting it like this ll.setBackgroundColor(color); Is this working? When you send it through PHP, you can set it using, ll.setBackgroundColor(Color.parseColor(BackgroundColorInHashCode())); – Myth Oct 28 '16 at 07:26
  • @M_sahoo26 , i have posted an example as your answer below , hope it helps you understand – Ak9637 Oct 28 '16 at 07:27
  • or you can use reflection to get a list of all colors, defined in java.awt.Color (http://stackoverflow.com/a/8125171/6635287) and then serach in list for the current color – Radinator Oct 28 '16 at 07:28

2 Answers2

0

For example : If you receive color=green;

You can get

getResources.getString(R.string."your color value here");

and you should have a string resources in strings.xml like

<string name="your color name">@color/colorGreen</string> 

and the values of this color be defined in ur colors.xml like

<color name="colorGreen">#DF201A</color>
Radinator
  • 1,048
  • 18
  • 58
Ak9637
  • 990
  • 6
  • 12
  • sorry it's not working , i want to read that color value from json – M_sahoo26 Oct 28 '16 at 09:42
  • in my above code ,i did not mention that condition from server when user exist then display in row background green color if newuser then display in row red color in side listview – M_sahoo26 Oct 28 '16 at 10:29
0

Pass your list in constructor of BaseAdapter, then call notifydataseChanged

    class AdapterList extends BaseAdapter {

    ArrayList<StatusList> statusList;

    public BaseAdapter(ArrayList<StatusList> statusList) {
    this.statusList = statusList;
    }

}
Nidhi
  • 777
  • 7
  • 17