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.