main.xml 文件:
复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
androID:orIEntation="horizontal"
>
<linearLayout
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
androID:orIEntation="vertical"
>
<ListVIEw
androID:ID="@+ID/ListvIEw"
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
/>
<button
androID:ID="@+ID/add"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:text="添加"
/>
</linearLayout>
</linearLayout>
ListvIEw_item.xml文件:
复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
androID:orIEntation="horizontal"
androID:background="#000000"
androID:padding="20dp"
>
<EditText
androID:ID="@+ID/edit"
androID:layout_wIDth="200dp"
androID:layout_height="wrap_content"
/>
<button
androID:ID="@+ID/del"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:text="删除"
/>
</linearLayout>
MainActivity .java
复制代码 代码如下:
package com.yyy.testandroID;
import java.util.ArrayList;
import androID.app.Activity;
import androID.content.Context;
import androID.os.Bundle;
import androID.vIEw.LayoutInflater;
import androID.vIEw.VIEw;
import androID.vIEw.VIEw.OnClickListener;
import androID.vIEw.VIEw.OnFocuschangelistener;
import androID.vIEw.VIEwGroup;
import androID.Widget.BaseAdapter;
import androID.Widget.button;
import androID.Widget.EditText;
import androID.Widget.ListVIEw;
import androID.Widget.TextVIEw;
public class TestAndroIDActivity extends Activity {
/** Called when the activity is first created. */
private button button,add;
private TextVIEw text;
private ListVIEw ListvIEw;
public MyAdapter adapter;
@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
ListvIEw = (ListVIEw) findVIEwByID(R.ID.ListvIEw);
add = (button) findVIEwByID(R.ID.add);
adapter = new MyAdapter(this);
ListvIEw.setAdapter(adapter);
add.setonClickListener(new OnClickListener() {
@OverrIDe
public voID onClick(VIEw arg0) {
// Todo auto-generated method stub
adapter.arr.add("");
adapter.notifyDataSetChanged();
}
});
}
private class MyAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
public ArrayList<String> arr;
public MyAdapter(Context context) {
super();
this.context = context;
inflater = LayoutInflater.from(context);
arr = new ArrayList<String>();
for(int i=0;i<3;i++){ //ListvIEw初始化3个子项
arr.add("");
}
}
@OverrIDe
public int getCount() {
// Todo auto-generated method stub
return arr.size();
}
@OverrIDe
public Object getItem(int arg0) {
// Todo auto-generated method stub
return arg0;
}
@OverrIDe
public long getItemID(int arg0) {
// Todo auto-generated method stub
return arg0;
}
@OverrIDe
public VIEw getVIEw(final int position,VIEw vIEw,VIEwGroup arg2) {
// Todo auto-generated method stub
if(vIEw == null){
vIEw = inflater.inflate(R.layout.List_item,null);
}
final EditText edit = (EditText) vIEw.findVIEwByID(R.ID.edit);
edit.setText(arr.get(position)); //在重构adapter的时候不至于数据错乱
button del = (button) vIEw.findVIEwByID(R.ID.del);
edit.setonFocuschangelistener(new OnFocuschangelistener() {
@OverrIDe
public voID onFocusChange(VIEw v,boolean hasFocus) {
// Todo auto-generated method stub
if(arr.size()>0){
arr.set(position,edit.getText().toString());
}
}
});
del.setonClickListener(new OnClickListener() {
@OverrIDe
public voID onClick(VIEw arg0) {
// Todo auto-generated method stub
//从集合中删除所删除项的EditText的内容
arr.remove(position);
adapter.notifyDataSetChanged();
}
});
return vIEw;
}
}
}
总结以上是内存溢出为你收集整理的android ListView内数据的动态添加与删除实例代码全部内容,希望文章能够帮你解决android ListView内数据的动态添加与删除实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)