android - 未在 fragment 中调用 baseadapter 的 getview() 方法

标签 android xml listview android-fragments

我正在使用 fragment 来显示列表。 ListAdapter 使用 Baseadapter。它返回大于 0 的 getcount 但仍未调用 getview() 方法。

另外,我在 fragment 中设置了适配器并调用了 listadapter,如下所示:

mListAdapter = new MyListAdapter(getActivity(), data, enum);
sList.setAdapter(mListAdapter); 
mListAdapter.notifyDataSetChanged();  

适配器被调用没有问题并且 GetCount() 也被调用但是 GetView() 未被调用。

列表适配器:

public class MyListAdapter extends BaseAdapter {

private ArrayList<WatchVO> data;
private static LayoutInflater inflater;
private Context mcontext;
private ENUM enum;

public String TAG = "MyListAdapter";


public MyListAdapter(Context context, ArrayList<WatchVO> data,
        ENUM enum) {
   // super(context,0,data);

    this.mcontext = context;
    this.data = data;
    this.enum = enum;
    inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() { Log.d(TAG,"count"+data.size());
    if (data.size()<=0)
        return 1;
    return data.size();
}

@Override
public Object getItem(int position) {
    Log.d(TAG,"getitem"+data.get(position));
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
        if(convertView==null) {
            holder = new ViewHolder();

            Log.d(TAG,""+parent +"pos" + position);
        convertView = inflater.inflate(R.layout.single_row_scripts,parent,false);
            holder.tableRow = (TableRow) convertView.findViewById(R.id.table_row_item);
            holder.priceChangeContainer = (LinearLayout) convertView.findViewById(R.id.color_container_red_green);
            holder.sName = (TextView) convertView.findViewById(R.id.sName);
            holder.sPrice = (TextView) convertView.findViewById(R.id.sMRP);
            holder.sPriceChange = (TextView) convertView.findViewById(R.id.sPrice);
            holder.sPricePercentageChange = (TextView) convertView.findViewById(R.id.sPriceChange);
            holder.sVolume = (TextView) convertView.findViewById(R.id.sVolume);

            holder.sName.setText(data.get(position).getSName());
            holder.sPrice.setText(data.get(position).getSMRP());
            holder.sPriceChange.setText(data.get(position).getSChange());
            holder.sPricePercentageChange.setText(data.get(position).getSPercentageChange());


            convertView.setTag(holder);
     }
    else {
    holder = (ViewHolder) convertView.getTag();
}
    return convertView;
}
public static class ViewHolder {
    public TextView sName, sPrice, sPriceChange, sPricePercentageChange, sVolume;
    public LinearLayout priceChangeContainer;
    public TableRow tableRow;
}
}

最佳答案

您将错误的适配器传递给 listView。更改代码

mListAdapter = new MyListAdapter(getActivity(), data, enum); 
sList.setAdapter(marketMoversListAdapter); 

mListAdapter.notifyDataSetChanged();

mListAdapter = new MyListAdapter(getActivity(), data, enum); 
sList.setAdapter(mListAdapter); 

mListAdapter.notifyDataSetChanged();

关于android - 未在 fragment 中调用 baseadapter 的 getview() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45371721/

相关文章:

java - Android - 本地数据库(SQLite)与内部类(通过单例)

java - 一段时间后如何做do/while?

xml - Alamofire XML解析错误-1017

C# WPF ListView动态个性化每一行的字体大小

c# - 从数据集中的数据表中删除一行

android - 如何绕过 TextView 元素的文本限制?

android - LinearLayout 水平在真实设备上显示不正确

php - 在 PHP 中将关联数组转换为 XML

xml - Logstash,从多个文档中的 xml 文件拆分事件,保留根标签中的信息

android - 原生 Android 4.0 应用程序如何具有快速滚动的 ListViews?