Custom .notifyDataSetChanged () adapter not working

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

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

}
+5
source share
2 answers

The problem is that onMenuItemSelected()you have this line in:

product_data = db.getAllProducts(value);

When you created the adapter in onCreate(), you gave a link to product_data. At this point, your changes product_datawill be propagated to the adapter, because it has a link to your list.

But as soon as you assign a new list to your variable product_data(for example, in onMenuItemSelected()), the adapter now has a link to the old list, and yours product_datanow points to the new list, so your adapter does not know about your changes in the new list.

:

product_data.clear();
product_data.addAll(db.getAllProducts(value));

, , CursorAdapter.

: http://blog.cluepusher.dk/2009/11/16/creating-a-custom-cursoradapter-for-android/

+10

, , (I.e after add/remove items.) .

:

    if(menuItemIndex == 0) { db.deleteProduct(adapter . Get item(info.position)); product_data = db.getAllProducts(value); 
    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);

 //adapter.remove(adapter.getItem(info.position)); 
//adapter.notifyDataSetChanged(); 

}

, , , adapter.notifyDataSetChanged();

, Settimg onMenuItemSelected()

, .

0

All Articles