編輯:關於 Android 程式設計
ListView 除了顯示資料,還能讓使用者動態新增與刪除項目。以下用一個含輸入框與按鈕的範例,示範如何實現項目的即時增刪。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
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 {
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) {
// 點擊新增:加入空項目並刷新
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++) { // 初始化 3 個子項
arr.add("");
}
}
@Override
public int getCount() {
return arr.size();
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(final int position, View view, ViewGroup arg2) {
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) {
if (arr.size() > 0) {
arr.set(position, edit.getText().toString());
}
}
});
del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 從集合中刪除該項並刷新
arr.remove(position);
adapter.notifyDataSetChanged();
}
});
return view;
}
}
}
核心觀念:ListView 的增刪本質上是對背後資料集合(arr)做操作,改完後呼叫 notifyDataSetChanged() 通知 Adapter 刷新即可。另外,由於 ListView 會複用 View,特別在 getView 中把 EditText 的文字重新 setText 一次,並在焦點改變時把輸入內容同步回集合,能有效避免資料錯亂的問題。
Android WebView 填坑記錄:那些版本碎片化帶來的糾結與解法
前言 在應用程序開發過程中,我們經常會採用 WebView 來展現某些界面,這樣就能不受發布版本的控制,實時更新,遇到問題也能快速修復。 但在 Android 開發中,由
跟 Google 學寫 App:Building Apps with Connectivity & the Cloud(Web App 篇)
本文整理自 Google 官方課程 Building Apps with Connectivity & the Cloud 第二節 Performing Netw
IntentService 源碼全解析:它憑什麼能自動收工,還不卡 UI
IntentService 到底是什麼 IntentService 可以理解成 Android 內建的「異步請求加工廠」。你只要用 Context.startServic
Android 縮圖載入優化:以 inSampleSize 與 createScaledBitmap 節省記憶體
為何要載入縮圖 在列表或資訊流中,通常不需要載入原圖,只要顯示縮圖即可。這樣可以明顯降低記憶體使用,避免大圖造成崩潰。 載入流程 可先使用 inJustDecodeB