It turns out that I have a list where certain football matches and their phases are displayed, when it comes to displaying and showing its elements is perfect, the problem occurs in the following since each match contains a phase field like this:
[
{
"id": 1,
"equipo1_id": 1,
"equipo2_id": 2,
"fecha_hora": "...",
"fase": "Grupales",
...
},
{
"id": 7,
"equipo1_id": 3,
"equipo2_id": 9,
"fecha_hora": "...",
"fase": "Eliminacion",
...
}
]
I use that field "fase":
and I show it in a TextView to know which match corresponds to each type of match, so that they have an idea the list is something like this.
Grupales //<-- TextView
Equipo 1 vs Equipo 2
Grupales
Equipo 1 vs Equipo 2
Eliminacion
Equipo 1 vs Equipo 2
Eliminacion
Equipo 1 vs Equipo 2
What I want to achieve is to make it look like this:
Grupales //<-- TextView
Equipo 1 vs Equipo 2
Equipo 1 vs Equipo 2
...
...
Eliminacion
Equipo 1 vs Equipo 2
Equipo 1 vs Equipo 2
...
The idea is to hide the TextView with a .setVisibility()
. But what would be the correct way to implement this? Should I use the value that returns fase
and compare it with the other elements?
This would be the Asynchronous method to list the matches and make the Adapter set:
public class programacion extends AsyncTask<String, Void, Boolean> {
JSONArray results = new JSONArray();
@Override
protected Boolean doInBackground(String... strings) {
try {
results = WebService.listaProgramacion(token);
return true;
}
catch (JSONException e) {
Log.e("JSONException",e.getMessage());
e.printStackTrace();
}
catch (IOException e) {
Log.e("IOException", e.getMessage());
e.printStackTrace();
}
return false;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean success) {
super.onPostExecute(success);
dialog.dismiss();
if (success){
lista_partidos.setAdapter(new AdapterPartidos(getActivity(), R.layout.partidos_item_list, results));
lista_partidos.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
}
And this is my Adapter:
public class AdapterPartidos extends ArrayAdapter<JSONObject> {
JSONArray data;
Context contexto;
LayoutInflater inflater;
public AdapterPartidos(@NonNull Context context, int resource, JSONArray data) {
super(context, resource);
this.data = data;
this.contexto = context;
}
class ViewHolder {
TextView fase;
TextView equipo1;
TextView equipo2;
}
@Override
public int getCount() {
return this.data.length();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder v = null;
if(convertView == null) {
inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.partidos_item_list, null, false);
v = new ViewHolder();
v.fase = (TextView)convertView.findViewById(R.id.fase);
v.equipo1 = (TextView)convertView.findViewById(R.id.equipo1);
v.equipo2 = (TextView)convertView.findViewById(R.id.equipo2);
convertView.setTag(v);
}
else {
v = (ViewHolder)convertView.getTag();
}
try {
JSONObject content = data.getJSONObject(position);
v.fase.setText(content.getString("fase"));
v.equipo1.setText(content.getJSONObject("equipo1").getJSONObject("equipo").getString("nombre"));
v.equipo2.setText(content.getJSONObject("equipo2").getJSONObject("equipo").getString("nombre"));
}
catch (JSONException e) {
e.printStackTrace();
}
return convertView;
}