If I delete an item from my listview except last record other records id doesn't change. Let say I have added 3 items in my listview, after deleting second item, the id of the 3rd item doesnt change its id to 2,even after using Autoincrement in my query. So if i click on that 3rd item . it causes to crash the app and in log it shows Cursor index out of bounds exception.
below is my relevant code
DBhelper.java
public class DBhelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "MyDBname.db";
public static final String DATABASE_VERSION = "1";
public static final String PRODUCTS_TABLE_NAME= "products";
public static final String PRODUCTS_COLUMN_ID= "id";
public static final String PRODUCTS_COLUMN_NAME= "name";
public static final String PRODUCTS_COLUMN_QUAN= "quant";
public static final String PRODUCTS_COLUMN_PRICE= "price";
public DBhelper(Context context)
{
super(context, DATABASE_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table products " + "(id INTEGER PRIMARY KEY AUTOINCREMENT,name text,quant text,price text)");
//db.execSQL( "create table contacts " + "(id integer primary key, name text,phone text,email text, street text,place text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS products");
onCreate(db);
}
public boolean insertProduct(String name,String quant,String price)
{
SQLiteDatabase db =this.getWritableDatabase();
ContentValues contentvalues =new ContentValues();
contentvalues.put("name", name);
contentvalues.put("price", price);
contentvalues.put("quant", quant);
db.insert("products", null, contentvalues);
return true;
}
public Cursor getData(int id)
{
SQLiteDatabase db= this.getReadableDatabase();
Cursor res = db.rawQuery("select * from products where id="+id+"", null);
return res;
}
public int numberofRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numrows = (int) DatabaseUtils.queryNumEntries(db, PRODUCTS_COLUMN_NAME );
return numrows;
}
public boolean updateProducts(Integer id,String name, String price,String quant){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentvalues = new ContentValues();
contentvalues.put("name",name);
contentvalues.put("quant", quant);
contentvalues.put("price", price);
db.update("products", contentvalues, "id=?", new String[]{ Integer.toString(id)});
return true;
}
public Integer deleteproduct(Integer id)
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor res= db.rawQuery("select * from products", null);
res.moveToFirst();
return db.delete("products", "id=?", new String[] {Integer.toString(id)});
}
public ArrayList<String> getallProducts(){
ArrayList<String> arraylist = new ArrayList<String>();
SQLiteDatabase db= this.getReadableDatabase();
Cursor res= db.rawQuery("select * from products", null);
res.moveToFirst();
while(res.isAfterLast()==false)
{
arraylist.add(res.getString(res.getColumnIndex(PRODUCTS_COLUMN_NAME)));
res.moveToNext();
}
return arraylist;
}
}
Below class is meant to show list
Mainproducts.java
public class MainProducts extends Activity {
private ListView lv;
DBhelper mydb;
//Button btn1;Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
mydb = new DBhelper(this);
ArrayList<?> arrayList =mydb.getallProducts();
@SuppressWarnings("unchecked")
ArrayAdapter adp =new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(adp);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stu
//Toast.makeText(getApplicationContext(), ""+arg2, Toast.LENGTH_SHORT).show();
int idtosearch =arg2+1;
Bundle databundle= new Bundle();
databundle.putInt("id",idtosearch);
Intent intent = new Intent(MainProducts.this,AddActivity.class);
intent.putExtras(databundle);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.add1:
Bundle extras = new Bundle();
Toast.makeText(getApplicationContext(), "ID "+extras.getInt("id"), Toast.LENGTH_SHORT).show();
extras.putInt("id", 0);
Intent intent = new Intent(MainProducts.this,com.example.products2.AddActivity.class);
intent.putExtras(extras);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
and this class shows adds and edit the items , AddActivity.java
public class AddActivity extends Activity {
int idtoupdate=0;
private DBhelper mydb;
TextView name;
TextView quant;
TextView price;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
name = (TextView) findViewById(R.id.editTextname);
quant = (TextView) findViewById(R.id.editTextquant);
price = (TextView)findViewById(R.id.editTextprice);
btn1=(Button)findViewById(R.id.button1);
mydb =new DBhelper(this);
Bundle extras =getIntent().getExtras();
if(extras!=null)
{
int Value=extras.getInt("id");
if(Value>0)
{
Cursor rs=mydb.getData(Value);
Toast.makeText(getApplicationContext(), "Val "+Value, Toast.LENGTH_SHORT).show();
idtoupdate=Value;
rs.moveToFirst();
String nam = rs.getString(rs.getColumnIndex(DBhelper.PRODUCTS_COLUMN_NAME));
String quan = rs.getString(rs.getColumnIndex(DBhelper.PRODUCTS_COLUMN_QUAN));
String pric = rs.getString(rs.getColumnIndex(DBhelper.PRODUCTS_COLUMN_PRICE));
if (!rs.isClosed())
{
rs.close();
}
//Button b =(Button) findViewById(R.id.button1);
btn1.setVisibility(View.INVISIBLE);
name.setText((CharSequence)nam);
//name.setEnabled(true);
name.setFocusable(false);
name.setClickable(false);
quant.setText((CharSequence)quan);
//quant.setEnabled(true);
quant.setFocusable(false);
quant.setClickable(false);
price.setText((CharSequence)pric);
//quant.setEnabled(true);
price.setFocusable(false);
price.setClickable(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
int Value = extras.getInt("id");
if(Value>0){
getMenuInflater().inflate(R.menu.addproducts, menu);
}
else{
getMenuInflater().inflate(R.menu.main, menu);
}
}
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.item1:
Button b= (Button) findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);
name.setEnabled(true);
name.setFocusableInTouchMode(true);
name.setClickable(true);
quant.setEnabled(true);
quant.setFocusableInTouchMode(true);
quant.setClickable(true);
price.setEnabled(true);
price.setFocusableInTouchMode(true);
price.setClickable(true);
return true;
case R.id.item2:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteProduct)
.setPositiveButton(R.string.yes,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
mydb.deleteproduct(idtoupdate);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_LONG).show();
Intent i= new Intent(AddActivity.this,MainProducts.class);
startActivity(i);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
}
});
AlertDialog d= builder.create();
d.setTitle("Are you sure?");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void run(View view)
{
Bundle extras = getIntent().getExtras();
if(extras !=null)
{int Value = extras.getInt("id");
if(Value>0){
if(mydb.updateProducts(idtoupdate,name.getText().toString(), price.getText().toString(), quant.getText().toString())){
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AddActivity.this,com.example.products2.MainProducts.class);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
}
}
else{
if(mydb.insertProduct(name.getText().toString(), price.getText().toString(), quant.getText().toString())){
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(AddActivity.this,com.example.products2.MainProducts.class);
startActivity(intent);
}
}
}
}
Please help I didnt find any help on internet regarding same . Thanx in advance and its my first question sorry if i made any mistake please correct it if found necessary.
Aucun commentaire:
Enregistrer un commentaire