我正在开发一个应用程序,其中主要任务是显示不同类型的项目列表(完全是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