6

我有一个包含“删除”按钮的自定义列表视图项。我创建了一个名为LazyListAdapterextends的自定义适配器BaseAdapter。在我覆盖的 getView 方法中,我将此按钮的 onclick 方法设置如下:

@Override
public View getView(final int pos, View convertView, ViewGroup parent) {

     View v = convertView;

     // Some other things...

     ImageButton removeFav = (ImageButton) v.findViewById(R.id.removeFavorites);

     removeFav.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {

          // I delete the object from Parse database here,
          // Therefore I want the view to disappear here
     }
}

如何使用此 onclick 方法中的代码删除或以某种方式隐藏相应的视图?还是我应该改变我的方法?

非常感谢你。

4

3 回答 3

8

尝试这个

@Override
public View getView(final int pos, View convertView, ViewGroup parent) {

    View v = convertView;

    // Some other things...

    ImageButton removeFav = (ImageButton) v.findViewById(R.id.removeFavorites);

    removeFav.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

       // After you delete the object from Parse database here,
       notifyDataSetChanged();

    }
}
于 2013-04-18T06:38:11.580 回答
0

Simply you can remove an item inside getview method as in the example

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_row, null, false);
        viewHolder = new ViewHolder();
        viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
        convertView.setTag(viewHolder);

    } else {
        // we call the view created before to not create a view in each time
        viewHolder = (ViewHolder) convertView.getTag();
    }

    final int imgId = imageId.get(position);

    viewHolder.img.setImageResource(imgId);

    viewHolder.img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "Clicked " + imgId, Toast.LENGTH_SHORT)
                    .show();
        }
    });

    // Create a generic swipe-to-dismiss touch listener.
    viewHolder.img.setOnTouchListener(new SwipeDismissTouchListener(
            viewHolder.img, null,
            new SwipeDismissTouchListener.DismissCallbacks() {
                @Override
                public boolean canDismiss(Object token) {
                    return true;
                }

                @Override
                public void onDismiss(View view, Object token) {

                    Log.d(TAG, "Image ıd" + imgId);


                    imageId.remove(position);
                    remove(position);

                    notifyDataSetChanged();
                }
            }));

    return convertView;
}
于 2014-11-24T11:42:57.983 回答
0

尝试使用 parent.removeViewAt(position)。

于 2013-04-17T15:06:52.707 回答