0

I am developing an app in which the main task is to display a list of different type of items( to be exactly 4 different type of items). For this I have implemented a Custom Adapter like below :

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);
    }
}

When it loads the items for the first time, everything seems fine but when I want to load more data and update the listView nothing changes, it only displays the data that were loaded for the first time.

In order to update the list I have used the following code:

 mAdapter.updateNjoftimeList(njoftime);

which calls the method inside the adapter.

For each different type of item, I have developed a piece of code for displaying the data like below, for example;

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;

        }
    }
}

I have try also: mAdapter.notifyDataSetChanged() but it didn't worked either.

I know that this questions seems similar like a ton of questions asked before, but I haven't found a solutions yet. One of the reason maybe the different type of items i have to display.

Any solutions will be more than appreciated.

Xhulio
  • 581
  • 7
  • 26
  • its not clear to me where updateNjoftimeList() causes the adapters member "rows" to be updated ? – CSmith Jun 02 '15 at 15:24
  • well I was based in this questions: (http://stackoverflow.com/questions/15422120/notifydatasetchange-not-working-from-custom-adapter) – Xhulio Jun 02 '15 at 15:28
  • 2
    You're doing it in a very confusing way which is why it's hard to figure out what's wrong. What you are changing in your notifyDataSetChanged() is essentially `njoftime`, however, your entire logic is based on the `rows` which is a list of your custom adapters that never gets updated from what I can see. This means, your list will never know you have more items since the `rows.size()` stays the same even after adding new "rows" to `njoftime`. You will need to repopulate the `rows` collection in order for your solution to work (I'm sure you can figure out how). – kha Jun 02 '15 at 15:32
  • 1
    @kha Thanks for the hint. I somehow managed to resolve the issue. I will post the solutions in case somebody may need it. – Xhulio Jun 03 '15 at 09:10

1 Answers1

1

Well, somehow I found the solutions. The trick is that I have build the adapter based on the row which are of type NjoftimeItemInterface. When i call this.notifyDataSetChanged(); nothing happens because the adapter has the same number of rows, since I did not updated the row's ArrayList. The correct implementation is as follows:

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();

}
Xhulio
  • 581
  • 7
  • 26