main.xml 文件:
01
<?xml version="1.0" encoding="utf-8"?>
02
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03
android:layout_width="fill_parent"
04
android:layout_height="fill_parent"
05
android:orientation="horizontal"
06
>
07
<LinearLayout
08
android:layout_width="fill_parent"
09
android:layout_height="fill_parent"
10
android:orientation="vertical"
11
>
12
<ListView
13
android:id="@+id/listview"
14
android:layout_width="fill_parent"
15
android:layout_height="wrap_content"
16
/>
17
<Button
18
android:id="@+id/add"
19
android:layout_width="wrap_content"
20
android:layout_height="wrap_content"
21
android:text="添加"
22
/>
23
</LinearLayout>
24
</LinearLayout> listview_item.xml文件:
01
<?xml version="1.0" encoding="utf-8"?>
02
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03
android:layout_width="fill_parent"
04
android:layout_height="wrap_content"
05
android:orientation="horizontal"
06
android:background="#000000"
07
android:padding="20dp"
08
>
09
10
<EditText
11
android:id="@+id/edit"
12
android:layout_width="200dp"
13
android:layout_height="wrap_content"
14
/>
15
<Button
16
android:id="@+id/del"
17
android:layout_width="wrap_content"
18
android:layout_height="wrap_content"
19
android:text="删除"
20
/>
21
22
</LinearLayout> MainActivity .java
001
package com.yyy.testandroid;
002
003
004
005
import java.util.ArrayList;
006
007
import android.app.Activity;
008
import android.content.Context;
009
import android.os.Bundle;
010
import android.view.LayoutInflater;
011
import android.view.View;
012
import android.view.View.OnClickListener;
013
import android.view.View.OnFocusChangeListener;
014
import android.view.ViewGroup;
015
import android.widget.BaseAdapter;
016
import android.widget.Button;
017
import android.widget.EditText;
018
import android.widget.ListView;
019
import android.widget.TextView;
020
021
public class TestAndroidActivity extends Activity {
022
/** Called when the activity is first created. */
023
024
private Button button,add;
025
private TextView text;
026
private ListView listview;
027
public MyAdapter adapter;
028
@Override
029
public void onCreate(Bundle savedInstanceState) {
030
super.onCreate(savedInstanceState);
031
setContentView(R.layout.main);
032
listview = (ListView) findViewById(R.id.listview);
033
add = (Button) findViewById(R.id.add);
034
adapter = new MyAdapter(this);
035
listview.setAdapter(adapter);
036
037
add.setOnClickListener(new OnClickListener() {
038
@Override
039
public void onClick(View arg0) {
040
// TODO Auto-generated method stub
041
adapter.arr.add("");
042
adapter.notifyDataSetChanged();
043
}
044
});
045
}
046
047
048
private class MyAdapter extends BaseAdapter {
049
050
private Context context;
051
private LayoutInflater inflater;
052
public ArrayList<String> arr;
053
public MyAdapter(Context context) {
054
super();
055
this.context = context;
056
inflater = LayoutInflater.from(context);
057