gpt4 book ai didi

java - 如何从android数据库中删除选中的项目?

转载 作者:行者123 更新时间:2023-12-01 05:31:07 26 4
gpt4 key购买 nike

您好,我想从数据库中删除所选项目。我的代码是

  **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类在 ListView 中显示数据库项目。现在要选择要删除的项目。

   **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);
}

}});
}

}
}

显示 toast 中选中的项目。现在我想删除选定的项目。我无法将此选定的项目存储到数组列表中。我该怎么做?有人帮助我吗?提前致谢。

最佳答案

您使用这些“选定”项目的方法是不正确的。您应该在 ListView 中找到复选框,单击完成或返回后,这些项目应该在 ListView 中找到。

意味着所有被选择的项目(理想情况下你会在运行时通过列表跟踪它),然后单击“完成”,然后你提交并从 ListView 的适配器中删除所有这些项目,然后通知 ListView 数据已完成改变了。

您当前迭代它们的方法不是一个好方法,您应该使用每个项目映射到指向数据库中特定行的特定 ID 的事实。

使用此列表并根据需要删除行。

关于java - 如何从android数据库中删除选中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8952018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com