从ViewHolder(列表)中删除项目,更新屏幕

时间:2012-03-14 16:56:28

标签: android android-layout android-intent

我有一个ViewHolder,它保存(相对布局)视图添加到列表中。在每个单独的布局中,我有一个删除按钮。我希望能够单击删除按钮并更新屏幕。我当前的实现工作正常,但是我必须按下后退按钮,然后返回到屏幕以查看是否已删除。

从本质上讲,这是一个动态添加项目的购物车。简而言之,当点击删除按钮时,我需要添加什么才能更新屏幕的onClick事件? Invalidate()不起作用。

提醒:这些方法与onCreate()分开,所以我不能使用finish()或getIntent()或其他任何方法。

编辑:我已经为Reinier的回答添加了其他评论

1 个答案:

答案 0 :(得分:3)

我假设您正在使用带有自定义ListAdapter的ListView?将数据集(例如ArrayList)传递给构造函数中的ListAdapter。然后,当您操作数据集时(例如dataset.remove(Object object)),请在ListAdapter上调用.notifyDatasetChanged()。然后,您的适配器将更新它所属的视图。

更新

我不确定你的意思是'它拧紧了支架中的位置'。 ViewHolder基本上只是保存您想要在View - 方法中操作的getView()的引用。您的数据集与ViewHolder分开,因此从数据集中删除内容不应影响布局 - 除了从列表视图中删除项目,当然。

以下是应该起作用的一个例子。我试着尽可能地解释。您可能知道大部分内容,但我只是提供了一些额外信息以供将来参考/ Google员工使用。

注意:它可能不完美,但应该做得很好。

public class MyAdapter extends ArrayAdapter<CartItem> {

// This is our data-model.
// let's say your cartitems only contain an id and name
// normally this would be defined elsewhere in your code
public class CartItem {
    public int id;
    public String product_name;
}

/*
 * ViewHolders are basically meant to keep a reference to the Views,
 * so that you don't have to use .findViewById() on every getView()
 * for the elements you're trying to manipulate.
 * 
 * .findViewById() finds Views by traversing the hierarchy (heavy).
 * This generally isn't a problem, but we want to avoid this in
 * ListViews because getView() gets called a lot - which makes our app slow.   
 * 
 * We will want to keep a reference to our TextView and Button,
 * because these are the elements we want to change every getView().
 * Not to the RelativeLayout, because it's already passed in convertView
 * (after you inflated it for the first time) and we're not manipulating it
 * anyway.
 */
public class ViewHolder {

    public Button deleteButton;
    public TextView mTextView;

}

public MyAdapter(Context context, int textViewResourceId,
        ArrayList<CartItem> cartItems) {

    // here, we tie our data (ArrayList of CartItem's)
    // to the ListAdapter (ArrayAdapter = extended ListAdapter)
    super(context, textViewResourceId, cartItems);
}


    // this method 
public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    // if convertView == null, that means we haven't inflated our listitem yet.
    if(convertView == null) {

        // so, we'll inflate our listitem now.
        // after this, convertView will contain our RelativeLayout
        // and its children/subviews
        LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listitem, null);

        // now we're gonna instantiate the ViewHolder to keep a reference
        // to our TextView and ImageView
        holder = new ViewHolder();

        holder.mTextView = (TextView) convertView
                .findViewById(R.id.listitem_textview);
        holder.deleteButton = (Button) convertView
                .findViewById(R.id.listitem_deletebutton);

        // Now that we have our reference, we want to make sure we can
        // keep our reference by using tags. Tags are a way to attach
        // data to a View.
        convertView.setTag(holder);
    } else {
        // if we have already inflated our listitem, we just get the
        // references to our Views from the tag
        holder = (ViewHolder) convertView.getTag();
    }

    // we want to read/do-stuff-with a specific CartItem. First, get
    // a reference to the data-object.
    final CartItem mCartItem = (CartItem) getItem(position);

    // now, it's time to manipulate our views
    holder.mTextView.setText(mCartItem.product_name);
    holder.deleteButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // then, pass a signal to MyAdapter that we want to remove
            // this item from our dataset
            remove(mCartItem);
            // now, we want to update any (list)views attached to our MyAdapter
            // this will let the ListView update itself
            notifyDataSetChanged();
        }

    });

    // convertView will be recycled, which means what we output here
    // will be the input for the next getView()-call
    return convertView;
}
}

如果您想从活动中操作数据集,请执行以下操作:

MyAdapter myAdapter = new MyAdapter(this, R.layout.listitem, cartItems);
ListView listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(myAdapter);

cartItems.remove(object);
myAdapter.notifyDatasetChanged();

这将从列表视图中删除该项目,是的,这将重置位置索引。但是,如果您正确设置ListAdapter,则不会出现问题。

相关问题