android开发--详解ListView,动态添加,删除Adapter中的数据项
ListView是Android开发中最常用到的控件之一,所以学习ListView显得非常重要,在本文中,作者继承了BaseAdapter实现了自己的Adapter,在其中实现相应的方法,包括添加,删除,清空list。
除此之外,作者实现了listview的两个方法,即当用户选择某一项和用户点击某一项发生的相关事件,下图即为此程序的截图:

一下是具体的实现源代码:
public class ListView_Activity extends Activity {private Button button1;private Button button2;private Button button3;private ListView listView;private String[] data={"二次曝光","十二生肖","血滴子","大上海","人在囧途","泰囧","新少林寺","大魔术师","哆啦a梦"};MyAdapter myAdapter =new MyAdapter(ListView_Activity.this);private static int counter=0;private TextView textView;private TextView textView2;private TextView textView3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_list_view_);listView=(ListView)findViewById(R.id.listview);listView.setAdapter(myAdapter);button1=(Button)findViewById(R.id.buttonadd);button2=(Button)findViewById(R.id.buttondelete);button3=(Button)findViewById(R.id.buttonclear);button1.setOnClickListener(new MyButton());button2.setOnClickListener(new MyButton());button3.setOnClickListener(new MyButton());textView=(TextView)findViewById(R.id.textview);textView2=(TextView)findViewById(R.id.textviewselect);textView3=(TextView)findViewById(R.id.textviewclick);listView.setOnItemSelectedListener(new ListView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stub//由于是键盘向下键选择某一个View,所以焦点聚焦到那一个View,所以采用此方法返回结果textView2.setText(arg0.getSelectedItem().toString());}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stub//由于这是用户的点击事件,所以由此可以确定点击的那个位置(arg2),所以采用此方法返回结果textView3.setText(data[arg2]);}});}class MyButton implements OnClickListener{@Overridepublic void onClick(View view) {// TODO Auto-generated method stubswitch (view.getId()) {case R.id.buttonadd:if(counter<data.length){myAdapter.addText(data[counter]);}textView.setText(counter+"");break;case R.id.buttondelete:myAdapter.remove(--counter);textView.setText(counter+"");break;case R.id.buttonclear:myAdapter.removeAll();textView.setText(counter+"");break;default:break;}}}class MyAdapter extends BaseAdapter{private Context context;private List<String> textList=new ArrayList<String>();public MyAdapter(Context context) {this.context=context;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn textList.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn textList.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stubString inflater=Context.LAYOUT_INFLATER_SERVICE;LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(inflater);LinearLayout linearLayout=null;linearLayout=(LinearLayout)layoutInflater.inflate(R.layout.text2,null);TextView textView=(TextView)linearLayout.findViewById(R.id.text);textView.setText(String.valueOf(textList.get(arg0)));return linearLayout;}public void addText(String text){if (counter<=data.length-1) {textList.add(text);counter++;}else {Toast.makeText(ListView_Activity.this, "列表中已经显示了所有的项目!!!", Toast.LENGTH_LONG).show();}notifyDataSetChanged();}public void remove(int index) {if(index<0){Toast.makeText(ListView_Activity.this, "列表中并没有结果!!!", Toast.LENGTH_LONG).show();counter=0;return;}else {textList.remove(index);}notifyDataSetChanged();}public void removeAll(){textList.clear();counter=0;notifyDataSetChanged();}}}