Android基础控件ListView基础操作
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基础操作的更多相关文章
- Android列表控件ListView详解
ListView绝对可以称得上是Android中最常用的控件之一,几乎所有应用程序都会用到它. 由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候 ...
- Android:控件ListView列表项与适配器结合使用
Listview是用来展示一些重复性的数据用的,比如一些列表集合数据展示到手机,需要适配器作为载体获取数据,最后将数据填充到布局. ListView里面的每个子项Item可以使一个字符串,也可以是一个 ...
- Android基础控件ListView和自定义BaseAdapter适配器
1.简介 ListView用于列表显示,相当于OC中的TableView,和适配器一块使用,相关属性: footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条, ...
- Android重要控件———ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- Android学习——控件ListView的使用
一.ListView的简单用法 首先新建一个ListViewTest项目,并让Android Studio自动创建好活动.然后修改activity_main.xml中的代码,如下: <?xml ...
- android 基础控件(EditView、SeekBar等)的属性及使用方法
android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...
- 矩阵, 矩阵 , Android基础控件之ImageView
天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...
- React Native环境搭建以及几个基础控件的使用
之前写了几篇博客,但是没有从最基础的开始写,现在想了想感觉不太合适,所以现在把基础的一些东西给补上,也算是我从零开始学习RN的经验吧! 一.环境搭建 首先声明一下,本人现在用的编辑器是SublimeT ...
- Android基本控件之listView(三)<用ListView实现分页加载>
我们之前讨论了ListView的基本使用方法和ListView的优化 今天我们再来讨论一个关于ListView的一个新的东西~就是分页加载.那么什么是分页加载呢?简单点说,就是"下拉刷新&q ...
随机推荐
- 2018自己的JavaScript开发指南
这是一个备忘清单,可以让你在不用做太多选择的情况下快速学习.我会列出一些工具来满足大部分场景下的前端开发.当你看完这篇文章,你会有足够的自信来调整你的技术栈. ☉概要 我会将地图划分为你需要解决的问题 ...
- Worker Thread等到工作来,来了就工作
Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...
- vba增删改查数据库2
sub test()Set cnn = CreateObject("ADODB.Connection") Set rs = CreateObject("Adodb.Rec ...
- LightOJ 1151 Snakes and Ladders 期望dp+高斯消元
题目传送门 题目大意:10*10的地图,不过可以直接看成1*100的,从1出发,要到达100,每次走的步数用一个大小为6的骰子决定.地图上有很多个通道 A可以直接到B,不过A和B大小不确定 而且 ...
- .net 裁剪图片
private void GetImg() { ) { return; } ]; string[] imgsize = Request["imgsize"].Split('& ...
- next() 与 nextLine() 区别
next() 与 nextLine() 区别 next(): 1.一定要读取到有效字符后才可以结束输入. 2.对输入有效字符之前遇到的空白,next() 方法会自动将其去掉. 3.只有输入有效字符后才 ...
- [转]Redis实现缓存,你应该懂的哪些思路!
场景一:类似于微博,实现关注和被关注功能. 思路: 对每个用户使用两个集合类型键,用来存储关注别人的用户和被该用户关注的用户.当用户A关注用户B的时候,执行两步操作: sadd user:A B sa ...
- leetcode-47-全排列②
题目描述: 方法一:回溯 class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: #nums = ...
- VPGAME的Kubernetes迁移实践
VPGAME 是集赛事运营.媒体资讯.大数据分析.玩家社群.游戏周边等为一体的综合电竞服务平台.总部位于中国杭州,在上海和美国西雅图分别设立了电竞大数据研发中心和 AI 研发中心.本文将讲述 VPGA ...
- Entity Framework Code First使用者的福音 --- EF Power Tool使用记之二(问题探究)
转:http://www.cnblogs.com/LingzhiSun/archive/2011/06/13/EFPowerTool_2.html 上次为大家介绍EF Power Tool之后,不 ...