public class Dialog_Personal_info extends Dialog {
EditText
editName , editAddress , editPhone;
Button
confirm, cancel, newList;
ListView list;
Context context;
public static ArrayList<HashMap<String, String>> list_Data;
private SimpleAdapter adapter;
public Dialog_Personal_info(Context context) throws JSONException {
super(context);
setContentView(R.layout.dialog_personal_info);
this.context = context;
this.callback = callback;
try {
initViews();
setActions();
} catch (JSONException e) {
e.printStackTrace();
}
}
private void initViews() throws JSONException {
list_Data = new ArrayList<HashMap<String, String>>();
editName = (EditText)findViewById(R.id.info_dialog_name);
editAddress = (EditText)findViewById(R.id.info_dialog_address);
editPhone = (EditText)findViewById(R.id.info_dialog_phone);
confirm = (Button)findViewById(R.id.info_dialog_confirm);
cancel = (Button)findViewById(R.id.info_dialog_cancel);
newList = (Button)findViewById(R.id.info_dialog_new);
list = (ListView) findViewById(R.id.info_personal_list);
//dbList 來源可從sqlite取得 這裡就不提供囉
for (int i = 0; dbList.size() > i; i++) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("NAME", personal.infos.get(i).name);
item.put("PHONE", personal.infos.get(i).phone);
item.put("ADDRESS", personal.infos.get(i).address);
list_Data.add(item);
}
//自行定義一個Adapter 這裡下面會說
adapter = new Dialog_Personal_Adapter(context,list_Data);
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
private void setActions(){
//確認按鈕
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(HashMap<String, String> list:list_Data){
.......//這裡取得ListView中的資訊
}
dismiss();
}
});
//關閉Dailog
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
//當使用者按下新增則原有的list_Data新增一筆空的值(當然你也可以給個預設值)
newList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("NAME", "");
item.put("PHONE", "");
item.put("ADDRESS", "");
list_Data.add(item);
adapter =
new Dialog_Personal_Adapter(
context,list_Data,Dialog_Personal_info.this);
list.setAdapter(adapter);
}
});
}
}
好了一個基本的Dailog可以顯示了
接下來是本文的重點動態編輯ListView中的List
public class Dialog_Personal_Adapter extends SimpleAdapter {
private Context mContext;
public static ArrayList<HashMap<String, String>> list_Data;
public Dialog_Personal_Adapter(Context context, ArrayList<HashMap<String, String>> list_Data2) {
super(context, list_Data2, 0, null, null);
this.mContext = context;
this.list_Data = list_Data2;
this.dialog_Personal_info = dialog_Personal_info;
}
public int getCount() {
return this.list_Data.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup arg2) {
ViewHolder viewHolder = null;
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.personal_list, null);
viewHolder.btnDel = (Button) view.findViewById(R.id.info_dialog_delect);
viewHolder.editName = (EditText)view.findViewById(R.id.info_dialog_name);
viewHolder.editAddress = (EditText)view.findViewById(R.id.info_dialog_address);
viewHolder.editPhone = (EditText)view.findViewById(R.id.info_dialog_phone);
//畫面初始由前段傳入的值直接帶入editText中
viewHolder.editName.setText(list_Data.get(position).get("NAME"));
viewHolder.editPhone.setText(list_Data.get(position).get("PHONE"));
viewHolder.editAddress.setText(list_Data.get(position).get("ADDRESS"));
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
//監聽ListView中的刪除按鈕
viewHolder.btnDel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//確認刪除
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setMessage(mContext.getString(R.string.sureDelete));
dialog.setPositiveButton(mContext.getString(R.string.connfirmed),new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
......//確認刪除這邊就執行刪除的功能囉
}
});
//確認刪除取消按鈕
dialog.setNegativeButton(mContext.getString(R.string.cancel),new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
dialog.show();
}
});
//這裡是當editText 有被修改則會執行此method 寫入 list_Data ,如果沒有用此方法, 則就算修改按了確定系統還是會抓不到editText中的值,當然如果有更好的方式歡 迎留言討論囉
editChang(viewHolder,position);
return view;
}
public void editChang(ViewHolder viewHolder, final int position){
//當名字修改的時候存入list_Data
viewHolder.editName.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
list_Data.get(position).put("NAME", arg0.toString());
}
});
//當地址修改的時候存入list_Data
viewHolder.editAddress.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
list_Data.get(position).put("ADDRESS", arg0.toString());
}
});
//當電話修改的時候存入list_Data
viewHolder.editPhone.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
list_Data.get(position).put("PHONE", arg0.toString());
}
});
}
final static class ViewHolder {
Button btnDel;
EditText editName;
EditText editAddress;
EditText editPhone;
}
}
這樣就完成囉
加入一筆試試看(ListView跑出去啦~原諒我沒時間調整 ~>0<~)
刪除一筆看看
功能都正常!!
接下來把 xml 交給各位
dialog_personal_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="550dp"
android:paddingLeft="22dp"
android:paddingRight="22dp"
android:layout_marginTop="60dp"
android:orientation="vertical"
android:background="@drawable/v2_bg_data" >
<LinearLayout
android:id="@+id/list_layout"
android:layout_width="match_parent"
android:layout_height="298dp"
android:layout_marginTop="156dp"
android:layout_gravity="center_horizontal"
android:gravity="center">
<ListView
android:id="@+id/info_personal_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:scrollbarSize="0dp">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:gravity="center">
<Button
android:id="@+id/info_dialog_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:textSize="22dp"
android:background="@drawable/v2_btn_data"
android:text="@string/added"/>
<Button
android:id="@+id/info_dialog_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:textSize="22dp"
android:background="@drawable/v2_btn_data"
android:text="@string/connfirmed"/>
<Button
android:id="@+id/info_dialog_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:background="@drawable/v2_btn_data"
android:text="@string/backtrack"/>
</LinearLayout>
</LinearLayout>
personal_list.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginRight="2dp"
android:layout_marginLeft="6dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5a3a23"
android:textSize="22dp"
android:text="@string/name"/>
<EditText
android:id="@+id/info_dialog_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#5a3a23"
android:textSize="22dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5a3a23"
android:textSize="22dp"
android:text="@string/phone"/>
<EditText
android:id="@+id/info_dialog_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="10"
android:textColor="#5a3a23"
android:phoneNumber="true"
android:textSize="22dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5a3a23"
android:textSize="22dp"
android:text="@string/address"/>
<EditText
android:id="@+id/info_dialog_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#5a3a23"
android:textSize="22dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/info_dialog_delect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete"
android:background="@drawable/v2_btn_data"
android:textSize="22dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
沒有留言:
張貼留言