[英]notifyDataSetChanged doesn't work with custom adapter and different type of rows
我正在開發一個應用程序,其中的主要任務是顯示不同類型的項目的列表(恰好是4種不同類型的項目)。 為此,我實現了一個自定義適配器,如下所示:
public class NjoftimeAdapter extends BaseAdapter {
final List<NjoftimeItemInterface> rows;
final ArrayList<Njoftime> njoftime;
NjoftimeAdapter(ArrayList<Njoftime> njoftime, Context context) {
this.njoftime = njoftime;
rows = new ArrayList<NjoftimeItemInterface>();// member variable
// iteron mbi objektet e bizneset dhe kontrollon cfare tipi eshte. Ne
// varesi te tipit theret adapterin e tij
for (Njoftime njoftim : njoftime) {
if (njoftim.getType() == NjoftimeKategori.PUNA_IME.ordinal()) {
rows.add(new PunaImeAdapter(LayoutInflater.from(context),
njoftim, context));
}
if (njoftim.getType() == NjoftimeKategori.NJOFTIME_CATEGORY.ordinal()) {
rows.add(new OthersAdapter(LayoutInflater.from(context),
njoftim, context));
}
if (njoftim.getType() == NjoftimeKategori.MAKINA_CATEGORY.ordinal()) {
rows.add(new MakinaAdapter(LayoutInflater.from(context),
njoftim, context));
}
if (njoftim.getType() == NjoftimeKategori.PRONA_CATEGORY.ordinal()) {
rows.add(new PronaAdapter(LayoutInflater.from(context),
njoftim, context));
}
}
}
@Override
public int getViewTypeCount() {
return NjoftimeKategori.values().length;
}
@Override
public int getItemViewType(int position) {
return rows.get(position).getViewType();
}
public int getCount() {
return rows.size();
}
public void updateNjoftimeList(ArrayList<Njoftime> newList){
njoftime.clear();
njoftime.addAll(newList);
this.notifyDataSetChanged();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
return rows.get(position).getView(convertView);
}
}
當它第一次加載項目時,一切似乎都很好,但是當我想加載更多數據並更新listView
沒有任何變化,它只顯示第一次加載的數據。
為了更新列表,我使用了以下代碼:
mAdapter.updateNjoftimeList(njoftime);
在適配器內部調用該方法。
例如,對於每種不同類型的項目,我開發了一段代碼來顯示數據,如下所示;
OtherAdapter:
public class OthersAdapter implements NjoftimeItemInterface {
private Njoftime njoftime;
private LayoutInflater inflater;
private Context mContext;
public OthersAdapter(LayoutInflater inflater, Njoftime njoftime, Context context) {
this.njoftime = njoftime;
this.inflater = inflater;
this.mContext = context;
}
public View getView(View convertView) {
ViewHolder holder;
View view;
//we have a don't have a converView so we'll have to create a new one
if (convertView == null) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.other_element, null);
//use the view holder pattern to save of already looked up subviews
holder = new ViewHolder(
(TextView) viewGroup.findViewById(R.id.tittle),
(TextView) viewGroup.findViewById(R.id.short_desc),
(TextView) viewGroup.findViewById(R.id.category),
(RelativeLayout)viewGroup.findViewById(R.id.card));
viewGroup.setTag(holder);
view = viewGroup;
} else {
//get the holder back out
holder = (ViewHolder) convertView.getTag();
view = convertView;
}
//change the font
Typeface typeFace = Typeface.createFromAsset(mContext.getAssets(), "fonts/Lato-Regular.ttf");
holder.category.setTypeface(typeFace);
holder.short_description.setTypeface(typeFace);
holder.title.setTypeface(typeFace);
Others other = (Others) njoftime;
holder.title.setText(other.getTitle());
holder.short_description.setText(other.getShort_desc());
holder.category.setText(other.getCategory());
if(other.getSponsor()){
holder.card.setBackgroundResource(R.drawable.njoftime_item_background_sponsor);
}
return view;
}
public int getViewType() {
return NjoftimeKategori.NJOFTIME_CATEGORY.ordinal();
}
public class ViewHolder {
public TextView title;
public TextView short_description;
public TextView category;
public RelativeLayout card;
public ViewHolder(TextView title, TextView short_description, TextView category,RelativeLayout card) {
this.title = title;
this.short_description = short_description;
this.category = category;
this.card = card;
}
}
}
我也嘗試過: mAdapter.notifyDataSetChanged()
但也沒有用。
我知道這個問題似乎像以前問過的很多問題一樣,但是我還沒有找到解決方案。 原因之一可能是我必須顯示的項目類型不同。
任何解決方案將不勝感激。
好吧,我以某種方式找到了解決方案。 訣竅是我已經基於NjoftimeItemInterface
類型的row
構建了適配器。 當我調用this.notifyDataSetChanged();
因為適配器沒有相同的行數,所以沒有任何反應,因為我沒有更新該行的ArrayList。 正確的實現如下:
public void updateNjoftimeList(ArrayList<Njoftime> newList){
for (Njoftime njoftim : newList) {
if (njoftim.getType() == NjoftimeKategori.PUNA_IME.ordinal()) {
rows.add(new PunaImeAdapter(LayoutInflater.from(mContext),
njoftim, mContext));
}
if (njoftim.getType() == NjoftimeKategori.NJOFTIME_CATEGORY.ordinal()) {
rows.add(new OthersAdapter(LayoutInflater.from(mContext),
njoftim, mContext));
}
if (njoftim.getType() == NjoftimeKategori.MAKINA_CATEGORY.ordinal()) {
rows.add(new MakinaAdapter(LayoutInflater.from(mContext),
njoftim, mContext));
}
if (njoftim.getType() == NjoftimeKategori.PRONA_CATEGORY.ordinal()) {
rows.add(new PronaAdapter(LayoutInflater.from(mContext),
njoftim, mContext));
}
}
this.notifyDataSetChanged();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.