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