2017-07-28 71 views
2

我正在使用Fragment來顯示列表。 ListAdapter使用Baseadapter。它返回的getcount大於0,但仍未調用getview()方法。baseadapter的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; 
} 
} 
+0

嘿..它會爲一次通話。它不會在晚些時候要求。因爲您每次檢查convertView == null。一旦您將視圖分配給convertview,則convertview不爲null。所以你的情況不會奏效。 –

回答

0

你逝去的錯誤適配器的ListView。更改代碼

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

mListAdapter.notifyDataSetChanged(); 

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

mListAdapter.notifyDataSetChanged(); 
+0

我改變這仍然不行。 –

+0

如果在getView中打印日誌。並檢查是否有日誌打印。 –

+0

試試這個也仍然沒有調用getView()方法。 ( –

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; 
+0

我做了更改,但我的問題沒有解決,我同意只調用一次getview()方法,但有一次也調用getview(),謝謝重播 –

+0

請發佈完整的適配器類? –

+0

請檢查我編輯我的帖子與完整的適配器類 –

相關問題