android开发--详解ListView,动态添加,删除Adapter中的数据项

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

      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=0private TextView textView; private TextView textView2; private TextView textView3; @Override protected 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() {   @Override   public void onItemSelected(AdapterView arg0, View arg1,     int arg2, long arg3) {    // TODO Auto-generated method stub    //由于是键盘向下键选择某一个View,所以焦点聚焦到那一个View,所以采用此方法返回结果    textView2.setText(arg0.getSelectedItem().toString());   }   @Override   public void onNothingSelected(AdapterView arg0) {    // TODO Auto-generated method stub       }  });  listView.setOnItemClickListener(new OnItemClickListener() {   @Override   public void onItemClick(AdapterView arg0, View arg1, int arg2,     long arg3) {    // TODO Auto-generated method stub    //由于这是用户的点击事件,所以由此可以确定点击的那个位置(arg2),所以采用此方法返回结果    textView3.setText(data[arg2]);   }  }); } class MyButton implements OnClickListener@Override public void onClick(View view) {  // TODO Auto-generated method stub  switch (view.getId()) {  case R.id.buttonadd:   if(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 textList=new ArrayList();    public MyAdapter(Context context) {   this.context=context;  }  @Override  public int getCount() {   // TODO Auto-generated method stub   return textList.size();  }  @Override  public Object getItem(int arg0) {   // TODO Auto-generated method stub   return textList.get(arg0);  }  @Override  public long getItemId(int arg0) {   // TODO Auto-generated method stub   return arg0;  }  @Override  public View getView(int arg0, View arg1, ViewGroup arg2) {   // TODO Auto-generated method stub   String 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();  }   }}


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

你可能感兴趣的:(android开发--详解ListView,动态添加,删除Adapter中的数据项)