简体   繁体   中英

How to update listview in tab?

I have two tabs in my app and there are listviews in this tabs.

I set List data to each listvew.

在此处输入图片说明

I want to delete form list, and from listview when I click [x] image.

Item deleted from list In my code , but I dont know how to update listview,I use notifyDataSetChanged() in my customadapter, but not update.

Activity for first tab:

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

my Custom List Adapter:

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

您可以在列表视图中引入notifyDataSetChanged() ,也可以重置setAdapter(),以得到新的列表值,但是以后的设置会很昂贵

You have 2 different List , try doing this:

Create a constructor without passing a List i mean:

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

Then in the CustomListAdapter create an List Local variable and instatiate it in the constructor:

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

then create an Add or Delete method in the CustomListAdapter :

public void AddItem(Product product){
   if(null != product){
    mProductList.add(product);
  }
 }

Then an updateMethod too:

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

The only reason it is not working for you now, is because you have two different lists. You are removing from wrong list.

So first of all, do not delete from your list. Write a method in your adapter where you remove it from list you have stored in that Adapter, and then call notifyDatasetChanged in that method.

如果notifyDatasetChanged无法正常工作,请尝试使用此方法

yourlist.invalidateviews();

You need to remove that particular item from arraylist by calling

AllProducts.remove(position);
notifyDatasetChanged();

write this methode in your adapter and use it to remove perticular Item from adapter.

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

I resolved my problem.

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

and

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

and for second listview:

MyTabActivity.setTab(1);

Working with tabs in android 2.x, I found a bug in the emulator: The tabs are not correctly refreshed or removed. Alternatives is to use support library v7 which as support for action bar tabs.

Use following code to update ListView . In order to update ListView you must have to call notifyDataSetChanged(); menthod on Adapter . See Below code.

What you did.

notifyDataSetChanged();
notifyDataSetInvalidated();

What you have to do is,

yourAdapter.notifyDataSetChanged();

Or you may use following code to refresh your list.

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM