您好我想从数据库中删除所选项目。我的代码是
**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;
}
}
上面的java类显示了列表视图中的数据库项。现在要选择要删除的项目。
**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);
}
}});
}
}
}
它以吐司形式显示已检查的项目。现在我想删除所选项目。我无法将此选定项目存储到数组列表中。我怎么能这样做。任何身体帮助我?提前谢谢。
答案 0 :(得分:0)
您使用这些“已选择”项目的方法不正确。您应该在列表视图中找到复选框,点击完成或返回后,这些项目应该在ListView中找到。
意味着所有被选中的人(理想情况下你会在运行时通过列表跟踪),然后单击,然后你从Listview的适配器提交并删除所有这些项目,然后通知listview数据有改变。
您当前迭代它们的方法不是一个好方法,您应该使用每个项目映射到指向数据库中特定行的特定ID的事实。
使用此列表并根据需要删除行。