Android studio连接sqlite的ListView的删除代码
时间: 2024-06-11 07:07:16 浏览: 16
以下是一个简单的Android Studio连接SQLite的ListView的删除代码:
1. 在布局文件中添加ListView:
```
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
```
2. 在MainActivity.java中,定义一个ArrayList和一个CustomAdapter:
```
public class MainActivity extends AppCompatActivity {
private ArrayList<Item> itemList;
private CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemList = new ArrayList<>();
customAdapter = new CustomAdapter(this, itemList);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(customAdapter);
}
}
```
3. 定义一个Item类,包含需要显示的数据:
```
public class Item {
private int id;
private String name;
private String description;
public Item(int id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
```
4. 定义一个CustomAdapter类,用于为ListView提供数据:
```
public class CustomAdapter extends BaseAdapter {
private Context context;
private ArrayList<Item> itemList;
public CustomAdapter(Context context, ArrayList<Item> itemList) {
this.context = context;
this.itemList = itemList;
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return itemList.get(position);
}
@Override
public long getItemId(int position) {
return itemList.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, null);
}
TextView nameTextView = view.findViewById(R.id.nameTextView);
TextView descriptionTextView = view.findViewById(R.id.descriptionTextView);
Item item = itemList.get(position);
nameTextView.setText(item.getName());
descriptionTextView.setText(item.getDescription());
return view;
}
public void removeItem(int position) {
itemList.remove(position);
notifyDataSetChanged();
}
}
```
5. 在MainActivity.java中,添加一个方法用于从SQLite数据库中加载数据并显示在ListView中:
```
private void loadItems() {
itemList.clear();
DatabaseHelper databaseHelper = new DatabaseHelper(this);
SQLiteDatabase database = databaseHelper.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM items", null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String description = cursor.getString(cursor.getColumnIndex("description"));
Item item = new Item(id, name, description);
itemList.add(item);
} while (cursor.moveToNext());
}
cursor.close();
database.close();
customAdapter.notifyDataSetChanged();
}
```
6. 在MainActivity.java的onCreate方法中调用loadItems方法,以便在应用程序启动时加载数据:
```
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemList = new ArrayList<>();
customAdapter = new CustomAdapter(this, itemList);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(customAdapter);
loadItems();
}
```
7. 在CustomAdapter类中添加一个方法removeItem,用于从ListView和SQLite数据库中删除项目:
```
public void removeItem(int position) {
itemList.remove(position);
notifyDataSetChanged();
DatabaseHelper databaseHelper = new DatabaseHelper(context);
SQLiteDatabase database = databaseHelper.getWritableDatabase();
int id = itemList.get(position).getId();
database.delete("items", "id = ?", new String[]{String.valueOf(id)});
database.close();
}
```
8. 在MainActivity.java中,为ListView添加一个OnItemLongClickListener,以便在长按列表项时进行删除操作:
```
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
customAdapter.removeItem(position);
return true;
}
});
```
这样,当用户长按ListView中的项时,它将从ListView和SQLite数据库中删除。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)