gpt4 book ai didi

java - 如何使 EditText 获得您长按的 ArrayList 元素的值?

转载 作者:行者123 更新时间:2023-11-30 04:40:35 26 4
gpt4 key购买 nike

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 元素的值?

最佳答案

使用OnItemClickListener .在方法中写这样的东西:

toDoEditText.setText(view.getText());

此处的 View 是您对该项目的 View 。您应该自己实现方法 getText()。

关于java - 如何使 EditText 获得您长按的 ArrayList 元素的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6055839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com