最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2013年03月03日 16:12:29   作者:  
ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除

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; 

         } 

     } 

 }

相關(guān)文章

  • Android隨手筆記44之JSON數(shù)據(jù)解析

    Android隨手筆記44之JSON數(shù)據(jù)解析

    本文將主要介紹在Android開發(fā)中,如何在服務(wù)器端創(chuàng)建JSON數(shù)據(jù),以及如何在Android客戶端對(duì)JSON數(shù)據(jù)進(jìn)行解析,對(duì)android json解析 相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android調(diào)用OpenCV2.4.10實(shí)現(xiàn)二維碼區(qū)域定位

    Android調(diào)用OpenCV2.4.10實(shí)現(xiàn)二維碼區(qū)域定位

    這篇文章主要為大家詳細(xì)介紹了Android調(diào)用OpenCV 2.4.10實(shí)現(xiàn)二維碼區(qū)域定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Android實(shí)現(xiàn)recyclerview城市字母索引列表

    Android實(shí)現(xiàn)recyclerview城市字母索引列表

    大家好,本篇文章主要講的是Android實(shí)現(xiàn)recyclerview城市字母索引列表,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Android系統(tǒng)設(shè)置中的清除數(shù)據(jù)會(huì)清除哪些數(shù)據(jù)?

    Android系統(tǒng)設(shè)置中的清除數(shù)據(jù)會(huì)清除哪些數(shù)據(jù)?

    這篇文章主要介紹了Android系統(tǒng)設(shè)置中的清除數(shù)據(jù)會(huì)清除哪些數(shù)據(jù)?本文對(duì)比了清除前和清除后的數(shù)據(jù)情況,從而得出到底清除了哪些數(shù)據(jù),需要的朋友可以參考下
    2015-01-01
  • Android中dataBinding使用的簡(jiǎn)單封裝

    Android中dataBinding使用的簡(jiǎn)單封裝

    前面一段時(shí)間學(xué)習(xí)了一下Android中的DataBinding,但是只是很簡(jiǎn)單地實(shí)現(xiàn)了一下,DataBinding中最強(qiáng)大的地方還沒有認(rèn)真地學(xué)習(xí)過,有很多地方還不理解,下面這篇文章主要給大家介紹了關(guān)于Android中dataBinding使用的簡(jiǎn)單封裝,需要的朋友可以參考下
    2023-06-06
  • Android圓形旋轉(zhuǎn)菜單開發(fā)實(shí)例

    Android圓形旋轉(zhuǎn)菜單開發(fā)實(shí)例

    本文給大家分享一個(gè)動(dòng)畫菜單,基于android開發(fā)圓形旋轉(zhuǎn)菜單案例,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-09-09
  • Android強(qiáng)制下線功能實(shí)現(xiàn)的代碼示例

    Android強(qiáng)制下線功能實(shí)現(xiàn)的代碼示例

    本篇文章主要介紹了Android強(qiáng)制下線功能實(shí)現(xiàn)的代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • Android讀取服務(wù)器圖片的三種方法

    Android讀取服務(wù)器圖片的三種方法

    這篇文章主要為大家詳細(xì)介紹了Android讀取服務(wù)器圖片的三種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法

    Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法,涉及Android中Listview的響應(yīng)點(diǎn)擊與樣式變換相關(guān)操作技巧,需要的朋友可以參考下
    2015-12-12
  • AndroidStudio3.6的卸載安裝,Gradle持續(xù)下載/Gradle Build失敗等問題

    AndroidStudio3.6的卸載安裝,Gradle持續(xù)下載/Gradle Build失敗等問題

    這篇文章主要介紹了AndroidStudio3.6的卸載安裝,Gradle持續(xù)下載/Gradle Build失敗等問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評(píng)論

易门县| 涿鹿县| 长子县| 吴江市| 静海县| 巫溪县| 佛学| 怀化市| 巴楚县| 东阿县| 建始县| 浮山县| 本溪市| 河曲县| 福州市| 龙岩市| 响水县| 桐乡市| 祁门县| 建瓯市| 河东区| 历史| 八宿县| 乐业县| 栾川县| 乌海市| 泸西县| 林州市| 静海县| 赤壁市| 康保县| 电白县| 乌拉特中旗| 新邵县| 通化县| 扶风县| 漳平市| 左贡县| 赤城县| 南城县| 察隅县|