Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customized adapter.notifyDataSetChanged() not working

i am using a customized adapter of list view to show an image and title. In my application i add and remove the items in the list and i have to show that changes on the interface. on any change in data i get all list from data base and store it in adapter list and then call adapter.notifyDataSetChanged(); but interface is not showing any change. on the other hand when i directly add or remove item from adapter it respond correctly. i have used simple adapter and for that adapter.notifyDataSetChanged(); is working fine but for customized it is not responding. i have also tried many solution on the stackoverflow but non of them work. like Updating the list view when the adapter data changes

Android List view refresh

Adapter.notifyDataSetChanged() is not working

notifyDataSetChange not working from custom adapter

i doing this as 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 customized adapter class is given 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;
    }

}
like image 641
user2274314 Avatar asked Apr 29 '13 15:04

user2274314


1 Answers

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

product_data = db.getAllProducts(value);

When you created the adapter in onCreate(), you gave it a reference to product_data. At that point your changes to product_data would propagate into the adapter, because it has a reference to your list.

But as soon as you assign a new list to your product_data variable (like you do in the onMenuItemSelected()) the adapter now has a reference to the old list, while your product_data now points to a new list. So your adapter doesn't know about your changes in the new list.

Instead of reassigning the variable, try clearing it and copying your items like this:

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

On another note, if you read your data from the database, you should consider using a CursorAdapter instead.

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

like image 167
Daniel Gabriel Avatar answered Oct 15 '22 14:10

Daniel Gabriel