2011年5月31日星期二

動態修改列表視圖(ListView)的內容

修改前文"動態刪除列表視圖(ListView)的內容"的程序代碼, 在對話框(delAlertDialog)新增修改選項, 再建立另一個包含編輯文本視圖 (EditText) 的對話框(editDialog). 完成後, 通過 MyArrayAdapter.remove() 和 MyArrayAdapter.insert() 修改列表視圖(ListView)的內容.

動態修改列表視圖(ListView)的內容

package com.AndroidDynList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class AndroidDynList extends Activity {

EditText input;
Button add, clear;
ListView listview;
ArrayAdapter<String> MyArrayAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
input = (EditText)findViewById(R.id.input);
add = (Button)findViewById(R.id.add);
clear = (Button)findViewById(R.id.clear);
listview = (ListView)findViewById(R.id.list);

MyArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listview.setAdapter(MyArrayAdapter);

add.setOnClickListener(addOnClickListener);
clear.setOnClickListener(clearOnClickListener);

listview.setOnItemClickListener(listViewOnItemClickListener);
}

private Button.OnClickListener addOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String newInput = input.getText().toString();
MyArrayAdapter.add(newInput);
MyArrayAdapter.notifyDataSetChanged();
}};

private Button.OnClickListener clearOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyArrayAdapter.clear();
MyArrayAdapter.notifyDataSetChanged();
}};

private ListView.OnItemClickListener listViewOnItemClickListener
= new ListView.OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, final int position,
long id) {
// TODO Auto-generated method stub
final String strSelectedItem = parent.getItemAtPosition(position).toString();

AlertDialog.Builder delAlertDialog = new AlertDialog.Builder(AndroidDynList.this);
delAlertDialog.setTitle("- DELETE/Edit Selected Item? -");
delAlertDialog.setMessage(strSelectedItem);
delAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
MyArrayAdapter.remove(strSelectedItem);
MyArrayAdapter.notifyDataSetChanged();
}
});
delAlertDialog.setNeutralButton("Edit", new DialogInterface.OnClickListener() {

// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
AlertDialog.Builder editDialog = new AlertDialog.Builder(AndroidDynList.this);
editDialog.setTitle("--- Edit ---");

final EditText editText = new EditText(AndroidDynList.this);
editText.setText(strSelectedItem);
editDialog.setView(editText);

editDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
MyArrayAdapter.remove(strSelectedItem);
MyArrayAdapter.insert(editText.getText().toString(), position);
}});
editDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
//...
}
});
editDialog.show();
}
});
delAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
//...
}
});
delAlertDialog.show();
}};
}


沒有留言:

發佈留言