我有一个自定义类Distraction和一个使用ArrayAdapter显示Distracctions的ArrayList 单击列表中的各个项目后,将出现一个弹出菜单,其中包含“编辑”和“删除”选项 按下编辑时,会出现一个警告对话框,其中包含两个EditTexts,用于输入用户输入的名称和优先级 然后我想将用户输入与先前存储的值进行比较,如果不同则更新 但是,无论我在任何字段中输入什么,代码当前都会将名称更改为空白。
代码:
分心等级
public class Distraction {
//Distraction properties
private String distraction_name;
private String distraction_priority;
private String distraction_occurance;
//Constructor of Distraction type
public Distraction (String distraction_name, String distraction_priority, String distraction_occurance)
this.distraction_name = distraction_name;
this.distraction_priority = distraction_priority;
this.distraction_occurance = distraction_occurance;
}
//Getter methods
public String getDistraction_name(){return distraction_name;}
public String getDistraction_priority(){return distraction_priority;}
public String getDistraction_occurance(){return distraction_occurance;}
public void setDistraction_name(String distraction_name){
this.distraction_name = distraction_name;
}
public void setDistraction_priority(String distraction_priority){
this.distraction_priority = distraction_priority;
}
public void setDistraction_occurance(String distraction_occurance) {
this.distraction_occurance = distraction_occurance;
}
}
我的活动代码
//Declare an Array List of Distractions
final ArrayList<Distraction> distractionsArray = new ArrayList<>();
//Creation of first sample data
distractionsArray.add(new Distraction("Example: Text from Alice", "3", "1"));
//Initialise the custom array adapter (nested ArrayAdapter class at the end of this activity)
final ArrayAdapter<Distraction> adapter = new distractionArrayAdapter(DistractionsActivity.this, 0, distractionsArray );
//Bind list view with the custom adapter
distractions_list.setAdapter(adapter);
//Adapter onclickable function
distractions_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//Popup menu for distraction (EDIT, REMOVE)
PopupMenu popup2 = new PopupMenu(DistractionsActivity.this, view);
popup2.inflate(R.menu.distraction_menu);
popup2.show();
popup2.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//WHEN USER PRESSES EDIT
if (item.getItemId() == R.id.Edit) {
LayoutInflater dialog = LayoutInflater.from(DistractionsActivity.this);
final View edit_distraction_dialog_view = dialog.inflate(R.layout.edit_distractions, null);
//Create and initialise an AlertBuilder to open a pop-up where
//the user will be able to give some info about the distraction
final AlertDialog.Builder builder = new AlertDialog.Builder(DistractionsActivity.this);
builder.setTitle("Edit Distraction").setView(edit_distraction_dialog_view);
builder.setView(R.layout.edit_distractions);
//Set positive OK button for alert dialog
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final EditText input_name = (EditText) edit_distraction_dialog_view.findViewById(R.id.edit_distraction_name_edittext);
final EditText input_priority = (EditText) edit_distraction_dialog_view.findViewById(R.id.edit_distraction_priority_edittext);
final String new_name = input_name.getText().toString();
final String new_priority = input_priority.getText().toString();
final String old_name = distractionsArray.get(position).getDistraction_name();
final String old_priority = distractionsArray.get(position).getDistraction_priority();
if (!(new_name.equals(old_name))){
distractionsArray.get(position).setDistraction_name(new_name);
}
else
{
Toast.makeText(DistractionsActivity.this, "No input!", Toast.LENGTH_SHORT);
}
adapter.notifyDataSetChanged();
}
});
//Set negative CANCEL button for the alert dialog
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Close alert dialog
dialog.cancel();
}
});
builder.show();
}
//WHEN USER PRESSES REMOVE
if (item.getItemId() == R.id.Remove) {
adapter.remove(distractionsArray.get(position));
adapter.notifyDataSetChanged();
}
return false;
}
});
}
});
它也不会在else语句中打印Toast。
有人可以解释为什么我的输入没有通过以及如何解决它?
答案 0 :(得分:2)
因此,假设视图正确加载,此代码本身应该可以正常工作。
final EditText input_name = (EditText) edit_distraction_dialog_view.findViewById(R.id.edit_distraction_name_edittext);
final EditText input_priority = (EditText) edit_distraction_dialog_view.findViewById(R.id.edit_distraction_priority_edittext);
// You can debug and set breakpoints on these, though
final String new_name = input_name.getText().toString();
final String new_priority = input_priority.getText().toString();
如果你得到空字符串,那么有几种可能性
1)你没有打字。但是,我怀疑
2)对getText().toString()
的调用并不等待您点击按钮。但是你在positive
点击操作上这样做了。
3)您在另一个对话框的顶部加载了一个新视图。
似乎就是这种情况
// An inflater
LayoutInflater inflater = LayoutInflater.from(DistractionsActivity.this);
// An inflated View
final View edit_distraction_dialog_view = inflater.inflate(R.layout.edit_distractions, null);
// A dialog
final AlertDialog.Builder builder = new AlertDialog.Builder(DistractionsActivity.this);
builder
.setTitle("Edit Distraction")
.setView(edit_distraction_dialog_view); // You already called setView!
// Don't need to call it again!
// builder.setView(R.layout.edit_distractions);
您可以将findViewById
移到onClick
builder.show(); // Need to call this first
final EditText input_name = (EditText) edit_distraction_dialog_view.findViewById(R.id.edit_distraction_name_edittext);
final EditText input_priority = (EditText) edit_distraction_dialog_view.findViewById(R.id.edit_distraction_priority_edittext);
//Set positive OK button for alert dialog
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String new_name = input_name.getText().toString();
String new_priority = input_priority.getText().toString();