如何在标签中更新listview?

时间:2014-02-17 12:30:58

标签: java android listview adapter

我的应用中有两个标签,此标签中有列表视图。

我将列表数据设置为每个listvew。

enter image description here

我想删除表单列表,并在单击[x]图像时从列表视图中删除。

项目从列表中删除在我的代码中,但我不知道如何更新listview,我在我的customadapter中使用notifyDataSetChanged(),但没有更新。

第一个标签的活动:

public static List<Product> mCartList;
mCartList = AllProducts.getCartList();
        listViewCatalog = (ListView) findViewById(R.id.my_order_list);
        mProductAdapter = new CustomListAdapter(MyOrders.this, mCartList, "", true);
        listViewCatalog.setAdapter(mProductAdapter);

我的自定义列表适配器:

public class CustomListAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private Context mContext;
    private List<Product> mProductList;
    private String mType;
    private boolean mShowQuantity;

    public CustomListAdapter(Context context, List<Product> list, String type, boolean showQuantity) {
        layoutInflater = LayoutInflater.from(context);
        mContext = context;
        mProductList = list;
        mShowQuantity = showQuantity;
        mType = type;
    }

    @Override
    public int getCount() {
        return mProductList.size();
    }

    @Override
    public Object getItem(int position) {
        return mProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder item;
        final int finalMPosition = position;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_row, null);
            item = new ViewHolder();

            item.imageView = (ImageView) convertView.findViewById(R.id.product_image);
            item.name = (TextView) convertView.findViewById(R.id.name);
            item.pid = (TextView) convertView.findViewById(R.id.pid);
            item.price = (TextView) convertView.findViewById(R.id.price);
            item.description = (TextView) convertView.findViewById(R.id.description);

            item.removeProduct = (ImageView) convertView.findViewById(R.id.removeProduct);
            item.addToCart = (TextView) convertView.findViewById(R.id.addtocard);
            item.productQuantity = (TextView) convertView.findViewById(R.id.textViewQuantity);

            convertView.setTag(item);
        } else {
            item = (ViewHolder) convertView.getTag();
        }

        final Product curProduct = mProductList.get(position);
        item.imageView.setImageDrawable(curProduct.imageView);
        item.name.setText(curProduct.name);
        item.pid.setText(curProduct.pid);
        int length = curProduct.description.length();
        int start = 0, end = length;
        if (length >= 40) {
            start = 0;
            end = 40;
        }
        String s = curProduct.description.substring(start, end);
        item.description.setText(s + "...");
        item.price.setText(curProduct.price + mContext.getString(R.string.currency));


        if (mShowQuantity) {
            item.addToCart.setVisibility(View.GONE);
//            item.productQuantity.setText("Quantity: " + AllProducts.getProductQuantity(curProduct));
            item.productQuantity.setVisibility(View.GONE);
        } else {
            item.productQuantity.setVisibility(View.GONE);
            item.removeProduct.setVisibility(View.GONE);
        }
        item.removeProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
                ab.setTitle("Delete ");
                ab.setMessage("This product will be deleted from list.").setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        curProduct.selected = false;
                        AllProducts.removeProduct(curProduct);
                        notifyDataSetChanged();
                        notifyDataSetInvalidated();
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                ab.create().show();
            }
        });

        item.addToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent productDetailsIntent = new Intent(mContext, ProductDetail.class);
                productDetailsIntent.putExtra(AllProducts.PRODUCT_INDEX, finalMPosition);
                productDetailsIntent.putExtra("type", mType);
                mContext.startActivity(productDetailsIntent);
            }
        });

        return convertView;
    }

    public class ViewHolder {
        TextView pid;
        TextView addToCart;
        TextView name;
        TextView price;
        TextView description;
        ImageView imageView;
        ImageView removeProduct;
        TextView productQuantity;
    }
}

9 个答案:

答案 0 :(得分:1)

您可以在列表视图中引入notifyDataSetChanged(),或者您可以重置新列表值所具有的setAdapter(),但稍后会有一些代价

答案 1 :(得分:1)

您有2个不同的List,请尝试这样做:

创建构造函数而不传递List我的意思是:

       CustomListAdapter(MyOrders.this, "", true);

然后在CustomListAdapter中创建一个List Local变量并在构造函数中实例化它:

private List<Product> mProductList;

public CustomListAdapter(Context context, String type, boolean showQuantity) {
    layoutInflater = LayoutInflater.from(context);
    mContext = context;
    mProductList = new ArrayList<Product>();
    mShowQuantity = showQuantity;
    mType = type;
}

然后在CustomListAdapter

中创建一个添加或删除方法
public void AddItem(Product product){
   if(null != product){
    mProductList.add(product);
  }
 }

然后是updateMethod:

public void update(){
mProductList.notifyDataSetChanged();
}

答案 2 :(得分:0)

现在它不适合你的唯一原因是因为你有两个不同的列表。你正在从错误的名单中删除。

首先,不要从列表中删除。在适配器中编写一个方法,将其从存储在该适配器中的列表中删除,然后在该方法中调用notifyDatasetChanged。

答案 3 :(得分:0)

如果notifyDatasetChanged不起作用,请尝试使用此

yourlist.invalidateviews();

答案 4 :(得分:0)

您需要通过调用

从arraylist中删除该特定项目
AllProducts.remove(position);
notifyDatasetChanged();

答案 5 :(得分:0)

在适配器中编写此方法,并使用它从适配器中删除特定的项目。

public void remove(int position){
    mProductList.remove(position);
    notifyDatasetChanged();
}

答案 6 :(得分:0)

我解决了我的问题。

public static void setTab(int i){
    if(i==0){
        mTabHost.setCurrentTab(i+1);
        mTabHost.setCurrentTab(i);
    }
    else{
        mTabHost.setCurrentTab(i-1);
        mTabHost.setCurrentTab(i);
    }
}

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    curProduct.selected = false;
                    AllProducts.removeProduct(curProduct);
                    MyTabActivity.setTab(0);
                }

和第二个listview:

MyTabActivity.setTab(1);

答案 7 :(得分:0)

使用android 2.x中的标签,我在模拟器中发现了一个错误:标签未正确刷新或删除。替代方案是使用支持库v7作为操作栏选项卡的支持。

答案 8 :(得分:0)

使用以下代码更新ListView。 要更新ListView,您必须在notifyDataSetChanged();上致电Adapter menthod。见下面的代码。

你做了什么。

notifyDataSetChanged();
notifyDataSetInvalidated();

你要做的是,

yourAdapter.notifyDataSetChanged();

或者您可以使用以下代码刷新列表。

private List<Object> mCarlistitem= new ArrayList<Object>();
public void refreshlist()
    {
        mCarlistitem.clear();
        for(int i=0; i< mCartList.size(); i++)
        {
            Object object = new Object();           
            mCarlistitem.add(object);
        }
         mProductAdapter = new CustomListAdapter(MyOrders.this, mCartList, "",true);
         listViewCatalog.setAdapter(mProductAdapter);

    }