gpt4 book ai didi

java - ListView 不显示在屏幕上

转载 作者:行者123 更新时间:2023-12-02 05:51:13 26 4
gpt4 key购买 nike

我最近开始在 Android 应用程序上使用 ListView,但我没有让它显示在屏幕上。

我看到了很多关于如何制作自定义 Adapter 的教程,并逐行进行了操作,但最终我无法使 ListView 显示在在我的屏幕上,我什至让一个 TextView 中的固定文本显示在 ListView 上,但它仍然没有显示。

这是我的实体代码的构造函数:

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

这是我的自定义适配器,它扩展了“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;
}

这是具有 ListView 的布局“activity_atividade02”,以及我不会在此处显示的其他一些 TextView 。

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

这是布局“layout_classificacao”,将由适配器填充

<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>

这是主类“Atividade02”一切开始

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);

}
}

继续,ListView 没有显示“ClassificacaoAdapter”,而我希望它能够显示。

最佳答案

确保适配器具有方法getItemcount()并且它返回classificacaoList.size()。它不应返回 0

关于java - ListView 不显示在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com