【发布时间】:2017-01-29 15:42:28
【问题描述】:
我想我找到了原因/或原因之一,为什么从列表视图中选择的项目不会从数据库中删除自身。创建数据库行时,每行获取ID,每次新行ID+1。 我会用例子来说明问题:
我的数据库中有 5 行(5 名员工),每个员工在添加到数据库时都会自动获得 ID。所以我有 ID 的 1,2,3,4,5。当我删除第一行(ID = 1)时,我剩下 4 行 - 2、3、4、5,并且它们不会将自己“重构”为 1、2、3、4。因此,使用“onItemClick”方法中的“long id”参数不起作用,因为我得到 ID=0 用于单击列表中的第一个雇员,但我的数据库中的第一个雇员没有 ID=0,但 ID=2因为我之前删除了 ID=1 的员工。单击列表中的第二个雇员,我得到 ID=1,而数据库中的第二个雇员的 ID=3。
有没有办法让数据库始终使用从 1 到 -n 员工的 ID,这意味着如果我从示例中删除 ID=1 的员工,有没有办法让数据库更改 ID,所以它不是 2 ,3,4,5 删除 ID=1 后,又是 1,2,3,4??
我检查了与我的问题类似的所有其他问题和答案,但没有一个对我有用。 在 MainActivity 单击按钮时,它会切换到第二个活动和第二个布局。在第二个活动中,我使用适配器来显示数据库列表。我的列表在那里,但我无法单击任何项目并将其删除。我尝试了侦听器、listActivity 等,但没有任何效果。我还在学习中,请耐心等待。
我想要在布局中显示此列表,当用户单击其中一项时,系统会询问他是否要删除,如果他选择是,则从列表视图和数据库中删除所选项目。
我的第二个活动是:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
public class RemoveEmploye extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.del_layout);
final Database db=new Database(this);
final ListView lv = (ListView)findViewById(R.id.delEmployeList);
List<Employe> employes = db.selectAll();
ArrayAdapter<Employe> adapter = new ArrayAdapter<>
(this,android.R.layout.simple_list_item_1, android.R.id.text1, employes);
lv.setAdapter(adapter);
}
public void goBack(View v) {
Intent in= new Intent(RemoveEmploye.this,MainActivity.class);
startActivity(in);
}
我的第二个布局是(del_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.EmDatabase.RemoveEmploye">
<ListView
android:layout_width="wrap_content"
android:layout_height="527dp"
android:id="@+id/delEmployeList">
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/back"
android:clickable="true"
android:onClick="goBack"/>
</LinearLayout>
我的数据库 selectAll() 是:
public List<Employe> selectAll() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("EmployeDB", new String[] { ID, NAME, SURNAME, AGE,
COMPANY, WTYPE}, null, new String[] { }, null, null, null, null);
String name="";
String surname="";
String age="";
String company="";
String wtype="";
int id=0;
ArrayList<Employe> employes =new ArrayList<Employe>();
if(cursor.moveToFirst()) {
do {
id= Integer.parseInt(cursor.getString(0));
name= cursor.getString(1);
surname= cursor.getString(2);
age= cursor.getString(3);
company= cursor.getString(4);
wtype= cursor.getString(5);
Employe e= new Employe();
e.setId(id);
e.setName(name);
e.setSurname(surname);
e.setAge(Integer.parseInt(age));
e.setCompany(company);
e.setWorktype(wtype);
employes.add(e);
} while (cursor.moveToNext());
}
return employes;
}
【问题讨论】:
-
请提供您的列表项的 xml
-
Malik,我提供了用于显示列表视图的 del_layout.xml 文件,它在我的消息中命名为 Second_layout
-
啊,不。现在我明白了……您使用的是 android listViewItem。你必须让你自己的 listViewItem 布局,然后你可以使用我的 awnser
-
欲了解更多信息,请查看here 或在 google 上搜索自定义列表视图
-
Malik,我在谷歌和网站上查过你发给我的。我只找到了没有数据库的示例,还有更多我目前了解的选项。正如我已经说过的,我还在学习,这是我的第一个“更大”的应用程序项目。我想知道您是否可以制作我需要的自定义列表视图?很抱歉耽误你的时间,但我真的很纠结这个问题,我需要尽快完成这项任务。