android ListView內資料的動態添加與刪除執行個體代碼

來源:互聯網
上載者:User

main.xml 檔案:

複製代碼 代碼如下:

<?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檔案: 複製代碼 代碼如下: <?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

複製代碼 代碼如下: 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個子項

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)); //在重構adapter的時候不至於資料錯亂

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

//從集合中刪除所刪除項的EditText的內容

arr.remove(position);

adapter.notifyDataSetChanged();

}

});

return view;

}

}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.