欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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防止按鈕過(guò)快點(diǎn)擊造成多次事件的解決方法

    Android防止按鈕過(guò)快點(diǎn)擊造成多次事件的解決方法

    這篇文章主要介紹了Android防止按鈕過(guò)快點(diǎn)擊造成多次事件的解決方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Flutter 日期時(shí)間DatePicker控件及國(guó)際化

    Flutter 日期時(shí)間DatePicker控件及國(guó)際化

    這篇文章主要介紹了Flutter 日期時(shí)間DatePicker控件及國(guó)際化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Kotlin對(duì)象比較注意點(diǎn)示例詳解

    Kotlin對(duì)象比較注意點(diǎn)示例詳解

    你知道在kotlin中,如何來(lái)比較對(duì)象相等嗎?下面這篇文章主要給大家介紹了關(guān)于Kotlin對(duì)象比較注意點(diǎn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Android mvvm之LiveData原理案例詳解

    Android mvvm之LiveData原理案例詳解

    這篇文章主要介紹了Android mvvm之LiveData原理案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Android繼承ViewGroup實(shí)現(xiàn)Scroll滑動(dòng)效果的方法示例

    Android繼承ViewGroup實(shí)現(xiàn)Scroll滑動(dòng)效果的方法示例

    這篇文章主要介紹了Android繼承ViewGroup實(shí)現(xiàn)Scroll滑動(dòng)效果的方法,結(jié)合實(shí)例形式分析了Android滑動(dòng)效果的原理及擴(kuò)展ViewGroup實(shí)現(xiàn)滑動(dòng)功能的相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android仿微信頂/底部菜單欄效果

    Android仿微信頂/底部菜單欄效果

    這篇文章主要介紹了Android仿微信底部菜單欄和頂部菜單欄實(shí)現(xiàn)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android 物理按鍵整理及實(shí)例代碼

    Android 物理按鍵整理及實(shí)例代碼

    這篇文章主要介紹了Android 物理按鍵整理及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解

    Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解

    這篇文章主要介紹了Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解,本文詳細(xì)講解了am命令的語(yǔ)法,然后給出了啟動(dòng)內(nèi)置程序的操作實(shí)例,需要的朋友可以參考下
    2015-04-04
  • Android?Studio?2022.1.1創(chuàng)建項(xiàng)目的Gradle配置問(wèn)題

    Android?Studio?2022.1.1創(chuàng)建項(xiàng)目的Gradle配置問(wèn)題

    這篇文章主要介紹了Android?Studio?2022.1.1創(chuàng)建項(xiàng)目的Gradle配置問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • App內(nèi)切換語(yǔ)言詳解

    App內(nèi)切換語(yǔ)言詳解

    本篇文章主要介紹了App內(nèi)切換語(yǔ)言的方法。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04

最新評(píng)論