赞
踩
目录
我们在顶部添加一个按钮,每次点击添加一条记录,并且数据为空时提示用户没数据
XML 布局文件(activity_main.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <Button
- android:id="@+id/addButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="添加记录"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="16dp"/>
-
- <ListView
- android:id="@+id/listView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@id/addButton"
- android:dividerHeight="1dp"/>
-
- <TextView
- android:id="@+id/noDataText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="无可用数据"
- android:layout_centerInParent="true"
- android:visibility="gone"/> <!-- 初始设置为隐藏 -->
-
- </RelativeLayout>
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<String> dataList;
- private ArrayAdapter<String> adapter;
- private TextView noDataText;
-
- @Override
- protected 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() {
- @Override
- public void onClick(View v) {
- addRecord();
- }
- });
- }
-
- private void addRecord() {
- // 在这里添加记录
- dataList.add("新建记录 " + (dataList.size() + 1));
- adapter.notifyDataSetChanged();
-
- // 如果有数据,则隐藏文本消息
- if (!dataList.isEmpty()) {
- noDataText.setVisibility(View.GONE);
- }
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- // 在 onResume 中检查数据列表是否为空,并根据情况显示或隐藏文本消息
- if (dataList.isEmpty()) {
- noDataText.setVisibility(View.VISIBLE);
- } else {
- noDataText.setVisibility(View.GONE);
- }
- }
- }
要在指定位置插入数据,可以添加一个编辑框让用户输入要插入的位置,然后添加一个按钮来触发插入操作。
XML 布局文件(activity_main.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <Button
- android:id="@+id/addButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="添加记录"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="16dp"/>
-
- <EditText
- android:id="@+id/positionEditText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/addButton"
- android:layout_marginTop="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginEnd="16dp"
- android:hint="要插入的位置"
- android:inputType="number"/>
-
- <Button
- android:id="@+id/insertButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="在指定位置插入"
- android:layout_below="@id/positionEditText"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="16dp"/>
-
- <ListView
- android:id="@+id/listView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@id/insertButton"
- android:dividerHeight="1dp"/>
-
- <TextView
- android:id="@+id/noDataText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="无可用数据"
- android:layout_centerInParent="true"
- android:visibility="gone"/> <!-- 初始设置为隐藏 -->
-
- </RelativeLayout>
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<String> dataList;
- private ArrayAdapter<String> adapter;
- private TextView noDataText;
- private EditText positionEditText;
-
- @Override
- protected 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() {
- @Override
- public void onClick(View v) {
- addRecord();
- }
- });
-
- insertButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public 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);
- }
- }
- }
从 ListView 中删除某项数据有两种方法
例子:
XML 布局文件(activity_main.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <Button
- android:id="@+id/addButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="添加记录"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="16dp"/>
-
- <Button
- android:id="@+id/deleteByObjectButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="对象删除"
- android:layout_below="@id/addButton"
- android:layout_alignParentStart="true"
- android:layout_marginStart="16dp"
- android:layout_marginTop="16dp"/>
-
- <Button
- android:id="@+id/deleteByPositionButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="游标删除"
- android:layout_below="@id/addButton"
- android:layout_alignParentEnd="true"
- android:layout_marginEnd="16dp"
- android:layout_marginTop="16dp"/>
-
- <ListView
- android:id="@+id/listView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@id/deleteByObjectButton"
- android:dividerHeight="1dp"/>
-
- <TextView
- android:id="@+id/noDataText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="无可用数据"
- android:layout_centerInParent="true"
- android:visibility="gone"/> <!-- 初始设置为隐藏 -->
-
- </RelativeLayout>
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<String> dataList;
- private ArrayAdapter<String> adapter;
- private TextView noDataText;
-
- @Override
- protected 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() {
- @Override
- public void onClick(View v) {
- addRecord();
- }
- });
-
- deleteByObjectButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- deleteByObject();
- }
- });
-
- deleteByPositionButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public 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 数据有两种方式
下面我们就写二个按钮 来演示下这两种方法
XML 布局文件(activity_main.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <Button
- android:id="@+id/addButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="添加记录"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="16dp"/>
-
- <Button
- android:id="@+id/removeOneByOneButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="逐个删除"
- android:layout_below="@id/addButton"
- android:layout_alignParentStart="true"
- android:layout_marginStart="16dp"
- android:layout_marginTop="16dp"/>
-
- <Button
- android:id="@+id/clearAllButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="一次性删除"
- android:layout_below="@id/addButton"
- android:layout_alignParentEnd="true"
- android:layout_marginEnd="16dp"
- android:layout_marginTop="16dp"/>
-
- <ListView
- android:id="@+id/listView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@id/removeOneByOneButton"
- android:dividerHeight="1dp"/>
-
- <TextView
- android:id="@+id/noDataText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="无可用数据"
- android:layout_centerInParent="true"
- android:visibility="gone"/> <!-- 初始设置为隐藏 -->
-
- </RelativeLayout>
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<String> dataList;
- private ArrayAdapter<String> adapter;
- private TextView noDataText;
-
- @Override
- protected 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() {
- @Override
- public void onClick(View v) {
- addRecord();
- }
- });
-
- removeOneByOneButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- removeOneByOne();
- }
- });
-
- clearAllButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public 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);
- }
- }
- }
更新 ListView 中的数据有两种办法
例子:
XML 布局文件(activity_main.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <Button
- android:id="@+id/addButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="添加记录"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="16dp"/>
-
- <Button
- android:id="@+id/updateByObjectButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="根据对象更新"
- android:layout_below="@id/addButton"
- android:layout_alignParentStart="true"
- android:layout_marginTop="16dp"/>
-
- <Button
- android:id="@+id/updateByPositionButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="根据游标更新"
- android:layout_below="@id/addButton"
- android:layout_alignParentEnd="true"
- android:layout_marginTop="16dp"/>
-
- <ListView
- android:id="@+id/listView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@id/updateByObjectButton"
- android:dividerHeight="1dp"/>
-
- <TextView
- android:id="@+id/noDataText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="无可用数据"
- android:layout_centerInParent="true"
- android:visibility="gone"/> <!-- 初始设置为隐藏 -->
-
- </RelativeLayout>
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<String> dataList;
- private ArrayAdapter<String> adapter;
- private ListView listView;
- private TextView noDataText;
-
- @Override
- protected 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() {
- @Override
- public void onClick(View v) {
- addRecord();
- }
- });
-
- updateByObjectButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- updateByObject();
- }
- });
-
- updateByPositionButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public 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);
- }
- }
- }
例子:
XML 布局文件(activity_main.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <Button
- android:id="@+id/addButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="添加记录"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="16dp"/>
-
- <EditText
- android:id="@+id/searchEditText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入查询条件"
- android:layout_below="@id/addButton"
- android:layout_marginTop="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginEnd="16dp"/>
-
- <Button
- android:id="@+id/searchButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="查询"
- android:layout_below="@id/searchEditText"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="16dp"/>
-
- <ListView
- android:id="@+id/listView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@id/searchButton"
- android:dividerHeight="1dp"/>
-
- <TextView
- android:id="@+id/noDataText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="无可用数据"
- android:layout_centerInParent="true"
- android:visibility="gone"/> <!-- 初始设置为隐藏 -->
-
- </RelativeLayout>
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<String> dataList;
- private ArrayList<String> originalDataList; // 保存原始数据
- private ArrayAdapter<String> adapter;
- private ListView listView;
- private TextView noDataText;
- private EditText searchEditText;
- private Button searchButton;
-
- @Override
- protected 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() {
- @Override
- public void onClick(View v) {
- addRecord();
- }
- });
-
- searchButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- searchRecords();
- }
- });
-
- searchEditText.addTextChangedListener(textWatcher);
- }
-
- private final TextWatcher textWatcher = new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
-
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {}
-
- @Override
- public 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<String> filteredList = new ArrayList<>();
- for (String record : originalDataList) {
- if (record.toLowerCase().contains(query)) {
- filteredList.add(record);
- }
- }
- updateList(filteredList);
- }
-
- private void updateList(List<String> records) {
- dataList.clear();
- dataList.addAll(records);
- adapter.notifyDataSetChanged();
- toggleNoDataTextVisibility();
- }
-
- private void toggleNoDataTextVisibility() {
- noDataText.setVisibility(dataList.isEmpty() ? View.VISIBLE : View.GONE);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。