1

I have a custom listview in my app.All its items are stored in database.I have a remove button in each row.Onclicking the remove button,I would like to delete corresponding row,both from database and Listview.I successfully executed the code to delete from database,but the change is not reflecting in my listview.I am not understanding what my datalist(name of datalist) is.

add2cart.java

public class add2cart extends Activity{
ListView adlstvw;
Button btn,remove_btn;
SQLiteDatabase mydb;
public String sme;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add2crt);
    adlstvw=(ListView)findViewById(R.id.lstvw_add2crt);
    btn=(Button)findViewById(R.id.place_order);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
     sme= extras.getString("dtls");       
     }


    mydb=add2cart.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
    Cursor cr = mydb.rawQuery("SELECT * FROM add2cart", null);
    String [] pname = new String[cr.getCount()];
    String [] price = new String[cr.getCount()];

    int i = 0;
    while(cr.moveToNext())
    {
        String name = cr.getString(cr.getColumnIndex("pnme"));
        String prprice = cr.getString(cr.getColumnIndex("prate"));
        pname[i] = name;
        price[i] = prprice;
        i++;
    }
    CartAdapter cart=new CartAdapter(this,pname,price);
    adlstvw.setAdapter(cart);

    btn.setOnClickListener(new OnClickListener() {

         @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in=new Intent(add2cart.this,buy_ltr.class); 
            Bundle bndl = new Bundle();
            bndl.putString("som",sme); 
            in.putExtras(bndl);
            startActivity(in);

        }
    });

}

}

CartAdapter.java

public class CartAdapter extends BaseAdapter{

public static ListView adlstvw;
private final String[] pname;
private final String[] price;
private Context cntxt;
 public CartAdapter(Context c,String [] pname,String [] price)
 {

     cntxt=c;
     this.pname=pname;
     this.price=price;
 }
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return pname.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View List;
    LayoutInflater mLayoutInflater=(LayoutInflater)cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView==null) {
List=new View(cntxt);
List=mLayoutInflater.inflate(R.layout.add2crt_sub,parent, false);

 }
 else {
 List=(View)convertView;
 }

Button button=(Button)List.findViewById(R.id.remove);
button.setTag(position);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


        int position = (Integer)v.getTag();
        SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
        String pnam = pname[position];

    mydb.execSQL("DELETE FROM add2cart WHERE pnme ='"+pnam+"'");


     ////////////ADDED CODE/////////////////////

    Cursor cr = mydb.rawQuery("SELECT * FROM add2cart", null);
    String [] pname = new String[cr.getCount()];
    String [] price = new String[cr.getCount()];

    int i = 0;
    while(cr.moveToNext())
    {
        String name = cr.getString(cr.getColumnIndex("pnme"));
        String prprice = cr.getString(cr.getColumnIndex("prate"));
        pname[i] = name;
        price[i] = prprice;
        i++;
    }
   CartAdapter cart=CartAdapter.this; 
   add2cart.adlstvw.setAdapter(cart);
   notifyDataSetChanged();
    ////////////////////////////////////////

    }
    });
 TextView nametxt=(TextView)List.findViewById(R.id.prdt_nme);
 final TextView pricetxt=(TextView)List.findViewById(R.id.prdt_rate);
 final TextView totltxt=(TextView)List.findViewById(R.id.prdt_totl);
 final EditText edittext=(EditText)List.findViewById(R.id.prdt_qnty);
 edittext.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

        String s1=pricetxt.getText().toString();
        String s2=edittext.getText().toString();
        int i1=Integer.parseInt(s1);
        int i2=Integer.parseInt(s2);
        int res=i1*i2;
        totltxt.setText(Integer.toString(res));
        if (s2.matches("")) {

            edittext.setText("");

        }


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub




    }



    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});

 nametxt.setText(pname[position].toString());
 pricetxt.setText(price[position]);

    return List;
}


}
}
droid
  • 184
  • 13
  • Instead of using pname.length, use crtlist and now whn deleting the item from this list will be reflected back when you call notifydatasetchanged. @Override public int getCount() { // TODO Auto-generated method stub return crtlist.size; } – androidStud Dec 19 '14 at 05:35

3 Answers3

1

call notifyDataSetChanged() directly, no use of using ClassName.this.notifyDataSetChanged(), as you are already in the adapter.

         crtlist.remove(position); //removing from your List
         notifyDataSetChanged(); //notify your adapter about the change.

Reference here

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • Where is the word "datalist" mentioned in your code? – Darpan Dec 19 '14 at 11:07
  • the list here would be the list which you used to show your data in adapter, and then deleted one row of it. – Darpan Dec 19 '14 at 11:31
  • 1
    You may want to change your arrays `pname` and `price` to `ArrayList` as it is better to handle data. Then, call pname.remove(pname.indexOf(pnam)); price.remove(pname.indexOf(pnam)); notifyDataSetChanged(); – Darpan Dec 22 '14 at 11:35
1

You are using Array and you are populating those like the bellow code.

mydb=add2cart.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
Cursor cr = mydb.rawQuery("SELECT * FROM add2cart", null);
String [] pname = new String[cr.getCount()];
String [] price = new String[cr.getCount()];

int i = 0;
while(cr.moveToNext())
{
    String name = cr.getString(cr.getColumnIndex("pnme"));
    String prprice = cr.getString(cr.getColumnIndex("prate"));
    this.pname[i] = name;
    this.price[i] = prprice;
    i++;
}

same thing you do after delete the item from the database. After that create the adapter again and set it to listview ( you can access the listview from the adapter by making it public static in the activity). Or try notifyDataSetChanged() after populating the array .

Abakasha Panda
  • 348
  • 3
  • 8
  • make the ListView object public static in the activity , access that object from adapter and do listview.setAdapter() inside the button click listener. – Abakasha Panda Jan 08 '15 at 09:29
  • have you tried both notifyDataSetChanged and setting the adapter again – Abakasha Panda Jan 09 '15 at 07:15
  • No you can access it like ActivityName.ListviewName , if you have declared public static ...try again – Abakasha Panda Jan 12 '15 at 06:36
  • while creating object for CartAdapter don't use 'this' key word instead use 'CartAdapter.this' it will work. It is because you can not use 'this' keyword inside Anonymous inner class which refers to parent class object. – Abakasha Panda Jan 12 '15 at 07:53
  • I tried to set listview like this. CartAdapter cart=CartAdapter.this; add2cart.adlstvw.setAdapter(cart); But change in database still not reflect in listview.. – droid Jan 12 '15 at 08:18
  • No man create the CartAdapter object with the new data from the database then only it will reflect. like CartAdapter cart=new CartAdapter(cntxt,pname,price); where pname and price should hold the updated data. and then set to the listview – Abakasha Panda Jan 12 '15 at 10:54
  • Finally it worked.sorry...it was my fault.you explained it well.Thanks for your patience :) . – droid Jan 12 '15 at 10:59
0

Try removing the view item from adapter rather than removing from list.

listAdapter.remove(viewToRemove); listAdapter.notifyDataSetChanged();

arjun
  • 3,514
  • 4
  • 27
  • 48
  • CartAdapter.remove(getItem(position)); CartAdapter.notifyDataSetChanged(); have you done like this.. – arjun Dec 19 '14 at 08:24