Search code examples
javaandroidlistviewadapterandroid-arrayadapter

ListView doesn't display on screen


I recently started to use ListView on Android Application and I'm not making it to display at the screen.

I saw many tutorials on how to do a custom Adapter and followed it line by line, but at the end I wasn't able to make the ListView display on my screen, I even let a fixed Text in one TextView to display at the ListView but it still didn't displayed it.

This is the constructor of my entity code:

 public Classificacao(int colocacao, int time, int pontos) {
        this.colocacao = colocacao;
        this.time = time;
        this.pontos = pontos;
    }

This my Custom Adapter that extends "ArrayAdapter"

private Context context;
private ArrayList<Classificacao> classificacaoList;

ClassificacaoAdapter(Context context, ArrayList<Classificacao>         classificacaoList) {
    super(context, R.layout.layout_classificacao, classificacaoList);
    this.context = context;
    this.classificacaoList = classificacaoList;
}

@SuppressLint("SetTextI18n")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null){
        convertView = layoutInflater.inflate(R.layout.layout_classificacao, parent, false);
    }

    TextView colocacao = convertView.findViewById(R.id.texto1);
    colocacao.setText(Integer.toString(classificacaoList.get(position).getColocacao()));

    ImageView imagem = convertView.findViewById(R.id.imagem1);
    imagem.setImageResource(classificacaoList.get(position).getImagem());

    TextView nome = convertView.findViewById(R.id.texto2);
    nome.setText(classificacaoList.get(position).getTime());

    TextView pontos = convertView.findViewById(R.id.texto3);
    pontos.setText(Integer.toString(classificacaoList.get(position).getPontos()));

    return convertView;
}

This the layout "activity_atividade02" that have the ListView, and some others Text views that I won't display here.

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

This is the layout "layout_classificacao" that will be filled by the adapter

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp">

    <TextView
        android:id="@+id/texto1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="30sp" />

    <ImageView
        android:id="@+id/imagem1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:contentDescription="imagem do time"
        app:srcCompat="@drawable/flamengo" />

    <TextView
        android:id="@+id/texto2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/texto3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="30sp" />
</LinearLayout>

And this is the main class "Atividade02" that start everything

public class Atividade02 extends AppCompatActivity {

    ListView listView;

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

        listView = findViewById(R.id.listView);

        ArrayList<Classificacao> classificacaoArrayList = new ArrayList<>();

        classificacaoArrayList.add(new Classificacao(1, 2,46));
        classificacaoArrayList.add(new Classificacao(2, 1,42));
        classificacaoArrayList.add(new Classificacao(3,0,38));
        classificacaoArrayList.add(new Classificacao(4,3,35));
        classificacaoArrayList.add(new Classificacao(5,5,33));
        classificacaoArrayList.add(new Classificacao(6,4,33));

        ArrayAdapter classificacaoAdapter = new ClassificacaoAdapter(this, classificacaoArrayList);

        listView.setAdapter(classificacaoAdapter);

    }
}

Resuming, the ListView isn't displaying the "ClassificacaoAdapter" and I want it to do it.


Solution

  • Well... it was a pretty obvious problem, in the main layout activity_atividade02 there was a LinearLayout above the ListView that it's layout_height was set to match_parent and it was occupying the entire layout and not letting the ListView being displayed.