こんにちは私はデータベースから選択した項目を削除します。私のコードはアンドロイドのデータベースからチェック項目を削除するには?
**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);
}
}});
}
}
}
これは、トーストでチェックされた項目を表示します。今度は選択したアイテムを削除します。この選択した項目を配列リストに保存することはできません。どうすればいいの?私の体は私を助けますか?前もって感謝します。
選択したアイテムをリストに保存しない理由はありますか?私はあなたがリストにそれらを格納し、このリストに格納されているすべてのアイテムをデータベース上のdelete関数を呼び出すことをお勧めします。 – EarlOfEgo