package sg.edu.rp.c345.p04;
import java.util.ArrayList;
import java.util.Collections;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class toDo extends ListActivity {
/** Called when the activity is first created. */
EditText toDoEditText;
Button submitButton;
ListView toDoListView;
ArrayList<String> toDoTasks;
ArrayAdapter<String> taskArrayAdapter;
static final private int SORT_TASKS = Menu.FIRST;
static final private int REVERSE_SORT_TASKS = Menu.FIRST + 1;
static final private int REMOVE_TASKS = Menu.FIRST + 2;
static final private int EDIT_TASKS = Menu.FIRST + 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toDoEditText = (EditText) findViewById(R.id.toDoEditText);
submitButton = (Button) findViewById(R.id.submitButton);
toDoListView = getListView();
toDoTasks = new ArrayList<String>();
taskArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, toDoTasks);
toDoListView.setAdapter(taskArrayAdapter);
registerForContextMenu(toDoListView);
submitButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (toDoEditText.getText().toString().length() == 0) {
return;
}
String task = toDoEditText.getText().toString();
toDoTasks.add(task);
taskArrayAdapter.notifyDataSetChanged();
toDoEditText.setText("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu optionsMenu) {
super.onCreateOptionsMenu(optionsMenu);
// Create and add new menu items.
MenuItem itemSort = optionsMenu.add(0, SORT_TASKS, Menu.NONE,
R.string.sortAll);
MenuItem itemReverseSort = optionsMenu.add(0, REVERSE_SORT_TASKS,
Menu.NONE, R.string.sortReverse);
MenuItem itemRem = optionsMenu.add(0, REMOVE_TASKS, Menu.NONE,
R.string.removeAll);
itemSort.setIcon(R.drawable.sort_btn);
itemReverseSort.setIcon(R.drawable.reverse_sort_btn);
itemRem.setIcon(R.drawable.remove_btn);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem optionItem) {
super.onOptionsItemSelected(optionItem);
switch (optionItem.getItemId()) {
case (SORT_TASKS): {
// Perform the necessary task to sort the items in the ArrayList
// alphabetically
// Hint: use Collections.sort()
Collections.sort(toDoTasks);
taskArrayAdapter.notifyDataSetChanged();
return true;
}
case (REVERSE_SORT_TASKS): {
// Perform the necessary task to sort the items in the ArrayList
// reverse alphabetically
// Hint: use Collections.reverse()
Collections.reverse(toDoTasks);
taskArrayAdapter.notifyDataSetChanged();
return true;
}
case (REMOVE_TASKS): {
// Perform the necessary actions to remove ALL the tasks in the
// ArrayList
toDoTasks.clear();
taskArrayAdapter.notifyDataSetChanged();
return true;
}
}
return false;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Choose your action");
menu.add(0, REMOVE_TASKS, Menu.NONE, R.string.removeItem);
menu.add(0, EDIT_TASKS, Menu.NONE, R.string.editItem);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
switch (item.getItemId()) {
case (REMOVE_TASKS): {
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final int removeIndex = menuInfo.position;
toDoTasks.remove(removeIndex);
taskArrayAdapter.notifyDataSetChanged();
return true;
}
case (EDIT_TASKS): {
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final int editIndex = menuInfo.position;
final String editText = menuInfo.toString();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Edit Task");
alert.setMessage("Edit Task Message");
// Set an EditText view to get user input
final EditText editInput = new EditText(this);
alert.setView(editInput);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String editTask = editInput.getText().toString();
// Do something with value!
toDoTasks.set(editIndex, editTask);
taskArrayAdapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Canceled.
}
});
alert.show();
}
}
return false;
}
}
如何使EditText获取您长按的ArrayList元素的值?
答案 0 :(得分:1)
使用OnItemClickListener
。在该方法中写下类似的东西:
toDoEditText.setText(view.getText());
此处视图是您对该项目的看法。你应该自己实现方法getText()。
答案 1 :(得分:0)
也许AutoCompleteTextView效果更好