ListView中如何添加删除按钮

我们在安卓开发中有时候会需要这样一个功能,就是在一个listView上显示一个列表,列表上需要有一个删除按钮,点击相应的删除按钮则删除该条信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Test_4_Activity extends Activity {
private DeletableAdapter adapter;
private ArrayList<String> text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test4);
ListView list_view = (ListView) findViewById(R.id.list_view);
text = new ArrayList<String>();
text.add("Android_001");
text.add("Android_002");
text.add("Android_003");
text.add("Android_004");
// 初始化数据结束
adapter = new DeletableAdapter(this, text);
list_view.setAdapter(adapter);
// list_view.setSelector(R.drawable.list_select_color);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.add("10000");
adapter.notifyDataSetChanged();
}
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class DeletableAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> text;
public DeletableAdapter(Context context, ArrayList<String> text) {
this.context = context;
this.text = text;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return text.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return text.get(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
final int index = position;
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_simple_list_item_2, null);
}
final TextView textView = (TextView) view
.findViewById(R.id.simple_item_1);
textView.setText(text.get(position));
final ImageView imageView = (ImageView) view
.findViewById(R.id.simple_item_2);
imageView.setBackgroundResource(android.R.drawable.ic_delete);
imageView.setTag(position);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.remove(index);
notifyDataSetChanged();
Toast.makeText(context, textView.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/simple_item_2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:focusable="false" />
<TextView
android:id="@+id/simple_item_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" />
</RelativeLayout>