gpt4 book ai didi

java - Android:数组适配器不显示ListView

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

我的适配器:

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewAdapter extends ArrayAdapter<Presenter> {
Context context;
List<Presenter> presenterList;
private LayoutInflater mmInflater;

public NewAdapter(Context context, List<Presenter> data) {
super(context, R.layout.listview_item_row_presenters);
mmInflater = LayoutInflater.from(context);
this.context = context;
this.presenterList = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = mmInflater.inflate(R.layout.listview_item_row_presenters, parent, false);
}


Presenter currentPresenter = presenterList.get(position);


ImageView imageView = (ImageView)itemView.findViewById(R.id.imgIcon);
imageView.setImageBitmap(currentPresenter.getImage());


TextView titleText = (TextView) itemView.findViewById(R.id.txtTitle);
titleText.setText(currentPresenter.getName());


TextView descripText = (TextView) itemView.findViewById(R.id.txtAbout);
descripText.setText(currentPresenter.getInfo());


return itemView;
}
}

调用并将适配器设置为ListView的方法

private void populateListView(List<Presenter> presList) {
ArrayAdapter<Presenter> adapter = new NewAdapter(this.getBaseContext(), presList);
ListView list = (ListView) findViewById(R.id.listViewPres);
list.setAdapter(adapter);
}

我有 Logcat 输出,证明没有数据为空或空,没有错误或任何东西:/我只能猜测这是上下文和充气机的问题,但我不明白为什么

提前谢谢

最佳答案

改变

 super(context, R.layout.listview_item_row_presenters);

super(context,  R.layout.listview_item_row_presenters, data); // add data

当您扩展ArrayAdapter时,构造函数中的super处理listItem大小,并且您需要将列表传递给构造函数方法

示例代码可以参见第2.3节@AndroidListView

关于java - Android:数组适配器不显示ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24097678/

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