android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
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個(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;
}
}
}
- Android列表組件ListView使用詳解之動(dòng)態(tài)加載或修改列表數(shù)據(jù)
- Android開(kāi)發(fā)中Listview動(dòng)態(tài)加載數(shù)據(jù)的方法示例
- Android 動(dòng)態(tài)添加view或item并獲取數(shù)據(jù)的實(shí)例
- Android 根據(jù)EditText搜索框ListView動(dòng)態(tài)顯示數(shù)據(jù)
- Android實(shí)現(xiàn)listview動(dòng)態(tài)加載數(shù)據(jù)分頁(yè)的兩種方法
- Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法
- Android實(shí)現(xiàn)動(dòng)態(tài)添加數(shù)據(jù)與堆疊折線圖詳解流程
相關(guān)文章
Android防止按鈕過(guò)快點(diǎn)擊造成多次事件的解決方法
這篇文章主要介紹了Android防止按鈕過(guò)快點(diǎn)擊造成多次事件的解決方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Flutter 日期時(shí)間DatePicker控件及國(guó)際化
這篇文章主要介紹了Flutter 日期時(shí)間DatePicker控件及國(guó)際化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Android繼承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-08Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解
這篇文章主要介紹了Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解,本文詳細(xì)講解了am命令的語(yǔ)法,然后給出了啟動(dòng)內(nèi)置程序的操作實(shí)例,需要的朋友可以參考下2015-04-04Android?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