I want to change the cell of a color ListView according to an Android Studio paramenter

1

I have a couple of problems with the ListView

1) In the ListView there are subjects and I want that if they have a warning they are marked with a different color, either changing the background or the colored letters. I receive the parameter in a JSONArray. And if this parameter "avis" has value "1" the background of that cell is of another color. I hope you can help me.

2) Every time I click on an item in the ListView, I go back and when I click again on another item, the information of the previous one is loaded. I tried using "notifydatachanged" but I can not get it to work for me.

GRACIAAAAAS

public class Subjects_list extends AppCompatActivity implements AdapterView.OnItemClickListener {
public static final String ip = "192.168.43.110";
public static Subjects ls;
private List<HashMap<String, String>> SubjectList = new ArrayList<>();
private ListView mListView;
private ProgressDialog pDialog;

public static final String EXTRA_MESSAGE = "";
private static final String KEY_FULLNAME = "Nom complet";
private static final String KEY_INITIALS = "Inicials";
private static final String KEY_CREDITS = "Credits";
private static final String KEY_COORDINADOR = "Coordinador";
public String avis ="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_subjects_list);
    Intent intent = getIntent();
    String message = intent.getStringExtra(Portal.EXTRA_MESSAGE);
    this.setTitle("LA TEVA MATRICULA");
    mListView = (ListView) findViewById(R.id.subjectslist);
    mListView.setOnItemClickListener(this);
    new getSubjectslistAsynTask(this).execute(message);


}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent intent = new Intent(this, Avis.class);
    TextView Initials = (TextView) findViewById(R.id.initials);
    String InitialsValue = Initials.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, InitialsValue);
    mListView.deferNotifyDataSetChanged();
    startActivity(intent);
}

private class getSubjectslistAsynTask extends AsyncTask<String, Void, String> {
    Context context;
    InputStream stream = null;
    String str = "";

    private getSubjectslistAsynTask(Context context) {
        this.context = context;
    }


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

    }
    @Override
    protected String doInBackground(String... urls) {

        try {
            String str = loadFromNetwork(urls[0]);
            JSONArray jsonArray = new JSONArray(str);


            if ( str != null){

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject s = jsonArray.getJSONObject(i);
                    String initials = s.getString("initials");
                    String fullname = s.getString("fullname");
                    String credits = s.getString("credits");
                    String coordinator = s.getString("coordinator");
                    avis = s.getString("avis");

                    HashMap<String, String> subject = new HashMap<>();

                    subject.put("initials",initials);
                    subject.put("fullname",fullname);
                    subject.put("credits",credits);
                    subject.put("coordinator",coordinator);
                    //subject.put("avis",avis);

                    SubjectList.add(subject);


                }

            }

        } catch (IOException e) {
            return e.getMessage();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    return null;
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();

       // onLoaded(ls.listsubjects);


            ListAdapter adapter = new SimpleAdapter(
                    Subjects_list.this, SubjectList,
                    R.layout.activity_list_subjectsuser, new String[]{"initials","fullname",
                    "credits","coordinator","avis"}, new int[]{
                    R.id.initials,R.id.fullname, R.id.credits, R.id.coordinator,R.id.avis});

            mListView.setAdapter(adapter);





    }

}
    
asked by Alba Egea Cadiz 17.01.2018 в 15:20
source

1 answer

1

You must definitely use a Adapter with custom elements (not a SimpleAdapter) that in this case would be the elements of the list.

As an example the answer of @sioesi in which it indicates how to create a CustomAdapter :

Listview with an icon and text

and within the method getView() of your Adapter depending on the property of JSONObject paint the background the cell of one color or another:

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = getLayoutInflater();
    View view = convertView;

    ...
    ...
    ...

    JSONObject s = jsonArray.getJSONObject(position);
    String avis = s.getString("avis");


    if (avis.equals("1") {
        view.setBackgroundColor(Color.BLUE);
    } else {
        view.setBackgroundColor(Color.RED);
    }

    return view;

}
    
answered by 17.01.2018 в 17:36