Search code examples
androidsqlitelistviewbaseadapter

notifyDatasetChanged is not working for listview


My ListView has set of rows with Delete Button in each row.

On Click of the delete button must delete particular row from Sqlite Database and refresh the listview.

Problem

I am able to delete row from database. But I am not able to refresh the page after deleting the row.

For refreshing I tried using notifyDatasetChanged(), but no luck.

I could able to see the new list if i got back to previous activity and come back to same activity.

Please find the adapter and class codes below.

MainClass:

public class AddToCart extends AppCompatActivity {

ListView cart_list;
DatabaseHandler db = new DatabaseHandler(this);
Cursor todoCursor;
Context context;
String name, price, image, model;
ArrayList<String> bitmapArray = new ArrayList<String>();
ArrayList<String> list_name = new ArrayList<String>();
ArrayList<String> list_price = new ArrayList<String>();
ArrayList<String> list_model = new ArrayList<String>();
Toolbar toolbar;
Button checkout;
static CustomAdapter_cart customAdapter_cart;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtocart);
    context = this;
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    cart_list = (ListView) findViewById(R.id.listview_cart);

    checkout = (Button) findViewById(R.id.checkout);
    checkout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(AddToCart.this, Signup.class);
            startActivity(in);
        }
    });

    Intent i = getIntent();

    // Get access to the underlying writeable database

    // Query for items from the database and get a cursor back
    // todoCursor = db1.rawQuery("SELECT id as _id, * from cart ", null);
    // db1.execSQL("DELETE FROM cart");
    // Reading all contacts
    List<Cart> contacts = db.getAllContacts();

    cart_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });

    for (Cart cn : contacts) {
        name = cn.getName();
        list_name.add(name);
        price = cn.getPhoneNumber();
        list_price.add(price);
        image = cn.getImage_list();
        bitmapArray.add(image);
        model = cn.getModel();
        list_model.add(model);


    }
    customAdapter_cart = new CustomAdapter_cart(this, list_name, list_price, bitmapArray, list_model);
    cart_list.setAdapter(customAdapter_cart);


}


public static class MyLovelyOnClickListener implements View.OnClickListener

{
    CustomAdapter_cart contextnew;
    String myLovelyVariable;

    Context context;


    public MyLovelyOnClickListener(Context contextnew, CustomAdapter_cart context, String tv_model) {

        this.contextnew = context;
        this.context = contextnew;
        this.myLovelyVariable = tv_model;
    }

    @Override
    public void onClick(View v) {


        final DatabaseHandler db = new DatabaseHandler(context);

        SQLiteDatabase db1 = db.getWritableDatabase();
        try {
            db1.delete("cart", "model = ?", new String[]{myLovelyVariable});

            customAdapter_cart.notifyDataSetChanged();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }


}

CustomAdapter.class

public class CustomAdapter_cart extends BaseAdapter  {
ArrayList<String> list_name = new ArrayList<String>();
ArrayList<String> list_price = new ArrayList<String>();
ArrayList<String> list_images = new ArrayList<String>();
ArrayList<String> list_model = new ArrayList<String>();
CustomAdapter_cart cart_refresh;
Bitmap b;
View rowView;
Context context;
AddToCart cart;
private static LayoutInflater inflater = null;
Cursor cu;
Context contextnew;
AddToCart.MyLovelyOnClickListener listener;



String model_item;


public CustomAdapter_cart(Context context, ArrayList<String> list_name, ArrayList<String> list_price, ArrayList<String> bitmapArray, ArrayList<String> list_model) {
    this.context = context;
    this.list_name = list_name;
    this.list_price = list_price;
    this.list_images = bitmapArray;
    this.list_model = list_model;
    this.cart_refresh = this;

    inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return list_name.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}



public class Holder {
    TextView tv_name, tv_price, tv_model;
    ImageView image;
    Button delete;

}

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {


    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    final Holder holder = new Holder();

    rowView = inflater.inflate(R.layout.list_items_cart, null);
    holder.tv_name = (TextView) rowView.findViewById(R.id.name_cart);
    holder.tv_price = (TextView) rowView.findViewById(R.id.price_cart);
    holder.image = (ImageView) rowView.findViewById(R.id.image_cart);
    holder.tv_model = (TextView) rowView.findViewById(R.id.model_cart);
    holder.delete = (Button) rowView.findViewById(R.id.delete);




    holder.tv_name.setText(list_name.get(position));

    holder.tv_price.setText(list_price.get(position));
    holder.tv_model.setText(list_model.get(position));
    String n = holder.tv_model.getText().toString();

    holder.image.setImageBitmap(loadImageFromStorage(list_images.get(position)));


   // holder.delete.setTag(holder.tv_model);

    listener = new AddToCart.MyLovelyOnClickListener(context,CustomAdapter_cart.this,n);
    holder.delete.setOnClickListener(listener);


    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name_item = ((TextView) rowView.findViewById(R.id.name_cart)).getText().toString();
            String price_item = ((TextView) rowView.findViewById(R.id.price_cart)).getText().toString();
            model_item = ((TextView) rowView.findViewById(R.id.model_cart)).getText().toString();

            Intent in = new Intent(context, AddCart_FullImage.class);
            in.putExtra("model", model_item);
            in.putExtra("name",  name_item);
            in.putExtra("price", price_item);
            context.startActivity(in);
        }
    });
    return rowView;
}

private Bitmap loadImageFromStorage(String path) {

    try {
        File f = new File(path, "");
        f.canRead();
        b = BitmapFactory.decodeStream(new FileInputStream(f));
        return b;


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

Any Help would be really helpfull.

Thanks.


Solution

  • you are just deleting a row from database. Your adapter is still having the same set of data. So first update the data set of the adapter and than call notifydatasetchanged()