Listview动态添加/删除数据

这里是针对list的数据添加/删除方式,其他的数据方式相同。


布局很简单:

<?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="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增加" />

        <Button
            android:id="@+id/btn_remove"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减少" />
    </LinearLayout>

    <ListView
        android:id="@+id/lv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

每行的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/custom_loc" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:text="TextView" />

</RelativeLayout>

activity:

public class AdapterAddData extends Activity implements OnClickListener {

	private Button addBtn;
	private Button removeBtn;
	private ListView show;
	private List list;
	private MyShowAdapter myadapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acy_adddate);

		init();
		list = new ArrayList();
		for (int i = 0; i < 3; i++) {
			list.add("123");
		}
		myadapter = new MyShowAdapter(this, list);
		show.setAdapter(myadapter);
	}

	// 初始化
	public void init() {
		// 组件
		addBtn = (Button) findViewById(R.id.btn_add);
		removeBtn = (Button) findViewById(R.id.btn_remove);
		show = (ListView) findViewById(R.id.lv_show);
		// 添加监听器
		addBtn.setOnClickListener(this);
		removeBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == addBtn) {
			list.add("123");
			myadapter.notifyDataSetChanged();
		} else if (v == removeBtn) {
			list.remove(0);
			myadapter.notifyDataSetChanged();
		}
	}

}

适配器:

public class MyShowAdapter extends BaseAdapter {

	private Context context;
	private List list;
	public MyShowAdapter(Context context, List list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView==null){
			convertView=LayoutInflater.from(context).inflate(R.layout.adater_line, null);
		}
		return convertView;
	}

}

效果:

这个运行的时候,开始加入了3条数据。

添加一个数据:

删除2条数据:


动态改变行数:

public class TestMoreList extends Activity implements OnClickListener {
	private int i = 10;
	private MyAdpter adp = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acy_morelist);

		ListView lv = (ListView) findViewById(R.id.lv_txt);
		adp = new MyAdpter(i);
		lv.setAdapter(adp);

		findViewById(R.id.btn_more).setOnClickListener(this);
	}

	class MyAdpter extends BaseAdapter {
		private int i = 0;

		public MyAdpter(int i) {
			this.i = i;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return i;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView txt = null;
			if (null == convertView) {
				convertView = getLayoutInflater().inflate(R.layout.line_txt,
						null);
			}
			txt = (TextView) convertView.findViewById(R.id.tv_txt);
			txt.setText(new Date().toString());
			return convertView;
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_more:
			adp.i += 10;
			adp.notifyDataSetChanged();
			break;

		default:
			break;
		}
	}
}



注意:

1.数据的动态改变主要是这句:

myadapter.notifyDataSetChanged();

如果修改了,没通知适配器数据更改,那就不能看到更改的效果,虽然list改变了。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值