Android基础控件ListView基础操作
2024-10-19 09:31:56
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; }
}
}
最新文章
- jquery仿搜狐投票动画代码
- xampp3.2下mysql中文乱码终极解决方案
- 关于matlab中特殊字符, 上标和下标
- javascript设计模式--备忘录模式(Memento)
- java工具类系列 (四.SerializationUtils)
- POJ1061 青蛙的约会 扩展欧几里得
- C++ 运行时类型识别 知道实例父类类型,显示出子类类型
- pcap的pcap_dump()保存的文件格式
- js中如何把字符串转化为对象
- MySQL数据库的双向加密方式
- HDU 1194 - Beat the Spread!
- saiku中过滤窗口优化及隐藏异常报错
- cocos2dx--两个场景切换各函数调用顺序
- DecimalFormat
- crontab演出newLISP脚本设置环境变量
- windows下运行hadoop2.7.2
- Unity 的几种打包姿势(android)
- Python/零起点(一、数字及元组)
- 聊一聊快速排序(Js)
- JavaScript基础整理