Skip to content Skip to sidebar Skip to footer

Android Refresh Adapter With Baseadapter

in this code after insterting new items to adapert my list could not refresh and update with notifyDataSetChanged() for example for this line my adapter could set without any probl

Solution 1:

Instead of creating a new items each time and setting it to adapter.. try to add and remove from the adapter directly.

First initialize the adapter with initial values you need or an empty list.

  1. Next when you want to add.. call adapter.add(obj)
  2. When you want to remove call adapter.remove(obj)
  3. When you want to empty the adapter call adapter.clear()

Finally call the adapter.notifyDatasetChanged() whenever you need the changes to be reflected

Solution 2:

Try this way,hope this will help you to solve your problem.

publicclassReceivedAdapterextendsBaseAdapter
{
    private Context context;
    private List<ReceivedItemStructure> row;
    publicReceivedAdapter(Context context, List<ReceivedItemStructure> row)
    {
        this.context = context;
        this.row = row;
    }
    publicvoidaddItem(ReceivedItemStructure item) {
        if(row!=null){
            row.add(item);
            notifyDataSetChanged();
        }
    }
    publicvoidremoveItem(ReceivedItemStructure item) {
        if(row!=null){
            row.remove(item);
            notifyDataSetChanged();
        }
    }
    publicvoidremoveItemAtPosition(int position) {
        if(row!=null){
            row.remove(position);
            notifyDataSetChanged();
        }
    }
    publicvoidclearAll() {
        if(row!=null){
            row.clear();
            notifyDataSetChanged();
        }
    }
    @Override
    publicintgetCount() {
        return row.size();
    }
    @Override
    public ReceivedItemStructure getItem(int position) {
        return row.get(position);
    }
    @Override
    publiclonggetItemId(int position) {
        return0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        if(convertView ==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.received_sms_list_fragment, null);
            holder.tv_smsBody = (TextView)convertView.findViewById(R.id.tv_smsBody);
            holder.tv_smsSender = (TextView)convertView.findViewById(R.id.tv_smsSender);
            holder.tv_smsDate = (TextView)convertView.findViewById(R.id.tv_smsDate);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_smsBody.setText(getItem(position).getmSmsBody());
        holder.tv_smsSender.setText(getItem(position).getmSmsBody());
        holder.tv_smsDate.setText(getItem(position).getmDate());
        return convertView;
    }

    classViewHolder{
        TextView tv_smsBody;
        TextView tv_smsSender;
        TextView tv_smsDate;
    }
}

Post a Comment for "Android Refresh Adapter With Baseadapter"