Hi I want to delete selected item from the data base. My code is
**Deletion.java**
package com.my.project;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
impor t android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class Deletion extends Activity
{
SharedPreferences my_Shared_Data;
SQLiteDatabase myDelete=null;
String DataBase_Name="deletedata";
String Table_Name="deletedetails";
Cursor c1,c2;
ListView lv1;
ArrayList<String> nameArray;
Spinner deleterecord;
ArrayAdapter<String> deleteAdapter;
private static String[] aname;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.delete);
lv1=(ListView)findViewById(R.id.list);
try{
myDelete=this.openOrCreateDatabase(DataBase_Name, MODE_PRIVATE, null);
System.out.println("databse has been creates.....");
myDelete.execSQL("create table if not exists " +Table_Name+ "(name varchar(50))");
System.out.println("table has been created.....");
c1=myDelete.rawQuery("select * from "+Table_Name , null);
c1.moveToFirst();
int count1=c1.getCount();
System.out.println("columns --->"+count1);
if(count1==1)
{
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('asha')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('karthick')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('arunthathi')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('vasanth')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('nithyakavini')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('abhimanyu')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('charles')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('samyuktha')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('vijayalayan')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('samantha')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('shasvathi')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('yazhini')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('kannan')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('mirunalini')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('adhavan')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('subhathra')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('muhammad')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('mayadevi')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('bhazeer')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('aadhityaa')");
myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('vijaynarayanan')");
System.out.println("data has been inserted.....");
}
c2=myDelete.rawQuery("select * from "+Table_Name , null);
c2.moveToFirst();
int count2=c2.getCount();
aname=new String[count2];
System.out.println("columns --->"+count2);
//final int column1=c2.getColumnIndex("name");
nameArray= new ArrayList<String>();
for(int i=0;i<count2;i++)
{
aname[i]=c2.getString(c2.getColumnIndex("name"));
c2.moveToNext();
nameArray.add(aname[i]);
//System.out.println(aname[i]);
}
for( int j = 0 ; j < nameArray.size();j++)
{
System.out.println(nameArray.get(j));
}
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nameArray));
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
}
});
}
catch (Exception e)
{
e.printStackTrace();
}}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.delete: Toast.makeText(this, "You pressed the delete menu!", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(Deletion.this,DeleteData.class);
setResult(Activity.RESULT_OK);
myIntent.putExtra("my_name", nameArray);
finish();
startActivity(myIntent);
break;
case R.id.cancel: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
The above java class shows the data base items in the list view. Now going to select the items to delete.
**DeleteData.java**
package com.my.project;
import java.util.ArrayList;
public class DeleteData extends Activity
{
private static String[] select_data;
private static String[] data_base_data;
ArrayList<String> data_base_Array;
ArrayList<String> my_name1;
ListView lv2;
Button delete;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.deletedata);
delete = (Button)findViewById(R.id.getchoice);
Intent sender=getIntent();
my_name1= new ArrayList<String>();
my_name1=this.getIntent().getStringArrayListExtra("my_name");
for( int k = 0 ; k < my_name1.size();k++)
{
System.out.println(my_name1.get(k));
}
lv2=(ListView)findViewById(R.id.deletelist);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,my_name1);
// lv2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,my_name1));
lv2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv2.setAdapter(adapter);
lv2.setTextFilterEnabled(true);
lv2.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
}
});
delete.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String selected = "";
int cntChoice = lv2.getCount();
System.out.println(cntChoice);
my_name1= new ArrayList<String>();
SparseBooleanArray sparseBooleanArray = lv2.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++)
{
if(sparseBooleanArray.get(i))
{
selected += lv2.getItemAtPosition(i).toString() + "\n";
// adapter.remove(selected);
/* select_data[i]=lv2.getItemAtPosition(i).toString();
my_name1.add(select_data[i]);*/
}
}
/* for( int j = 0 ; j < my_name1.size();j++)
{
System.out.println("checked data-->"+my_name1.get(j));
}*/
System.out.println("seleted items for deletion"+selected);
Toast.makeText(DeleteData.this,selected,Toast.LENGTH_LONG).show();
for(int i=0;i<cntChoice;i++)
{
System.out.println(i);
}
}});
}
}
}
It displays the checked items in toast. Now I want to delete the selected items. I can’t store this selected items into Array List. how can i do this.Any body help me? Thanks in advance.
Your approach to how you are using these “selected” items is incorrect. You should be having checkboxes that are found in your listview, upon the click of done or back then these items should be found in the ListView.
Meaning all those that were selected (ideally you’d keep track of this via a list at runtime) and then done is clicked, then you submit and remove all these items from the Listview’s adapter and then notify the listview that data has changed.
Your current approach of Iterating through them is not a good approach, You should be using the fact that each item maps to a particular ID that points to a particular row in the database.
Use this list and remove the rows as necessary.