1、简介

  基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作:

    public boolean add(E e) {//添加数据
throw new RuntimeException("Stub!");
}
public void add(int index, E element) {//通过索引添加数据
throw new RuntimeException("Stub!");
}
public boolean remove(Object o) {//移除数据
throw new RuntimeException("Stub!");
}
public E remove(int index) {//通过索引移除数据
throw new RuntimeException("Stub!");
}
public void clear() {//清除所有数据
throw new RuntimeException("Stub!");
}
public void notifyDataSetChanged() {//刷新ListView
throw new RuntimeException("Stub!");
}

2、简单使用

  1)添加按钮布局xml文件:

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加数据"
android:id="@+id/addbtn"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="指定位置添加数据"
android:id="@+id/addbtn1"/> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除数据"
android:id="@+id/Remove"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="指定位置删除数据"
android:id="@+id/Remove1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除所有数据"
android:id="@+id/clearAll"/> </LinearLayout>

  2)在自定义的Adapter.java文件中添加、移除代码:

    public void add(Custom custom){
if (aData == null){
aData = new LinkedList<>();
}
aData.add(custom);
notifyDataSetChanged();
}
public void add(int position,Custom custom){
if (aData == null){
aData = new LinkedList<>();
}
aData.add(position,custom);
notifyDataSetChanged();
}
public void remove(Custom custom){
if (aData !=null){
aData.remove(custom);
}
notifyDataSetChanged();
}
public void remove(int postition){
if (aData !=null){
aData.remove(postition);
}
notifyDataSetChanged();
}
public void clear() {
if(aData != null) {
aData.clear();
}
notifyDataSetChanged();
}

  3)Java文件的代码:

public class LoginActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,OnClickListener{

    private String[] names = new String[]{"猪八戒","孙悟空","唐僧"};
private String[] says = new String[]{"饿了","吃俺老孙一棒","紧箍咒"};
private int[] images = new int[]{R.drawable.icon,R.drawable.icon,R.drawable.icon};
private Button btnAdd,addBtn1,removeBtn,removeBtn1,clearBtn;
private CustomAdapter customAdapter = null;
private Custom custom_1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnAdd = (Button)findViewById(R.id.addbtn);
btnAdd.setOnClickListener(this);
addBtn1 = (Button)findViewById(R.id.addbtn1);
addBtn1.setOnClickListener(this);
removeBtn = (Button)findViewById(R.id.Remove);
removeBtn.setOnClickListener(this);
removeBtn1 = (Button)findViewById(R.id.Remove1);
removeBtn1.setOnClickListener(this);
clearBtn = (Button)findViewById(R.id.clearAll);
clearBtn.setOnClickListener(this);
ListView list_test = (ListView) findViewById(R.id.listview);
final LayoutInflater inflater = LayoutInflater.from(this);
View headView = inflater.inflate(R.layout.list_header, null, false);
View footView = inflater.inflate(R.layout.list_header, null, false); List<Custom> aData = new LinkedList<Custom>();
for (int i=0;i<names.length;i++){
aData.add(new Custom(names[i],says[i],images[i]));
}
//添加表头和表尾需要写在setAdapter方法调用之前!!!
list_test.addHeaderView(headView);
list_test.addFooterView(footView); customAdapter = new CustomAdapter((LinkedList<Custom>)aData,LoginActivity.this);
list_test.setAdapter(customAdapter);
list_test.setOnItemClickListener(this);
} @Override
public void onClick(View view){
switch (view.getId()){
case R.id.addbtn:
custom_1 = new Custom("沙和尚","呵呵呵",R.drawable.icon);
customAdapter.add(custom_1);
break;
case R.id.addbtn1:
customAdapter.add(2,new Custom("指定","假的",R.drawable.icon));
break;
case R.id.Remove:
customAdapter.remove(custom_1);
break;
case R.id.Remove1:
//判断是否越界 省略
customAdapter.remove(2);
break;
case R.id.clearAll:
customAdapter.clear();
break; }
}
}

Android基础控件ListView基础操作的更多相关文章

  1. Android列表控件ListView详解

    ListView绝对可以称得上是Android中最常用的控件之一,几乎所有应用程序都会用到它. 由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候 ...

  2. Android:控件ListView列表项与适配器结合使用

    Listview是用来展示一些重复性的数据用的,比如一些列表集合数据展示到手机,需要适配器作为载体获取数据,最后将数据填充到布局. ListView里面的每个子项Item可以使一个字符串,也可以是一个 ...

  3. Android基础控件ListView和自定义BaseAdapter适配器

    1.简介 ListView用于列表显示,相当于OC中的TableView,和适配器一块使用,相关属性: footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条, ...

  4. Android重要控件———ListView

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  5. Android学习——控件ListView的使用

    一.ListView的简单用法 首先新建一个ListViewTest项目,并让Android Studio自动创建好活动.然后修改activity_main.xml中的代码,如下: <?xml ...

  6. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  7. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  8. React Native环境搭建以及几个基础控件的使用

    之前写了几篇博客,但是没有从最基础的开始写,现在想了想感觉不太合适,所以现在把基础的一些东西给补上,也算是我从零开始学习RN的经验吧! 一.环境搭建 首先声明一下,本人现在用的编辑器是SublimeT ...

  9. Android基本控件之listView(三)<用ListView实现分页加载>

    我们之前讨论了ListView的基本使用方法和ListView的优化 今天我们再来讨论一个关于ListView的一个新的东西~就是分页加载.那么什么是分页加载呢?简单点说,就是"下拉刷新&q ...

随机推荐

  1. 记录一次MySQL数据库CPU负载异常高的问题

    1.起因 某日下午18:40开始,接收到滕讯云短信报警,显示数据库CPU使用率已超过100%,同时慢查询日志的条数有1500条左右. 正常情况下:CPU使用率为30%-40%之间,慢查询日志条数为0. ...

  2. 关于soapui如何做安全测试

    1.首先安装soapui5.1.2 第一步:运行SoapUI-Pro-x32-5.1.2_576024.exe文件,按照步骤安装成功: 第二步:拷贝Protection-4.6.jar到soapui安 ...

  3. laravel 优化小记

    laravel 优化 7 Performance Optimization Tips for the Laravel Developer 运行 php artisan optimize php art ...

  4. canvas基础用法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. hdu6089 Rikka with Terrorist

    题意:n*m的平面内有K个不安全点,Q个询问位置在(x,y)的人能走到多少个点?走到:(x,y)和(x',y')之间的矩形中不包含不安全点. 标程: #include<bits/stdc++.h ...

  6. thinkphp 模型定义

    模型定义 模型类并非必须定义,只有当存在独立的业务逻辑或者属性的时候才需要定义. 模型类通常需要继承系统的\Think\Model类或其子类,下面是一个Home\Model\UserModel类的定义 ...

  7. [SNOI 2017] 炸弹

    题目描述: 给定炸弹和爆炸范围,求对于每个炸弹连锁爆炸的炸弹总和对\(1e9+7\)取膜 思路: 为啥都是线段树+TS+tarjan呢? 实在是搞不懂~~ 线性\(O(n)\)递推即可. #inclu ...

  8. 软件-开发工具:Gradle

    ylbtech-软件-开发工具:Gradle Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具.它使用一种基于Groovy的特定领域语言(DSL)来声明 ...

  9. div中包着文字,div出现隐藏的时候,文字总是在div外面。

    背景: 给博客加一个侧边栏,点击出现隐藏,每次点击出现或者隐藏,文字总是很突兀的就出来了. 解决: overflow:hidden

  10. JS 变量的数据类型转换

    变量的类型转换,一般情况是JS自动转换的,但也有些时候需要手动转换. 1.其它类型转成布尔型 函数和方法的区别 Boolean(a)  //函数是可以独立使用的 document.write(a)   ...