点击该项

时间:2015-07-16 05:45:44

标签: android listview

我有一份通知列表视图。 我想在点击该通知后将其删除。

但是它给了我不支持的异常的错误。如果有任何人有解决方案,请回答。

这里是我的代码。

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

    }

});

这是我的适配器。

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

}

3 个答案:

答案 0 :(得分:0)

在这里你需要做以下事情:

  1. 覆盖notifyDataSetChanged()方法如下:

    public void notifyDataSetChanged(List<NotificationSetGet> clsdtlsetget){
        this.clsdtlsetget = clsdtlsetget;
        super.notifyDataSetChanged();
    }
    
  2. 您需要将onItemClick()更改为:

    notifiadp.notifyDataSetChanged(clsdtlsetget);

答案 1 :(得分:0)

您的getView代码中存在一些问题。 Tyr要遵循这个

Step 1:

创建ViewHolder类

class ViewHolder{
TextView textnotiftitle, txtnotificationtime;
}

Step 2:

修改你的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;
    }

注意:

如果条件

,您必须处理此部分的其他部分
if (!TextUtils.isEmpty(notititle)) {

答案 2 :(得分:0)

更改notificationAdapter中的 @Override public int getCount() { if(clsdtlsetget != null){ return clsdtlsetget.size(); } return 0; } 方法,如下所示,然后检查:

height: auto;