I use a custom list view adapter to show the image and title. In my application, I add and remove items in the list, and I have to show that the changes in the interface. at any data change, I get the entire list from the database and save it in the adapter list, and then call the adapter .notifyDataSetChanged (); but the interface does not show any changes. on the other hand, when I directly add or remove an element from the adapter, it responds correctly. I used a simple adapter for this adapter .notifyDataSetChanged (); It works fine, but for configured it does not respond. I also tried many solutions in stackoverflow, but it does not work. How to
Update list view when adapter data changes
Updating the list of Android files
Adapter.notifyDataSetChanged () not working
notifyDataSetChange does not work with custom adapter
I do it like folows at 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);
and in 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.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;
}
my adapted adapter class is below
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) {
super(context, layoutResourceId, product_data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = product_data;
this.source = source;
}
@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;
}
}