2014-02-17 21 views
1

我在我的应用程序中有两个选项卡,并且在此选项卡中有列表视图。如何更新标签中的列表视图?

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

enter image description here

我想删除列表的形式,并从列表视图中,当我点击[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; 
    } 
} 
+3

请提供您的自定义接口的代码。其实'notifyDataSetChanged()'会做你想要的每个标题的问题 - 更新列表视图 – nikis

+0

代码请 –

+0

ya在哪里调用notifyDataSetChanged()方法? – San

回答

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

和第二列表视图:

MyTabActivity.setTab(1); 
1

您可以在列表视图介绍notifyDataSetChanged()或者您可以重置setAdapter()的新列表值,但后来一个将是昂贵的

0

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

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

+0

有两个列表视图选项卡上,当我点击删除图片,从列表中删除,但我需要更新的是 –

+0

后您从列表中删除但我建议你从存储在CustomAdapter列表中删除。 你能提供适配器的完整代码吗? – Pankaj

+0

我张贴的MyAdapter –

0

如果notifyDatasetChanged工作不尝试通过调用

AllProducts.remove(position); 
notifyDatasetChanged(); 
+0

所有代码,但没有奏效 –

+0

这里是我的应用程序一段代码,删除和更新列表视图listView.remove(位置); notifyDataSetChanged();或listView.invalidateview(); – nEwbie

0

您需要从ArrayList中移除特定项目,尝试这样做:

创建构造函数而不通过List我的意思是:

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

然后在CustomListAdapter创建一个列表局部变量和instatiate它在构造函数中:

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(); 
} 
+0

我这样做,从项目ArrayList中删除,但列表视图不更新 –

1

你有2个不同的List使用此

yourlist.invalidateviews(); 
+0

,我必须把“公共无效更新(){ mProductList.notifyDataSetChanged();} ” –

+0

@SardorDushamov在CustomListAdapter类 –

+0

我的意思是,创建所有修改CustomListAdapter中的List的方法,因此您也修改了用于创建视图的列表。 –

0

write thi在您的适配器中使用它的方法并使用它从适配器中删除特定的Item。

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

与安卓2.X标签的工作,我发现在模拟器中的错误:选项卡不正确刷新或删除。替代方案是使用支持库v7作为对操作栏选项卡的支持。

0

使用以下代码更新ListView。 为了更新ListView你必须在Adapter上打电话notifyDataSetChanged();。见下面的代码。

你做了什么。

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

    } 
相关问题