麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 系統(tǒng) > Android > 正文

android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼

2020-04-11 12:30:47
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

main.xml 文件: 

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

     android:layout_width="fill_parent" 

     android:layout_height="fill_parent" 

     android:orientation="horizontal"  

     > 

     <LinearLayout 

       android:layout_width="fill_parent" 

      android:layout_height="fill_parent"    

      android:orientation="vertical" 

      > 

     <ListView  

      android:id="@+id/listview"     

      android:layout_width="fill_parent" 

      android:layout_height="wrap_content" 

     /> 

     <Button  

      android:id="@+id/add"     

      android:layout_width="wrap_content" 

      android:layout_height="wrap_content"  

      android:text="添加" 

      /> 

     </LinearLayout> 

 </LinearLayout>


listview_item.xml文件:
復(fù)制代碼 代碼如下:

 <?xml version="1.0" encoding="utf-8"?> 

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

     android:layout_width="fill_parent" 

     android:layout_height="wrap_content" 

     android:orientation="horizontal" 

     android:background="#000000" 

     android:padding="20dp" 

     > 

        

     <EditText 

     android:id="@+id/edit" 

     android:layout_width="200dp" 

     android:layout_height="wrap_content" 

     /> 

     <Button 

     android:id="@+id/del" 

     android:layout_width="wrap_content" 

     android:layout_height="wrap_content"    

     android:text="刪除" 

     /> 

        

 </LinearLayout>

MainActivity .java

復(fù)制代碼 代碼如下:

 package com.yyy.testandroid; 

    
import java.util.ArrayList; 
    

import android.app.Activity; 

import android.content.Context; 

import android.os.Bundle; 

import android.view.LayoutInflater; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.view.View.OnFocusChangeListener; 

import android.view.ViewGroup; 

 import android.widget.BaseAdapter; 

 import android.widget.Button; 

 import android.widget.EditText; 

 import android.widget.ListView; 

 import android.widget.TextView; 

    

 public class TestAndroidActivity extends Activity { 

     /** Called when the activity is first created. */ 

        

     private Button button,add; 

     private TextView text; 

     private ListView listview; 

     public MyAdapter adapter; 

     @Override 

     public void onCreate(Bundle savedInstanceState) { 

         super.onCreate(savedInstanceState); 

         setContentView(R.layout.main); 

         listview = (ListView) findViewById(R.id.listview); 

         add = (Button) findViewById(R.id.add); 

         adapter = new MyAdapter(this); 

         listview.setAdapter(adapter); 

            

         add.setOnClickListener(new OnClickListener() { 

             @Override 

             public void onClick(View arg0) { 

                 // TODO Auto-generated method stub 

                 adapter.arr.add(""); 

                 adapter.notifyDataSetChanged(); 

             } 

         }); 

     } 
 

     private class MyAdapter extends BaseAdapter { 

    

         private Context context; 

         private LayoutInflater inflater; 

         public ArrayList<String> arr; 

         public MyAdapter(Context context) { 

             super(); 

             this.context = context; 

             inflater = LayoutInflater.from(context); 

             arr = new ArrayList<String>(); 

             for(int i=0;i<3;i++){    //listview初始化3個(gè)子項(xiàng) 

                 arr.add(""); 

             } 

         } 

         @Override 

         public int getCount() { 

             // TODO Auto-generated method stub 

             return arr.size(); 

         } 

         @Override 

         public Object getItem(int arg0) { 

             // TODO Auto-generated method stub 

             return arg0; 

         } 

         @Override 

         public long getItemId(int arg0) { 

             // TODO Auto-generated method stub 

             return arg0; 

         } 

         @Override 

         public View getView(final int position, View view, ViewGroup arg2) { 

             // TODO Auto-generated method stub 

             if(view == null){ 

                 view = inflater.inflate(R.layout.list_item, null); 

             } 

             final EditText edit = (EditText) view.findViewById(R.id.edit); 

             edit.setText(arr.get(position));    //在重構(gòu)adapter的時(shí)候不至于數(shù)據(jù)錯(cuò)亂 

             Button del = (Button) view.findViewById(R.id.del); 

             edit.setOnFocusChangeListener(new OnFocusChangeListener() { 

                 @Override 

                 public void onFocusChange(View v, boolean hasFocus) { 

                     // TODO Auto-generated method stub 

                     if(arr.size()>0){ 

                         arr.set(position, edit.getText().toString()); 

                     } 

                 } 

             }); 

             del.setOnClickListener(new OnClickListener() { 

                 @Override 

                 public void onClick(View arg0) { 

                     // TODO Auto-generated method stub 

                     //從集合中刪除所刪除項(xiàng)的EditText的內(nèi)容 

                     arr.remove(position); 

                     adapter.notifyDataSetChanged(); 

                 } 

             }); 

             return view; 

         } 

     } 

 }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲第一成人在线 | 国产女做a爱免费视频 | 欧美成a人片在线观看久 | 日产精品久久久一区二区开放时间 | av影片在线观看 | 午夜视频大全 | 电影91| 国产一级二级在线播放 | 精品国产一区二区三区四 | 国产精品91久久久 | 激情小说另类 | 久久精品欧美视频 | 中文字幕精品一二三四五六七八 | 久久久久免费电影 | 久久生活片 | 成人资源在线观看 | 免费看操片| 爱逼爱操综合网 | 亚洲日韩精品欧美一区二区 | 日韩视频一区二区三区四区 | 二区三区四区 | 亚洲日色| 亚洲91网 | 成码无人av片在线观看网站 | 老子午夜影院 | h视频免费在线观看 | 国产精品午夜一区 | 欧美18videos性处按摩 | 涩涩激情网 | 国产一区二区亚洲 | 看av网址 | 国产国语毛片 | 在火车上摸两乳爽的大叫 | 中文字幕精品在线播放 | 中国a级黄色片 | 成人在线视频一区 | 自拍偷拍亚洲图片 | 久久久久久久网站 | 免费午夜视频 | 日本视频免费观看 | 欧洲精品久久久 |