ListView里面要填充的数据多少是静态的,但在实际开发中,这些数据往往都是动态变化的,比如数据内容发生改变、增加几行、或者删除几行,这就涉及到ListView数据的更新问题。接下来通过一个简单的示例程序来学习ListView的动态添加数据。
1、创建listview的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<Button
android:id="@+id/btn_add_dada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="添加数据"/>
</LinearLayout>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2、接着在res/layout/目录下新建一个listview的子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_num"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_RFID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_isSit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_isFefund"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"/>
</LinearLayout>
3、创建数据实体类,主要用于显示在列表数据的实体
package com.example.listviewtest;
public class Info {
private String num;
private String RFID;
private String isSit; //是否入座
private String isFefund; //是否退票
public Info(String num, String RFID, String isSit, String isFefund) {
this.num = num;
this.RFID = RFID;
this.isSit = isSit;
this.isFefund = isFefund;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getRFID() {
return RFID;
}
public void setRFID(String RFID) {
this.RFID = RFID;
}
public String getIsSit() {
return isSit;
}
public void setIsSit(String isSit) {
this.isSit = isSit;
}
public String getIsFefund() {
return isFefund;
}
public void setIsFefund(String isFefund) {
this.isFefund = isFefund;
}
}
4、最重要的一步,创建适配器,在适配器中添加一个add方法,通知适配器中数据的更新
package com.example.listviewtest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class InfoAdapter extends ArrayAdapter<Info> {
private List<Info> listInfo;
//用于将上下文、listview 子项布局的 id 和数据都传递过来
public InfoAdapter(@NonNull Context context, int resource, @NonNull List<Info> list) {
super(context, resource, list);
listInfo=list;
}
//增加一个方法添加动态数据
public void add(Info info){
listInfo.add(info);
notifyDataSetChanged();
}
//每个子项被滚动到屏幕内的时候会被调用
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Info info = getItem(position);//得到当前项的 Info 实例
//为每一个子项加载设定的布局
View view;
if(convertView==null)
{
view = LayoutInflater.from(getContext()).inflate(R.layout.info_item,parent,
false);
}else {
view = convertView;
}
//分别获取子布局textview 的实例
TextView tv_num=view.findViewById(R.id.tv_num);
TextView tv_RFID=view.findViewById(R.id.tv_RFID);
TextView tv_isSit=view.findViewById(R.id.tv_isSit);
TextView tv_isFefund=view.findViewById(R.id.tv_isFefund);
// 设置要显示内容
tv_num.setText(info.getNum());
tv_RFID.setText(info.getRFID());
tv_isSit.setText(info.getIsSit());
tv_isFefund.setText(info.getIsFefund());
return view;
}
}
5、最好编写主程序
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Info> infoList=new ArrayList<>();
InfoAdapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.list_view);
Info info=new Info("1","adb","a","e");
//infoList.add(info);
adapter=new InfoAdapter(MainActivity.this,R.layout.info_item,infoList);
listView.setAdapter(adapter);
Button button = findViewById(R.id.btn_add_dada);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Info info=new Info("1","rfid","f","f");
adapter.add(info);
}
});
}
}
版权声明:本文为lcp0633原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。