baseadapter的getview()方法未在片段中调用

时间:2017-07-28 10:59:13

标签: android xml listview android-fragments

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

此外,我在片段中设置了适配器并调用了listadapter,如:

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

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

ListAdapter:

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

2 个答案:

答案 0 :(得分:0)

你的错误适配器传递给listView。改变你的代码

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

mListAdapter.notifyDataSetChanged();

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

mListAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

更改您的代码,如下所示:

 if (convertView == null) {
             convertView = inflater.inflate(R.layout.single_row_scripts,null);
        tableRow = (TableRow) convertView.findViewById(R.id.table_row_item);
        priceChangeContainer = (LinearLayout) convertView.findViewById(R.id.color_container_red_green);
        sName = (TextView) convertView.findViewById(R.id.sName);
        sPrice = (TextView) convertView.findViewById(R.id.sMRP);
        sPriceChange = (TextView) convertView.findViewById(R.id.sPrice);
        sageChange = (TextView) convertView.findViewById(R.id.sChange);
        sVolume = (TextView) convertView.findViewById(R.id.sVolume);

        sName.setText(data.get(position).getSName());
        sPrice.setText(data.get(position).getSMRP());
        sChange.setText(data.get(position).getSChange());
        sChange.setText(data.get(position).getSChange());
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        return convertView;