0

I Have a listview of notification. and i want that after clicking on that notification it should be deleted.

but it gives me error of unsupported exception.If any one have solution please answer.

here, is my code.

NotificationSetGet notifisetget;
ArrayList<NotificationSetGet> clsdtlsetget;

NotificationAdapter notifiadp = new NotificationAdapter(clsdtlsetget,
                                getActivity());
lstview.setAdapter(notifiadp);

lstview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent,
                            View view, int position, long id) {
        // TODO Auto-generated method stub
        notifisetget = clsdtlsetget.get(position);

        clsdtlsetget.remove(position);

        notifiadp.notifyDataSetChanged();

    }

});

and here is my adapter.

public class NotificationAdapter extends BaseAdapter {

    private final LayoutInflater mInflater;
    ArrayList<NotificationSetGet> clsdtlsetget;
    NotificationSetGet notifisetget;
    Context mcontext;
    String notititle, currentDateandTime, noticreateddatetime, notitype;
    ListView mListView;

    public NotificationAdapter(ArrayList<NotificationSetGet> clsdtlsetget,
            Context context) {
        this.mcontext = context;
        mInflater = LayoutInflater.from(context);
        this.clsdtlsetget = clsdtlsetget;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 10;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return clsdtlsetget.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
            final ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.notificationraw,
                    parent, false);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        currentDateandTime = sdf.format(new Date());

        TextView textnotiftitle = (TextView) convertView
                .findViewById(R.id.textnotiftitle);
        TextView txtnotificationtime = (TextView) convertView
                .findViewById(R.id.txtnotificationtime);


        notifisetget = clsdtlsetget.get(position);

        notititle = notifisetget.getNotification_text();
        notitype = notifisetget.getnotificationType();
        if (!TextUtils.isEmpty(notititle)) {
            textnotiftitle.setText(notititle);
        }

        noticreateddatetime = notifisetget.getNotification_createdAt();


        return convertView;
    }

}
milez
  • 2,201
  • 12
  • 31
Mansi Bhatt
  • 214
  • 3
  • 12

3 Answers3

0

Here you need to do following thing:

  1. Override notifyDataSetChanged() method as follow:

    public void notifyDataSetChanged(List<NotificationSetGet> clsdtlsetget){
        this.clsdtlsetget = clsdtlsetget;
        super.notifyDataSetChanged();
    }
    
  2. You need to change in onItemClick() as:

    notifiadp.notifyDataSetChanged(clsdtlsetget);

Kinnar Vasa
  • 397
  • 1
  • 9
0

You have some issues in your getView code. Tyr to follow this

Step 1:

Create a ViewHolder class

class ViewHolder{
TextView textnotiftitle, txtnotificationtime;
}

Step 2:

Modify your getView

@Override
    public View getView(final int position, View convertView,
            final ViewGroup parent) {
        // TODO Auto-generated method stub
    ViewHolder holder;
        if (convertView == null) {
    holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.notificationraw,
                    parent, false);
holder.textnotiftitle = (TextView) convertView
                .findViewById(R.id.textnotiftitle);
        holder. txtnotificationtime = (TextView) convertView
                .findViewById(R.id.txtnotificationtime);
convertView.setTag(holder);
        }else{
holder = (ViewHolder) convertView.getTag();

}

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        currentDateandTime = sdf.format(new Date());




        notifisetget = clsdtlsetget.get(position);

        notititle = notifisetget.getNotification_text();
        notitype = notifisetget.getnotificationType();
        if (!TextUtils.isEmpty(notititle)) {
            holder.textnotiftitle.setText(notititle);
        }else{
// You need to handle the else part here
}


        noticreateddatetime = notifisetget.getNotification_createdAt();


        return convertView;
    }

Note:

You must handle the else part of this if condition

if (!TextUtils.isEmpty(notititle)) {
Amsheer
  • 7,046
  • 8
  • 47
  • 81
0

Change the getCount() method in your notificationAdapter like below and check:

 @Override
    public int getCount() {
        if(clsdtlsetget != null){
           return clsdtlsetget.size();
        }
        return 0;
    }
Pankaj
  • 7,908
  • 6
  • 42
  • 65
  • in listview i want to limit the listview to take only 10 records.so that's why i put 10 over there. – Mansi Bhatt Jul 16 '15 at 06:20
  • I doubt if you have 4-5 records then your app would get crashed because your arraylist have 4-5 elements and your adapter trying to add more element then arraylist have. – Pankaj Jul 16 '15 at 06:24
  • Then dont make it returning a hard coded value as `10`. What you can do is limit the arraylist which you are passing to constructor of `notificationAdapter` as the size of 10 only – Pankaj Jul 16 '15 at 06:32
  • ya,its removed from listview but when i came again it's remain unchanged – Mansi Bhatt Jul 16 '15 at 07:27
  • The answer you have accepted only remove the item from list not from your server or database from where you are generating your arraylist. So you need to update it via calling a update api or if you are having a database then remove the entry of the object which you are removing from your listview. – Pankaj Jul 16 '15 at 07:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83404/discussion-between-mansi-bhatt-and-clairvoyant). – Mansi Bhatt Jul 16 '15 at 08:32