簡體   English   中英

如何更新標簽中的列表視圖?

[英]How to update listview in tab?

我的應用程序中有兩個選項卡,並且這些選項卡中有列表視圖。

我為每個listvew設置了List數據。

在此處輸入圖片說明

我想刪除表單列表,並單擊[x]圖像時從列表視圖中刪除。

從列表中刪除項目在我的代碼中,但我不知道如何更新列表視圖,我在我的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;
    }
}

您可以在列表視圖中引入notifyDataSetChanged() ,也可以重置setAdapter(),以得到新的列表值,但是以后的設置會很昂貴

您有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創建一個Add或Delete方法:

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

然后也是一個updateMethod:

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

現在不適合您的唯一原因是因為您有兩個不同的列表。 您正在從錯誤的列表中刪除。

因此,首先不要從列表中刪除。 在適配器中編寫一個方法,將其從存儲在該適配器中的列表中刪除,然后在該方法中調用notifyDatasetChanged。

如果notifyDatasetChanged無法正常工作,請嘗試使用此方法

yourlist.invalidateviews();

您需要通過調用從arraylist中刪除該特定項目

AllProducts.remove(position);
notifyDatasetChanged();

在適配器中編寫此方法,並使用它從適配器中移除垂直的項目。

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

我解決了我的問題。

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

在android 2.x中使用選項卡時,我在模擬器中發現了一個錯誤:選項卡未正確刷新或刪除。 替代方法是使用支持庫v7作為對操作欄選項卡的支持。

使用以下代碼更新ListView 為了更新ListView您必須調用notifyDataSetChanged(); Adapter 請參見下面的代碼。

你做了什么。

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

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM