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
arr = new ArrayList<String>();
058
for(int i=0;i<3;i++){ //listview初始化3個子項
059
arr.add("");
060
}
061
}
062
@Override
063
public int getCount() {
064
// TODO Auto-generated method stub
065
return arr.size();
066
}
067
@Override
068
public Object getItem(int arg0) {
069
// TODO Auto-generated method stub
070
return arg0;
071
}
072
@Override
073
public long getItemId(int arg0) {
074
// TODO Auto-generated method stub
075
return arg0;
076
}
077
@Override
078
public View getView(final int position, View view, ViewGroup arg2) {
079
// TODO Auto-generated method stub
080
if(view == null){
081
view = inflater.inflate(R.layout.list_item, null);
082
}
083
final EditText edit = (EditText) view.findViewById(R.id.edit);
084
edit.setText(arr.get(position)); //在重構adapter的時候不至於數據錯亂
085
Button del = (Button) view.findViewById(R.id.del);
086
edit.setOnFocusChangeListener(new OnFocusChangeListener() {
087
@Override
088
public void onFocusChange(View v, boolean hasFocus) {
089
// TODO Auto-generated method stub
090
if(arr.size()>0){
091
arr.set(position, edit.getText().toString());
092
}
093
}
094
});
095
del.setOnClickListener(new OnClickListener() {
096
@Override
097
public void onClick(View arg0) {
098
// TODO Auto-generated method stub
099
//從集合中刪除所刪除項的EditText的內容
100
arr.remove(position);
101www.2cto.com
adapter.notifyDataSetChanged();
102
}
103
});
104
return view;
105
}
106
}
107
}