当前位置:   article > 正文

学习Android的第十七天

学习Android的第十七天

目录

Android ListView 添加插入数据

添加记录

在指定位置插入数据

Android ListView 删除数据

ListView 删除数据

ListView 清空数据

Android ListView 更改数据

ListView 数据更新

Android ListView 查询数据

ListView 数据查询


Android ListView 添加插入数据

添加记录

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

XML 布局文件(activity_main.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <Button
  8. android:id="@+id/addButton"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="添加记录"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerHorizontal="true"
  14. android:layout_marginTop="16dp"/>
  15. <ListView
  16. android:id="@+id/listView"
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:layout_below="@id/addButton"
  20. android:dividerHeight="1dp"/>
  21. <TextView
  22. android:id="@+id/noDataText"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="无可用数据"
  26. android:layout_centerInParent="true"
  27. android:visibility="gone"/> <!-- 初始设置为隐藏 -->
  28. </RelativeLayout>

Java 代码(MainActivity.java): 

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.Button;
  6. import android.widget.ListView;
  7. import android.widget.TextView;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. import java.util.ArrayList;
  10. public class MainActivity extends AppCompatActivity {
  11. private ArrayList<String> dataList;
  12. private ArrayAdapter<String> adapter;
  13. private TextView noDataText;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. Button addButton = findViewById(R.id.addButton);
  19. ListView listView = findViewById(R.id.listView);
  20. noDataText = findViewById(R.id.noDataText);
  21. dataList = new ArrayList<>();
  22. adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
  23. listView.setAdapter(adapter);
  24. addButton.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. addRecord();
  28. }
  29. });
  30. }
  31. private void addRecord() {
  32. // 在这里添加记录
  33. dataList.add("新建记录 " + (dataList.size() + 1));
  34. adapter.notifyDataSetChanged();
  35. // 如果有数据,则隐藏文本消息
  36. if (!dataList.isEmpty()) {
  37. noDataText.setVisibility(View.GONE);
  38. }
  39. }
  40. @Override
  41. protected void onResume() {
  42. super.onResume();
  43. // 在 onResume 中检查数据列表是否为空,并根据情况显示或隐藏文本消息
  44. if (dataList.isEmpty()) {
  45. noDataText.setVisibility(View.VISIBLE);
  46. } else {
  47. noDataText.setVisibility(View.GONE);
  48. }
  49. }
  50. }

在指定位置插入数据

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

XML 布局文件(activity_main.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <Button
  8. android:id="@+id/addButton"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="添加记录"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerHorizontal="true"
  14. android:layout_marginTop="16dp"/>
  15. <EditText
  16. android:id="@+id/positionEditText"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_below="@id/addButton"
  20. android:layout_marginTop="16dp"
  21. android:layout_marginStart="16dp"
  22. android:layout_marginEnd="16dp"
  23. android:hint="要插入的位置"
  24. android:inputType="number"/>
  25. <Button
  26. android:id="@+id/insertButton"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="在指定位置插入"
  30. android:layout_below="@id/positionEditText"
  31. android:layout_centerHorizontal="true"
  32. android:layout_marginTop="16dp"/>
  33. <ListView
  34. android:id="@+id/listView"
  35. android:layout_width="match_parent"
  36. android:layout_height="match_parent"
  37. android:layout_below="@id/insertButton"
  38. android:dividerHeight="1dp"/>
  39. <TextView
  40. android:id="@+id/noDataText"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="无可用数据"
  44. android:layout_centerInParent="true"
  45. android:visibility="gone"/> <!-- 初始设置为隐藏 -->
  46. </RelativeLayout>

Java 代码(MainActivity.java):

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.ListView;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import java.util.ArrayList;
  12. public class MainActivity extends AppCompatActivity {
  13. private ArrayList<String> dataList;
  14. private ArrayAdapter<String> adapter;
  15. private TextView noDataText;
  16. private EditText positionEditText;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. Button addButton = findViewById(R.id.addButton);
  22. Button insertButton = findViewById(R.id.insertButton);
  23. ListView listView = findViewById(R.id.listView);
  24. noDataText = findViewById(R.id.noDataText);
  25. positionEditText = findViewById(R.id.positionEditText);
  26. dataList = new ArrayList<>();
  27. adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
  28. listView.setAdapter(adapter);
  29. addButton.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. addRecord();
  33. }
  34. });
  35. insertButton.setOnClickListener(new View.OnClickListener() {
  36. @Override
  37. public void onClick(View v) {
  38. insertRecord();
  39. }
  40. });
  41. }
  42. private void addRecord() {
  43. dataList.add("新建记录 " + (dataList.size() + 1));
  44. adapter.notifyDataSetChanged();
  45. toggleNoDataTextVisibility();
  46. }
  47. private void insertRecord() {
  48. try {
  49. int position = Integer.parseInt(positionEditText.getText().toString());
  50. if (position >= 0 && position <= dataList.size()) {
  51. dataList.add(position, "插入记录 " + (dataList.size() + 1));
  52. adapter.notifyDataSetChanged();
  53. toggleNoDataTextVisibility();
  54. } else {
  55. Toast.makeText(this, "位置无效", Toast.LENGTH_SHORT).show();
  56. }
  57. } catch (NumberFormatException e) {
  58. Toast.makeText(this, "请输入有效的位置", Toast.LENGTH_SHORT).show();
  59. }
  60. }
  61. private void toggleNoDataTextVisibility() {
  62. if (dataList.isEmpty()) {
  63. noDataText.setVisibility(View.VISIBLE);
  64. } else {
  65. noDataText.setVisibility(View.GONE);
  66. }
  67. }
  68. }

Android ListView 删除数据

ListView 删除数据

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

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

例子:

XML 布局文件(activity_main.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <Button
  8. android:id="@+id/addButton"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="添加记录"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerHorizontal="true"
  14. android:layout_marginTop="16dp"/>
  15. <Button
  16. android:id="@+id/deleteByObjectButton"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="对象删除"
  20. android:layout_below="@id/addButton"
  21. android:layout_alignParentStart="true"
  22. android:layout_marginStart="16dp"
  23. android:layout_marginTop="16dp"/>
  24. <Button
  25. android:id="@+id/deleteByPositionButton"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="游标删除"
  29. android:layout_below="@id/addButton"
  30. android:layout_alignParentEnd="true"
  31. android:layout_marginEnd="16dp"
  32. android:layout_marginTop="16dp"/>
  33. <ListView
  34. android:id="@+id/listView"
  35. android:layout_width="match_parent"
  36. android:layout_height="match_parent"
  37. android:layout_below="@id/deleteByObjectButton"
  38. android:dividerHeight="1dp"/>
  39. <TextView
  40. android:id="@+id/noDataText"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="无可用数据"
  44. android:layout_centerInParent="true"
  45. android:visibility="gone"/> <!-- 初始设置为隐藏 -->
  46. </RelativeLayout>

Java 代码(MainActivity.java):

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.Button;
  6. import android.widget.ListView;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import java.util.ArrayList;
  11. public class MainActivity extends AppCompatActivity {
  12. private ArrayList<String> dataList;
  13. private ArrayAdapter<String> adapter;
  14. private TextView noDataText;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. Button addButton = findViewById(R.id.addButton);
  20. Button deleteByObjectButton = findViewById(R.id.deleteByObjectButton);
  21. Button deleteByPositionButton = findViewById(R.id.deleteByPositionButton);
  22. ListView listView = findViewById(R.id.listView);
  23. noDataText = findViewById(R.id.noDataText);
  24. dataList = new ArrayList<>();
  25. adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
  26. listView.setAdapter(adapter);
  27. addButton.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. addRecord();
  31. }
  32. });
  33. deleteByObjectButton.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. deleteByObject();
  37. }
  38. });
  39. deleteByPositionButton.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. deleteByPosition();
  43. }
  44. });
  45. }
  46. private void addRecord() {
  47. dataList.add("新建记录 " + (dataList.size() + 1));
  48. adapter.notifyDataSetChanged();
  49. toggleNoDataTextVisibility();
  50. }
  51. private void deleteByObject() {
  52. if (!dataList.isEmpty()) {
  53. // 根据对象删除最后一项数据
  54. dataList.remove(dataList.size() - 1);
  55. adapter.notifyDataSetChanged();
  56. toggleNoDataTextVisibility();
  57. } else {
  58. Toast.makeText(this, "无数据可删除", Toast.LENGTH_SHORT).show();
  59. }
  60. }
  61. private void deleteByPosition() {
  62. if (!dataList.isEmpty()) {
  63. // 根据游标(position)删除第一项数据
  64. dataList.remove(0);
  65. adapter.notifyDataSetChanged();
  66. toggleNoDataTextVisibility();
  67. } else {
  68. Toast.makeText(this, "无数据可删除", Toast.LENGTH_SHORT).show();
  69. }
  70. }
  71. private void toggleNoDataTextVisibility() {
  72. if (dataList.isEmpty()) {
  73. noDataText.setVisibility(View.VISIBLE);
  74. } else {
  75. noDataText.setVisibility(View.GONE);
  76. }
  77. }
  78. }

ListView 清空数据

清空 ListView 数据有两种方式

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

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

XML 布局文件(activity_main.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <Button
  8. android:id="@+id/addButton"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="添加记录"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerHorizontal="true"
  14. android:layout_marginTop="16dp"/>
  15. <Button
  16. android:id="@+id/removeOneByOneButton"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="逐个删除"
  20. android:layout_below="@id/addButton"
  21. android:layout_alignParentStart="true"
  22. android:layout_marginStart="16dp"
  23. android:layout_marginTop="16dp"/>
  24. <Button
  25. android:id="@+id/clearAllButton"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="一次性删除"
  29. android:layout_below="@id/addButton"
  30. android:layout_alignParentEnd="true"
  31. android:layout_marginEnd="16dp"
  32. android:layout_marginTop="16dp"/>
  33. <ListView
  34. android:id="@+id/listView"
  35. android:layout_width="match_parent"
  36. android:layout_height="match_parent"
  37. android:layout_below="@id/removeOneByOneButton"
  38. android:dividerHeight="1dp"/>
  39. <TextView
  40. android:id="@+id/noDataText"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="无可用数据"
  44. android:layout_centerInParent="true"
  45. android:visibility="gone"/> <!-- 初始设置为隐藏 -->
  46. </RelativeLayout>

Java 代码(MainActivity.java):

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.Button;
  6. import android.widget.ListView;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import java.util.ArrayList;
  11. public class MainActivity extends AppCompatActivity {
  12. private ArrayList<String> dataList;
  13. private ArrayAdapter<String> adapter;
  14. private TextView noDataText;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. Button addButton = findViewById(R.id.addButton);
  20. Button removeOneByOneButton = findViewById(R.id.removeOneByOneButton);
  21. Button clearAllButton = findViewById(R.id.clearAllButton);
  22. ListView listView = findViewById(R.id.listView);
  23. noDataText = findViewById(R.id.noDataText);
  24. dataList = new ArrayList<>();
  25. adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
  26. listView.setAdapter(adapter);
  27. addButton.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. addRecord();
  31. }
  32. });
  33. removeOneByOneButton.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. removeOneByOne();
  37. }
  38. });
  39. clearAllButton.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. clearAll();
  43. }
  44. });
  45. }
  46. private void addRecord() {
  47. dataList.add("新建记录 " + (dataList.size() + 1));
  48. adapter.notifyDataSetChanged();
  49. toggleNoDataTextVisibility();
  50. }
  51. private void removeOneByOne() {
  52. if (!dataList.isEmpty()) {
  53. dataList.remove(dataList.size() - 1);
  54. adapter.notifyDataSetChanged();
  55. toggleNoDataTextVisibility();
  56. } else {
  57. Toast.makeText(this, "无数据可删除", Toast.LENGTH_SHORT).show();
  58. }
  59. }
  60. private void clearAll() {
  61. if (!dataList.isEmpty()) {
  62. dataList.clear();
  63. adapter.notifyDataSetChanged();
  64. toggleNoDataTextVisibility();
  65. } else {
  66. Toast.makeText(this, "无数据可清空", Toast.LENGTH_SHORT).show();
  67. }
  68. }
  69. private void toggleNoDataTextVisibility() {
  70. if (dataList.isEmpty()) {
  71. noDataText.setVisibility(View.VISIBLE);
  72. } else {
  73. noDataText.setVisibility(View.GONE);
  74. }
  75. }
  76. }

Android ListView 更改数据

ListView 数据更新

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

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

例子:

XML 布局文件(activity_main.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <Button
  8. android:id="@+id/addButton"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="添加记录"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerHorizontal="true"
  14. android:layout_marginTop="16dp"/>
  15. <Button
  16. android:id="@+id/updateByObjectButton"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="根据对象更新"
  20. android:layout_below="@id/addButton"
  21. android:layout_alignParentStart="true"
  22. android:layout_marginTop="16dp"/>
  23. <Button
  24. android:id="@+id/updateByPositionButton"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="根据游标更新"
  28. android:layout_below="@id/addButton"
  29. android:layout_alignParentEnd="true"
  30. android:layout_marginTop="16dp"/>
  31. <ListView
  32. android:id="@+id/listView"
  33. android:layout_width="match_parent"
  34. android:layout_height="match_parent"
  35. android:layout_below="@id/updateByObjectButton"
  36. android:dividerHeight="1dp"/>
  37. <TextView
  38. android:id="@+id/noDataText"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:text="无可用数据"
  42. android:layout_centerInParent="true"
  43. android:visibility="gone"/> <!-- 初始设置为隐藏 -->
  44. </RelativeLayout>

Java 代码(MainActivity.java):

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.Button;
  6. import android.widget.ListView;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import java.util.ArrayList;
  11. public class MainActivity extends AppCompatActivity {
  12. private ArrayList<String> dataList;
  13. private ArrayAdapter<String> adapter;
  14. private ListView listView;
  15. private TextView noDataText;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. Button addButton = findViewById(R.id.addButton);
  21. Button updateByObjectButton = findViewById(R.id.updateByObjectButton);
  22. Button updateByPositionButton = findViewById(R.id.updateByPositionButton);
  23. listView = findViewById(R.id.listView);
  24. noDataText = findViewById(R.id.noDataText);
  25. dataList = new ArrayList<>();
  26. adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
  27. listView.setAdapter(adapter);
  28. addButton.setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. addRecord();
  32. }
  33. });
  34. updateByObjectButton.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. updateByObject();
  38. }
  39. });
  40. updateByPositionButton.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. updateByPosition();
  44. }
  45. });
  46. }
  47. private void addRecord() {
  48. dataList.add("新记录 " + (dataList.size() + 1));
  49. adapter.notifyDataSetChanged();
  50. toggleNoDataTextVisibility();
  51. }
  52. private void updateByObject() {
  53. if (!dataList.isEmpty()) {
  54. // 根据对象更新第一个数据项
  55. dataList.set(0, "更新的记录 1");
  56. adapter.notifyDataSetChanged();
  57. Toast.makeText(this, "根据对象更新成功", Toast.LENGTH_SHORT).show();
  58. } else {
  59. Toast.makeText(this, "没有可更新的数据", Toast.LENGTH_SHORT).show();
  60. }
  61. }
  62. private void updateByPosition() {
  63. if (!dataList.isEmpty()) {
  64. // 根据游标更新第二个数据项
  65. int position = 1; // 第二个位置
  66. if (position >= 0 && position < dataList.size()) {
  67. dataList.set(position, "更新的记录 2");
  68. adapter.notifyDataSetChanged();
  69. Toast.makeText(this, "根据游标更新成功", Toast.LENGTH_SHORT).show();
  70. } else {
  71. Toast.makeText(this, "位置无效", Toast.LENGTH_SHORT).show();
  72. }
  73. } else {
  74. Toast.makeText(this, "没有可更新的数据", Toast.LENGTH_SHORT).show();
  75. }
  76. }
  77. private void toggleNoDataTextVisibility() {
  78. if (dataList.isEmpty()) {
  79. noDataText.setVisibility(View.VISIBLE);
  80. } else {
  81. noDataText.setVisibility(View.GONE);
  82. }
  83. }
  84. }

Android ListView 查询数据

ListView 数据查询

例子:

XML 布局文件(activity_main.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <Button
  8. android:id="@+id/addButton"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="添加记录"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerHorizontal="true"
  14. android:layout_marginTop="16dp"/>
  15. <EditText
  16. android:id="@+id/searchEditText"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:hint="请输入查询条件"
  20. android:layout_below="@id/addButton"
  21. android:layout_marginTop="16dp"
  22. android:layout_marginStart="16dp"
  23. android:layout_marginEnd="16dp"/>
  24. <Button
  25. android:id="@+id/searchButton"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="查询"
  29. android:layout_below="@id/searchEditText"
  30. android:layout_centerHorizontal="true"
  31. android:layout_marginTop="16dp"/>
  32. <ListView
  33. android:id="@+id/listView"
  34. android:layout_width="match_parent"
  35. android:layout_height="match_parent"
  36. android:layout_below="@id/searchButton"
  37. android:dividerHeight="1dp"/>
  38. <TextView
  39. android:id="@+id/noDataText"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="无可用数据"
  43. android:layout_centerInParent="true"
  44. android:visibility="gone"/> <!-- 初始设置为隐藏 -->
  45. </RelativeLayout>

Java 代码(MainActivity.java):

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.text.Editable;
  4. import android.text.TextWatcher;
  5. import android.view.View;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.ListView;
  10. import android.widget.TextView;
  11. import androidx.appcompat.app.AppCompatActivity;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. public class MainActivity extends AppCompatActivity {
  15. private ArrayList<String> dataList;
  16. private ArrayList<String> originalDataList; // 保存原始数据
  17. private ArrayAdapter<String> adapter;
  18. private ListView listView;
  19. private TextView noDataText;
  20. private EditText searchEditText;
  21. private Button searchButton;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. Button addButton = findViewById(R.id.addButton);
  27. searchEditText = findViewById(R.id.searchEditText);
  28. searchButton = findViewById(R.id.searchButton);
  29. listView = findViewById(R.id.listView);
  30. noDataText = findViewById(R.id.noDataText);
  31. dataList = new ArrayList<>();
  32. originalDataList = new ArrayList<>(); // 初始化原始数据列表
  33. adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
  34. listView.setAdapter(adapter);
  35. addButton.setOnClickListener(new View.OnClickListener() {
  36. @Override
  37. public void onClick(View v) {
  38. addRecord();
  39. }
  40. });
  41. searchButton.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View v) {
  44. searchRecords();
  45. }
  46. });
  47. searchEditText.addTextChangedListener(textWatcher);
  48. }
  49. private final TextWatcher textWatcher = new TextWatcher() {
  50. @Override
  51. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  52. @Override
  53. public void onTextChanged(CharSequence s, int start, int before, int count) {}
  54. @Override
  55. public void afterTextChanged(Editable s) {
  56. // 每次文本发生变化时不自动查询
  57. }
  58. };
  59. private void addRecord() {
  60. originalDataList.add("新记录 " + (originalDataList.size() + 1)); // 添加记录到原始数据列表
  61. if (searchEditText.getText().toString().isEmpty()) {
  62. // 如果查询条件为空,则更新列表
  63. updateList(originalDataList);
  64. }
  65. searchRecords(); // 执行查询操作
  66. }
  67. private void searchRecords() {
  68. String query = searchEditText.getText().toString().trim().toLowerCase();
  69. List<String> filteredList = new ArrayList<>();
  70. for (String record : originalDataList) {
  71. if (record.toLowerCase().contains(query)) {
  72. filteredList.add(record);
  73. }
  74. }
  75. updateList(filteredList);
  76. }
  77. private void updateList(List<String> records) {
  78. dataList.clear();
  79. dataList.addAll(records);
  80. adapter.notifyDataSetChanged();
  81. toggleNoDataTextVisibility();
  82. }
  83. private void toggleNoDataTextVisibility() {
  84. noDataText.setVisibility(dataList.isEmpty() ? View.VISIBLE : View.GONE);
  85. }
  86. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/130653
推荐阅读
相关标签
  

闽ICP备14008679号