Add Jsonobject to List<Discipline>

Asked

Viewed 103 times

1

I’m having problems when I add data to a List. I have the following code that takes the return of a Jsonobject and adds it to a List. Then I move on to the InicialAdapter.java

DisciplinaTask asyncTask = (DisciplinaTask) new DisciplinaTask(new DisciplinaTask.AsyncResponse() {
        @Override
        public void processFinish(String output) {

            try {


                JSONObject jsonObject = new JSONObject(output);
                JSONArray jsonArray = jsonObject.getJSONArray("RETORNO");

                List<Disciplina> disciplinaList = new ArrayList<Disciplina>();


                for (int i=0; i<jsonArray.length(); i++) {

                    JSONObject retorno = jsonArray.getJSONObject(i);

                    Disciplina disciplinaModel = new Disciplina();

                    disciplinaModel.setNomeMateria(retorno.getString("nome"));

                    disciplinaList.add(disciplinaModel);

                }

                InicialAdapter adapter = new InicialAdapter(InicialActivity.this, disciplinaList);
                //AQUI NÓS SÓ VINCULAMOS A listaDisciplina ao ADAPTER
                listaDisciplina.setAdapter(adapter);
                //O NOSSO PROPRIO ADAPTER VAI SER RESPONSAVEL POR PEGAR OS DADOS DA DISCIPLINA E CONVERTER EM VIEWS PARA SEREM COLOCADAS DENTRO DA LISTA


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }).execute();

InicialAdapter.java:

package com.example.thiag.studyapp.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.thiag.studyapp.model.Disciplina;
import java.util.List;

public class InicialAdapter extends BaseAdapter{


    private final Context context;
    private final List<Disciplina> disciplinaList;


    //AQUI JÁ TEMOS UM CONSTRUTOR COM A LISTA DE DISCIPLINAS
    public InicialAdapter(Context context, List<Disciplina> disciplinaList) {

        this.context = context;
        this.disciplinaList = disciplinaList;
    }


    //ESTE ADAPTER É RESPONSAVEL POR POPULAR A LINHA DO LISTVIEW DA activity_inicial.xml
    //onde irão aparecer os cardviews

    @Override
    public int getCount() {

        return disciplinaList.size();

    }

    @Override
    public Object getItem(int position) {
        //DEVOLVE A DISCIPLINA NA POSIÇÃO
        return disciplinaList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return disciplinaList.get(position).getId();
    }

    @Override
    //Aqui, esse método vai perguntar para o adapter. Adapter, me devolve aqui uma view para a posição zero. Agora, me devolve a da posição um...
    //Esse método vai ter que saber quantos itens tem para saber a hora de parar. Por isso temos o getCount()
    public View getView(int position, View convertView, ViewGroup parent) {

        TextView view = new TextView(context);

        Disciplina disciplina = disciplinaList.get(position);

        view.setText(disciplinaList.toString());

        return view;
    }
}

However, what is being printed is the following (4 times):

[História, Matemática, Sociologia, Filosofia]
[História, Matemática, Sociologia, Filosofia]
[História, Matemática, Sociologia, Filosofia]
[História, Matemática, Sociologia, Filosofia]

Where am I wrong? Is it in the for, by the time I add in List?

  • You have tried debugging the json content and then the list content?

1 answer

0


I made that little mistake in your chorus

view.setText(disciplinaList.toString());

You called the wrong variable in Textview the correct would be

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

    TextView view = new TextView(context);

    Disciplina disciplina = disciplinaList.get(position);

    //Chama a variavel e o nome da materia
    view.setText(disciplina.getNomeMateria());

    return view;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.