目录
Android ListView 添加插入数据
添加记录
在指定位置插入数据
Android ListView 删除数据
ListView 删除数据
ListView 清空数据
Android ListView 更改数据
ListView 数据更新
Android ListView 查询数据
ListView 数据查询
Android ListView 添加插入数据
添加记录
我们在顶部添加一个按钮,每次点击添加一条记录,并且数据为空时提示用户没数据
XML 布局文件(activity_main.xml):文章来源:https://www.toymoban.com/news/detail-836261.html
<?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);
}
}
}
Android ListView 删除数据
ListView 删除数据
从 ListView 中删除某项数据有两种方法
- 根据对象删除
- 根据游标(position) 删除
例子:
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 清空数据
清空 ListView 数据有两种方式
- 调用 remove() 方法一项一项的删除
- 调用 clear() 方法一次性删除
下面我们就写二个按钮 来演示下这两种方法
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);
}
}
}
Android ListView 更改数据
ListView 数据更新
更新 ListView 中的数据有两种办法
- 根据对象更新
- 根据游标(pos) 更新
例子:
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);
}
}
}
Android ListView 查询数据
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"/>
<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):文章来源地址https://www.toymoban.com/news/detail-836261.html
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);
}
}
到了这里,关于学习Android的第十七天的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!