简体   繁体   English

如何更新标签中的列表视图?

[英]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. 我为每个listvew设置了List数据。

在此处输入图片说明

I want to delete form list, and from listview when I click [x] image. 我想删除表单列表,并单击[x]图像时从列表视图中删除。

Item deleted from list In my code , but I dont know how to update listview,I use notifyDataSetChanged() in my customadapter, but not update. 从列表中删除项目在我的代码中,但我不知道如何更新列表视图,我在我的customadapter中使用notifyDataSetChanged() ,但未更新。

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: 您有2个不同的List ,尝试执行以下操作:

Create a constructor without passing a List i mean: 创建一个构造函数而不传递一个List我的意思是:

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

Then in the CustomListAdapter create an List Local variable and instatiate it in the constructor: 然后在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;
}

then create an Add or Delete method in the CustomListAdapter : 然后在CustomListAdapter创建一个Add或Delete方法:

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

Then an updateMethod too: 然后也是一个updateMethod:

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。

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

yourlist.invalidateviews();

You need to remove that particular item from arraylist by calling 您需要通过调用从arraylist中删除该特定项目

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: 和第二个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. 在android 2.x中使用选项卡时,我在模拟器中发现了一个错误:选项卡未正确刷新或删除。 Alternatives is to use support library v7 which as support for action bar tabs. 替代方法是使用支持库v7作为对操作栏选项卡的支持。

Use following code to update ListView . 使用以下代码更新ListView In order to update ListView you must have to call notifyDataSetChanged(); 为了更新ListView您必须调用notifyDataSetChanged(); menthod on Adapter . 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);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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