本帖最后由 liu 于 2018-12-14 15:08 编辑
ListView动态添加删除数据
实现方法就是通过控制数据的增加或者删除,然后刷新适配器即可
举例
activity_main.xml:
[XML] 纯文本查看 复制代码 <?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="添加数据" />
</LinearLayout>
item_listview.xml:
[XML] 纯文本查看 复制代码 <?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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="@android:color/black" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginRight="15dp"
android:background="@color/colorAccent"
android:text="删除"
android:textColor="@android:color/white" />
</LinearLayout>
</LinearLayout>
MyAdapter:[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2018/12/13.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List<String> list;
public MyAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_listview, null);
holder.tv = convertView.findViewById(R.id.tv);
holder.btn = convertView.findViewById(R.id.btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(list.get(position));
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//从数据源中把所点击的数据移除
list.remove(list.get(position));
//刷新适配器
MyAdapter.this.notifyDataSetChanged();
}
});
return convertView;
}
//用于缓存控件
public class ViewHolder {
private TextView tv;
private Button btn;
}
}
MainActivity:[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.v7.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 Button mButton;
private ListView mListView;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
list = new ArrayList<>();
for (int i = 1; i < 11; i++) {
list.add("第" + i + "条数据");
}
}
private void initView() {
mButton = (Button) findViewById(R.id.btn);
mListView = (ListView) findViewById(R.id.lv);
final MyAdapter adapter = new MyAdapter(this, list);
mListView.setAdapter(adapter);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//向数据源中添加数据 第一个参数代表添加的位置 想要把数据添加到最后一个位置时可以写成 list.add("数据内容");
list.add(0, "新添加的数据");
//刷新适配器
adapter.notifyDataSetChanged();
}
});
}
}
效果图:
|