[英]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.