目录

Android ListView 添加插入数据

添加记录

在指定位置插入数据

Android ListView 删除数据

ListView 删除数据

ListView 清空数据

Android ListView 更改数据

ListView 数据更新

Android ListView 查询数据

ListView 数据查询


Android ListView 添加插入数据

添加记录

我们在顶部添加一个按钮,每次点击添加一条记录,并且数据为空时提示用户没数据

XML 布局文件(activity_main.xml):

 

Java 代码(MainActivity.java):

package com.example.myapplication;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private ArrayList dataList;private ArrayAdapter adapter;private TextView noDataText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button addButton = findViewById(R.id.addButton);ListView listView = findViewById(R.id.listView);noDataText = findViewById(R.id.noDataText);dataList = new ArrayList();adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList);listView.setAdapter(adapter);addButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addRecord();}});}private void addRecord() {// 在这里添加记录dataList.add("新建记录 " + (dataList.size() + 1));adapter.notifyDataSetChanged();// 如果有数据,则隐藏文本消息if (!dataList.isEmpty()) {noDataText.setVisibility(View.GONE);}}@Overrideprotected void onResume() {super.onResume();// 在 onResume 中检查数据列表是否为空,并根据情况显示或隐藏文本消息if (dataList.isEmpty()) {noDataText.setVisibility(View.VISIBLE);} else {noDataText.setVisibility(View.GONE);}}}

在指定位置插入数据

要在指定位置插入数据,可以添加一个编辑框让用户输入要插入的位置,然后添加一个按钮来触发插入操作。

XML 布局文件(activity_main.xml):

 

Java 代码(MainActivity.java):

package com.example.myapplication;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private ArrayList dataList;private ArrayAdapter adapter;private TextView noDataText;private EditText positionEditText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button addButton = findViewById(R.id.addButton);Button insertButton = findViewById(R.id.insertButton);ListView listView = findViewById(R.id.listView);noDataText = findViewById(R.id.noDataText);positionEditText = findViewById(R.id.positionEditText);dataList = new ArrayList();adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList);listView.setAdapter(adapter);addButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addRecord();}});insertButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {insertRecord();}});}private void addRecord() {dataList.add("新建记录 " + (dataList.size() + 1));adapter.notifyDataSetChanged();toggleNoDataTextVisibility();}private void insertRecord() {try {int position = Integer.parseInt(positionEditText.getText().toString());if (position >= 0 && position <= dataList.size()) {dataList.add(position, "插入记录 " + (dataList.size() + 1));adapter.notifyDataSetChanged();toggleNoDataTextVisibility();} else {Toast.makeText(this, "位置无效", Toast.LENGTH_SHORT).show();}} catch (NumberFormatException e) {Toast.makeText(this, "请输入有效的位置", Toast.LENGTH_SHORT).show();}}private void toggleNoDataTextVisibility() {if (dataList.isEmpty()) {noDataText.setVisibility(View.VISIBLE);} else {noDataText.setVisibility(View.GONE);}}}

Android ListView 删除数据

ListView 删除数据

从 ListView 中删除某项数据有两种方法

  • 根据对象删除
  • 根据游标(position) 删除

例子:

XML 布局文件(activity_main.xml):

 

Java 代码(MainActivity.java):

package com.example.myapplication;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private ArrayList dataList;private ArrayAdapter adapter;private TextView noDataText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button addButton = findViewById(R.id.addButton);Button deleteByObjectButton = findViewById(R.id.deleteByObjectButton);Button deleteByPositionButton = findViewById(R.id.deleteByPositionButton);ListView listView = findViewById(R.id.listView);noDataText = findViewById(R.id.noDataText);dataList = new ArrayList();adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList);listView.setAdapter(adapter);addButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addRecord();}});deleteByObjectButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {deleteByObject();}});deleteByPositionButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {deleteByPosition();}});}private void addRecord() {dataList.add("新建记录 " + (dataList.size() + 1));adapter.notifyDataSetChanged();toggleNoDataTextVisibility();}private void deleteByObject() {if (!dataList.isEmpty()) {// 根据对象删除最后一项数据dataList.remove(dataList.size() - 1);adapter.notifyDataSetChanged();toggleNoDataTextVisibility();} else {Toast.makeText(this, "无数据可删除", Toast.LENGTH_SHORT).show();}}private void deleteByPosition() {if (!dataList.isEmpty()) {// 根据游标(position)删除第一项数据dataList.remove(0);adapter.notifyDataSetChanged();toggleNoDataTextVisibility();} else {Toast.makeText(this, "无数据可删除", Toast.LENGTH_SHORT).show();}}private void toggleNoDataTextVisibility() {if (dataList.isEmpty()) {noDataText.setVisibility(View.VISIBLE);} else {noDataText.setVisibility(View.GONE);}}}

ListView 清空数据

清空 ListView 数据有两种方式

  • 调用 remove() 方法一项一项的删除
  • 调用 clear() 方法一次性删除

下面我们就写二个按钮 来演示下这两种方法

XML 布局文件(activity_main.xml):

 

Java 代码(MainActivity.java):

package com.example.myapplication;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private ArrayList dataList;private ArrayAdapter adapter;private TextView noDataText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button addButton = findViewById(R.id.addButton);Button removeOneByOneButton = findViewById(R.id.removeOneByOneButton);Button clearAllButton = findViewById(R.id.clearAllButton);ListView listView = findViewById(R.id.listView);noDataText = findViewById(R.id.noDataText);dataList = new ArrayList();adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList);listView.setAdapter(adapter);addButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addRecord();}});removeOneByOneButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {removeOneByOne();}});clearAllButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {clearAll();}});}private void addRecord() {dataList.add("新建记录 " + (dataList.size() + 1));adapter.notifyDataSetChanged();toggleNoDataTextVisibility();}private void removeOneByOne() {if (!dataList.isEmpty()) {dataList.remove(dataList.size() - 1);adapter.notifyDataSetChanged();toggleNoDataTextVisibility();} else {Toast.makeText(this, "无数据可删除", Toast.LENGTH_SHORT).show();}}private void clearAll() {if (!dataList.isEmpty()) {dataList.clear();adapter.notifyDataSetChanged();toggleNoDataTextVisibility();} else {Toast.makeText(this, "无数据可清空", Toast.LENGTH_SHORT).show();}}private void toggleNoDataTextVisibility() {if (dataList.isEmpty()) {noDataText.setVisibility(View.VISIBLE);} else {noDataText.setVisibility(View.GONE);}}}

Android ListView 更改数据

ListView 数据更新

更新 ListView 中的数据有两种办法

  • 根据对象更新
  • 根据游标(pos) 更新

例子:

XML 布局文件(activity_main.xml):

 

Java 代码(MainActivity.java):

package com.example.myapplication;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private ArrayList dataList;private ArrayAdapter adapter;private ListView listView;private TextView noDataText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button addButton = findViewById(R.id.addButton);Button updateByObjectButton = findViewById(R.id.updateByObjectButton);Button updateByPositionButton = findViewById(R.id.updateByPositionButton);listView = findViewById(R.id.listView);noDataText = findViewById(R.id.noDataText);dataList = new ArrayList();adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList);listView.setAdapter(adapter);addButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addRecord();}});updateByObjectButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {updateByObject();}});updateByPositionButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {updateByPosition();}});}private void addRecord() {dataList.add("新记录 " + (dataList.size() + 1));adapter.notifyDataSetChanged();toggleNoDataTextVisibility();}private void updateByObject() {if (!dataList.isEmpty()) {// 根据对象更新第一个数据项dataList.set(0, "更新的记录 1");adapter.notifyDataSetChanged();Toast.makeText(this, "根据对象更新成功", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "没有可更新的数据", Toast.LENGTH_SHORT).show();}}private void updateByPosition() {if (!dataList.isEmpty()) {// 根据游标更新第二个数据项int position = 1; // 第二个位置if (position >= 0 && position < dataList.size()) {dataList.set(position, "更新的记录 2");adapter.notifyDataSetChanged();Toast.makeText(this, "根据游标更新成功", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "位置无效", Toast.LENGTH_SHORT).show();}} else {Toast.makeText(this, "没有可更新的数据", Toast.LENGTH_SHORT).show();}}private void toggleNoDataTextVisibility() {if (dataList.isEmpty()) {noDataText.setVisibility(View.VISIBLE);} else {noDataText.setVisibility(View.GONE);}}}

Android ListView 查询数据

ListView 数据查询

例子:

XML 布局文件(activity_main.xml):

 

Java 代码(MainActivity.java):

package com.example.myapplication;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {private ArrayList dataList;private ArrayList originalDataList; // 保存原始数据private ArrayAdapter adapter;private ListView listView;private TextView noDataText;private EditText searchEditText;private Button searchButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button addButton = findViewById(R.id.addButton);searchEditText = findViewById(R.id.searchEditText);searchButton = findViewById(R.id.searchButton);listView = findViewById(R.id.listView);noDataText = findViewById(R.id.noDataText);dataList = new ArrayList();originalDataList = new ArrayList(); // 初始化原始数据列表adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList);listView.setAdapter(adapter);addButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addRecord();}});searchButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {searchRecords();}});searchEditText.addTextChangedListener(textWatcher);}private final TextWatcher textWatcher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {// 每次文本发生变化时不自动查询}};private void addRecord() {originalDataList.add("新记录 " + (originalDataList.size() + 1)); // 添加记录到原始数据列表if (searchEditText.getText().toString().isEmpty()) {// 如果查询条件为空,则更新列表updateList(originalDataList);}searchRecords(); // 执行查询操作}private void searchRecords() {String query = searchEditText.getText().toString().trim().toLowerCase();List filteredList = new ArrayList();for (String record : originalDataList) {if (record.toLowerCase().contains(query)) {filteredList.add(record);}}updateList(filteredList);}private void updateList(List records) {dataList.clear();dataList.addAll(records);adapter.notifyDataSetChanged();toggleNoDataTextVisibility();}private void toggleNoDataTextVisibility() {noDataText.setVisibility(dataList.isEmpty() ? View.VISIBLE : View.GONE);}}