私のコードを見てください。ListActivity でボタンが押されたときにアラートが表示されるようにします。この場合は位置 0 です。ボタンが押されると、ユーザーが新しいカテゴリを作成できるようにするアラートが表示されます。ユーザーがカテゴリとして必要とする文字列を取得し、それを arraylist に追加する必要があります。私が何時間も試みてきたのを助けてくださいT_T
public class Data extends ListActivity {
ArrayList<String> items = new ArrayList<String>();
private Context show;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
items.add("+ Create New");
setListAdapter(new ArrayAdapter<String>(Data.this,
android.R.layout.simple_list_item_1, items));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if (position == 0) {
items.add(getText());
}
protected void getText() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
// Need to add value to arraylist!
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
}
……
私はそれを考え出した。
protected void setCategory() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("New Category");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
//create a button that says ok, and can be pressed
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
getValue(value);
//getValue() allows the string to be taken out of this method
items.add(output);//put the string into the global variable
/*
* I dont understand why this way works over making "value" a string and then adding it
* as the global variable.
*/
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//do nothing
}
});
alert.show();
}
protected void getValue(Editable theInput) {
String input = theInput.toString();
output = input;
}
}