我正在使用列表视图的自定义适配器来显示图像和标题。在我的应用程序中,我添加和删除列表中的项目,我必须在界面上显示更改。在数据发生任何变化时,我从数据库中获取所有列表并将其存储在适配器列表中,然后调用 adapter.notifyDataSetChanged(); 但界面没有显示任何变化。另一方面,当我直接从适配器添加或删除项目时,它会正确响应。我使用了简单的适配器,并为此适配器.notifyDataSetChanged(); 工作正常,但对于定制它没有响应。我还在stackoverflow上尝试了许多解决方案,但没有一个有效。比如 当适配器数据改变时更新列表视图
Adapter.notifyDataSetChanged() 不工作
notifyDataSetChange 不适用于自定义适配器
我在 oncreate 这样做
enter code here
product_data = new ArrayList<Product>();
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getInt("list_id");
}
product_data = db.getAllProductsstatus(value, 1);
Collections.reverse(product_data);
adapter = new ProductAdopter(this,
R.layout.listview_item_row, product_data, 2);
ListView listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
registerForContextMenu(listView1);
并在 onMenuItemSelected
enter code here
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
String listItemName = adapter.getItem(info.position)._name;
int id = adapter.getItem(info.position)._id;
if(menuItemIndex == 0)
{
db.deleteProduct(adapter.getItem(info.position));
product_data = db.getAllProducts(value);
Collections.reverse(product_data);
//adapter.remove(adapter.getItem(info.position));
adapter.notifyDataSetChanged();
}
else if(menuItemIndex == 1){
String name ;
if (adapter.getItem(info.position)._status == 0){
popall("bar code", "no bar code entered");
}
else {
popall("bar code", adapter.getItem(info.position)._bar_code);
}
}
else if (menuItemIndex == 2){
adapter.getItem(info.position)._status = 0;
adapter.getItem(info.position)._bar_code = "0";
db.updateProduct(adapter.getItem(info.position));
product_data = db.getAllProductsstatus(value, 1);
adapter.notifyDataSetChanged();
popall("alert", "successfully done");
}
return true;
}
我的自定义适配器类如下
public class ProductAdopter extends ArrayAdapter<Product> {
Context context;
int layoutResourceId;
List<Product> data = null;
int source;
public ProductAdopter(Context context, int layoutResourceId, List<Product> product_data, int source) {
// TODO Auto-generated constructor stub
super(context, layoutResourceId, product_data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = product_data;
this.source = source;
}
// public ProductAdopter(MainActivity context2, int listviewItemRow, List<Product> product_data) {
// TODO Auto-generated constructor stub
//}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ProductHolder holder = null;
if(row == null && source == 1)
{
LayoutInflater inflater = ((MainActivity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ProductHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
if(row == null && source == 2)
{
LayoutInflater inflater = ((purchased)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ProductHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
if(row == null && source == 3)
{
LayoutInflater inflater = ((Skiped)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ProductHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (ProductHolder)row.getTag();
}
Product product = data.get(position);
holder.txtTitle.setText(product.getName());
if(product.icon.equals("no")){
holder.imgIcon.setImageResource(R.drawable.blank);
}
else{
File imgFile = new File(product.icon);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Bitmap resized = Bitmap.createScaledBitmap(myBitmap, 70, 70, true);
holder.imgIcon.setImageBitmap(resized);
myBitmap.recycle();
}
}
return row;
}
static class ProductHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}