Ishwinder Ishwinder - 4 months ago 24
Android Question

Try to delete selected item from listview and DB but my code will delete the previous item rather than the selected item

Below is the code for delete item from DB

public boolean deleteRow(long RowId) {
String where = id + "=" + RowId;
return sqldata.delete(table_name, where, null) != 0;

}


And Listview where I have to delete the item is

CheckData.DbHelper dbhelper;
ArrayList strings = new ArrayList();
CheckData cd;
public void onCreate(Bundle saved){
super.onCreate(saved);

setContentView(R.layout.activity_sql_view);
tv=(TextView)findViewById(R.id.get_info);
cd=new CheckData(this);
cd.open();
final Cursor c = cd.getData();
c.requery();
if(c.getCount()==0){
setContentView(R.layout.activity_sql_view);

Toast.makeText(this,
"No more Records",
Toast.LENGTH_SHORT).show();
}else {
ret();
}

}
public void ret(){
final Cursor c = cd.getData();
c.requery();

if (c.getCount()>0 && c.getCount()!=0)

{if (c.moveToFirst())
{
// ArrayList strings = new ArrayList();
do {


String mC = c.getString(0);

String mc1=c.getString(0);

String mc2=c.getString(2);
String mcc= c.getString(1) + "\n" + c.getString(2) + "\n";

strings.add(mc1);

// strings.add(mc2);



} while (c.moveToNext());
lv_arr = (String[]) strings.toArray(new String[strings.size()]);
}
}


c.close();

final ListView CView = new ListView(this);
final ArrayAdapter arrayAdapter=new ArrayAdapter(this,
android.R.layout.simple_list_item_1, lv_arr);
arrayAdapter.notifyDataSetInvalidated();
arrayAdapter.notifyDataSetChanged();
CView.setAdapter(arrayAdapter);
setContentView(CView);
CView.setOnItemLongClickListener(this);
}

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

Log.v("long clicked", "pos: " + position);

cd.open();
//long i=id;
// i--;

cd.deleteRow(id);

// CView.setVisibility(ListView.VISIBLE);
// Intent intent=new Intent(SqlView.this,SqlView.class);
// startActivity(intent);
// ret();

Toast.makeText(getApplicationContext(),
"You have deleted a workout!", Toast.LENGTH_LONG).show();
return false;
}
}


In above code I will try the commented code for deleting selected item but not working and also the Listview can not refreshed

Answer

You are passing the id or position of selected item from listview, So you should know that the listview items are initialize with 0 position and it may be possible you have store items with id 1 and then increase it by 1.

So In this kind of situation either delete it by increase id by 1 or create one object for selected item and then pass it to remove.

For first easy situation check below answer.

  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View view, int      position, long id) 
  {

     Log.v("long clicked", "pos: " + position);

      cd.open();
      //long i=id;
      //     i--;

      cd.deleteRow(id + 1);
      lv_arr.remove(position);

      // CView.setVisibility(ListView.VISIBLE);
      //  Intent intent=new Intent(SqlView.this,SqlView.class);
      //   startActivity(intent);
      //   ret();

      Toast.makeText(getApplicationContext(),"You have deleted a workout!", Toast.LENGTH_LONG).show();
      return false;
   }
Comments