Listview is showing nothing

Asked

Viewed 42 times

1

I have the class answer

public class Resposta {
    private String Nome;
    private boolean Certo_Errado;

    public Resposta(String nome, boolean certo_Errado) {
        Nome = nome;
        Certo_Errado = certo_Errado;
    }

    public String getNome() {
        return Nome;
    }

    public void setNome(String nome) {
        Nome = nome;
    }

    public boolean isCerto_Errado() {
        return Certo_Errado;
    }

    public void setCerto_Errado(boolean certo_Errado) {
        Certo_Errado = certo_Errado;
    }  

The custom Adapter

public class Resposta_Adapter extends ArrayAdapter<Resposta> {

    private final Context context;
    private final List<Resposta> Respostas;

    public Resposta_Adapter(Context context, List<Resposta> respostas) {
        super(context, R.layout.list_respostas);
        this.context = context.getApplicationContext();
        this.Respostas = respostas;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_respostas, parent, false);

        Resposta rsp = Respostas.get(position);

        TextView tc = view.findViewById(R.id.txt);
        tc.setText(rsp.getNome());

        return rowView;
    }
}

And in the main activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Resposta> oRespostas = new ArrayList<Resposta>();


        for (int i = 0; i < 5; i++) {
            boolean a = false;
            if (i == 2)
                a = true;
            else
                a = false;
            oRespostas.add(new Resposta("Resposta " + i, a));
        }

        ListView lista = (ListView) findViewById(R.id.lista);

        Resposta_Adapter adapter;
        adapter = new Resposta_Adapter(getApplicationContext(), oRespostas);

        lista.setAdapter(adapter);


    }
}

But listview is not being filled in. I can’t see where it might be wrong

Edit: XML of the main screen: `

<TextView
    android:id="@+id/txt_Pergunta"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Padrao"
    android:textAlignment="center"
    android:textSize="18sp"
    android:textStyle="italic" />

<ListView
    android:id="@+id/lista"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/txt_Pergunta"
    />

`

  • TextView tc = rowView.findViewById(R.id.txt); should resolve. Note that what you are doing in the method getView() is inefficient. Consider using Viewholder Pattern

  • @ramaral did not work

  • put the listview screen code

  • try using this instead of getApplicationContext()

  • @Weslleybarbosa tried and it didn’t work.

1 answer

0

Try using this method for getView

@Override
public View getView(int position, View view, ViewGroup parent) {

    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_respostas, parent, false);

    Resposta rsp = Respostas.get(position);

    TextView tc = view.findViewById(R.id.txt);
    tc.setText(rsp.getNome());

    return view;
}
  • same thing, shows nothing

  • Put the whole screen code

Browser other questions tagged

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