我正在開發一個應用程序,其中主要任務是顯示不同類型的項目列表(完全是4種不同類型的項目)。爲此,我已經實現了一個自定義適配器象下面這樣:當它加載的項目首次notifyDataSetChanged不適用於自定義適配器和不同類型的行
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()
但它沒有奏效。
我知道這個問題看起來很像以前問過的很多問題,但我還沒有找到解決方案。其中一個原因可能是我必須展示的不同類型的物品。
任何解決方案將不勝感激。
它對我不清楚updateNjoftimeList()導致適配器成員「行」被更新? – CSmith
以及我是基於這個問題:(http://stackoverflow.com/questions/15422120/notifydatasetchange-not-working-from-custom-adapter) – Xhulio
你在做一個非常混亂的方式,這就是爲什麼它很難弄清楚什麼是錯的。你在notifyDataSetChanged()中改變的基本上是'njoftime',然而,你的整個邏輯是基於'rows',這是你自定義適配器的列表,它永遠不會從我所能看到的更新。這意味着,即使在將新的「行」添加到「njoftime」之後,您的列表也永遠不會知道您有更多項目,因爲'rows.size()'保持不變。您需要重新填充「行」集合才能使解決方案發揮作用(我相信您可以弄清楚)。 – kha