notifyDataSetChanged不适用于自定义适配器和不同类型的行

时间:2015-06-02 15:16:27

标签: android listview baseadapter custom-adapter notifydatasetchanged

我正在开发一个应用程序,其中主要任务是显示不同类型的项目列表(准确地说是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()但它也没有用。

我知道这些问题似乎与之前提出的大量问题类似,但我还没有找到解决方案。其中一个原因可能是我必须展示不同类型的物品。

任何解决方案都将受到赞赏。

1 个答案:

答案 0 :(得分:1)

好吧,不知怎的,我找到了解决方案。诀窍是我已经基于类型为row的{​​{1}}构建了适配器。当我调用NjoftimeItemInterface没有任何反应,因为适配器具有相同的行数,因为我没有更新行的ArrayList。正确的实施如下:

this.notifyDataSetChanged();