将 Dialog 的 EditText 设置为 ListView Item onItemLongClick 的值
Set EditText of Dialog to value of ListView Item onItemLongClick
我在 Fragment
中有一个 ListView
,其中有一个 ContextMenu
可以编辑和删除条目。 ContextMenu
在选择 EDIT
选项时启动 Dialog
。我想将 Dialog
的 EditText
设置为通过 ListView
上的 onItemLongClick
选择的项目的 String
值,但一直无法这样做。提前致谢。
片段:
public static class FragmentS extends Fragment {
private ListView saveListView;
private List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
private static final int EDIT = 0, DELETE = 1;
LiftSave longClickedItemLiftSave;
DatabaseHandler dbHandler;
ArrayAdapter<LiftSave> saveAdapter;
public FragmentS() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_s,
container, false);
saveListView = (ListView) rootView.findViewById(R.id.saveListView);
registerForContextMenu(saveListView);
DatabaseHandler dbHandler;
dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
if (dbHandler.getLiftSavesCount() != 0)
LiftSaves.clear();
LiftSaves.addAll(dbHandler.getAllLiftSaves());
populateList();
saveListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemLiftSave = LiftSaves.get(position);
return false;
}
});
return rootView;
}
private void populateList() {
saveAdapter = new SaveListAdapter();
saveListView.setAdapter(saveAdapter);
}
public class SaveListAdapter extends ArrayAdapter<LiftSave> {
public SaveListAdapter() {
super(getActivity(), R.layout.listview_item, LiftSaves);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getActivity().getLayoutInflater().inflate(R.layout.listview_item, parent, false);
LiftSave currentLiftSave = LiftSaves.get(position);
TextView liftName = (TextView) view.findViewById(R.id.liftName);
liftName.setText(currentLiftSave.getLiftName());
TextView maxValue = (TextView) view.findViewById(R.id.maxValue);
maxValue.setText(currentLiftSave.getMaxValue());
TextView liftNotes = (TextView) view.findViewById(R.id.liftNotes);
liftNotes.setText(currentLiftSave.getLiftNotes());
TextView weightAndReps = (TextView) view.findViewById(R.id.weightAndReps);
weightAndReps.setText(currentLiftSave.getRepsAndWeight());
TextView date = (TextView) view.findViewById(R.id.todayDate);
date.setText(currentLiftSave.getTodayDate());
return view;
}
}
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderIcon(R.drawable.pencil_icon);
menu.setHeaderTitle("Save Options");
menu.add(Menu.NONE, EDIT, menu.NONE, "Edit Save");
menu.add(Menu.NONE, DELETE, menu.NONE, "Delete Save");
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case EDIT:
final View dialogViewEdit = LayoutInflater.from(this.getActivity()).inflate(R.layout.edit_save, null, false);
final AlertDialog builderE = new AlertDialog.Builder(this.getActivity()).create();
TextView liftName = (TextView) getActivity().findViewById(R.id.liftName);
TextView maxValue = (TextView) getActivity().findViewById(R.id.maxValue);
TextView weightAndReps = (TextView) getActivity().findViewById(R.id.weightAndReps);
TextView liftNotes = (TextView) getActivity().findViewById(R.id.liftNotes);
TextView date = (TextView) getActivity().findViewById(R.id.todayDate);
EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
editName.setText(liftName.getText().toString());
EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
editNotes.setText(liftNotes.getText().toString());
EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
editWR.setText(weightAndReps.getText().toString());
EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
editMax.setText(maxValue.getText().toString());
EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
editDate.setText(date.getText().toString());
Button cancel =(Button) dialogViewEdit.findViewById(R.id.btnCancel);
Button save =(Button) dialogViewEdit.findViewById(R.id.btnSave);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
builderE.dismiss();
}
});
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
LiftSave liftSave = new LiftSave(longClickedItemLiftSave.getId() , String.valueOf(editName.getText()), String.valueOf(editMax.getText()), String.valueOf(editNotes.getText()), String.valueOf(editWR.getText()), String.valueOf(editDate.getText()));
dbHandler.updateLiftSave(liftSave);
LiftSaves.clear();
LiftSaves.addAll(dbHandler.getAllLiftSaves());
saveAdapter.notifyDataSetChanged();
}
});
builderE.setView(dialogViewEdit);
builderE.show();
break;
case DELETE:
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setMessage("Are you sure you want to delete this save?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
dbHandler.deleteLiftSave(longClickedItemLiftSave);
saveAdapter.remove(longClickedItemLiftSave);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialogD = builder.create();
dialogD.show();
break;
}
return super.
onContextItemSelected(item);
}
}
似乎如果 longClickedItemLiftSave
得到正确填充,您可以将代码更改为这样,它会修复它:
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case EDIT:
final View dialogViewEdit = LayoutInflater.from(this.getActivity()).inflate(R.layout.edit_save, null, false);
final AlertDialog builderE = new AlertDialog.Builder(this.getActivity()).create();
/* not needed
TextView liftName = (TextView) getActivity().findViewById(R.id.liftName);
TextView maxValue = (TextView) getActivity().findViewById(R.id.maxValue);
TextView weightAndReps = (TextView) getActivity().findViewById(R.id.weightAndReps);
TextView liftNotes = (TextView) getActivity().findViewById(R.id.liftNotes);
TextView date = (TextView) getActivity().findViewById(R.id.todayDate);
*/
//Use values from longClickedItemLiftSave
EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
editName.setText(longClickedItemLiftSave.getLiftName());
EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
editNotes.setText(longClickedItemLiftSave.getLiftNotes());
EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
editWR.setText(longClickedItemLiftSave.getRepsAndWeight());
EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
editMax.setText(longClickedItemLiftSave.getMaxValue());
EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
editDate.setText(longClickedItemLiftSave.getTodayDate());
我在 Fragment
中有一个 ListView
,其中有一个 ContextMenu
可以编辑和删除条目。 ContextMenu
在选择 EDIT
选项时启动 Dialog
。我想将 Dialog
的 EditText
设置为通过 ListView
上的 onItemLongClick
选择的项目的 String
值,但一直无法这样做。提前致谢。
片段:
public static class FragmentS extends Fragment {
private ListView saveListView;
private List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
private static final int EDIT = 0, DELETE = 1;
LiftSave longClickedItemLiftSave;
DatabaseHandler dbHandler;
ArrayAdapter<LiftSave> saveAdapter;
public FragmentS() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_s,
container, false);
saveListView = (ListView) rootView.findViewById(R.id.saveListView);
registerForContextMenu(saveListView);
DatabaseHandler dbHandler;
dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
if (dbHandler.getLiftSavesCount() != 0)
LiftSaves.clear();
LiftSaves.addAll(dbHandler.getAllLiftSaves());
populateList();
saveListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemLiftSave = LiftSaves.get(position);
return false;
}
});
return rootView;
}
private void populateList() {
saveAdapter = new SaveListAdapter();
saveListView.setAdapter(saveAdapter);
}
public class SaveListAdapter extends ArrayAdapter<LiftSave> {
public SaveListAdapter() {
super(getActivity(), R.layout.listview_item, LiftSaves);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getActivity().getLayoutInflater().inflate(R.layout.listview_item, parent, false);
LiftSave currentLiftSave = LiftSaves.get(position);
TextView liftName = (TextView) view.findViewById(R.id.liftName);
liftName.setText(currentLiftSave.getLiftName());
TextView maxValue = (TextView) view.findViewById(R.id.maxValue);
maxValue.setText(currentLiftSave.getMaxValue());
TextView liftNotes = (TextView) view.findViewById(R.id.liftNotes);
liftNotes.setText(currentLiftSave.getLiftNotes());
TextView weightAndReps = (TextView) view.findViewById(R.id.weightAndReps);
weightAndReps.setText(currentLiftSave.getRepsAndWeight());
TextView date = (TextView) view.findViewById(R.id.todayDate);
date.setText(currentLiftSave.getTodayDate());
return view;
}
}
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderIcon(R.drawable.pencil_icon);
menu.setHeaderTitle("Save Options");
menu.add(Menu.NONE, EDIT, menu.NONE, "Edit Save");
menu.add(Menu.NONE, DELETE, menu.NONE, "Delete Save");
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case EDIT:
final View dialogViewEdit = LayoutInflater.from(this.getActivity()).inflate(R.layout.edit_save, null, false);
final AlertDialog builderE = new AlertDialog.Builder(this.getActivity()).create();
TextView liftName = (TextView) getActivity().findViewById(R.id.liftName);
TextView maxValue = (TextView) getActivity().findViewById(R.id.maxValue);
TextView weightAndReps = (TextView) getActivity().findViewById(R.id.weightAndReps);
TextView liftNotes = (TextView) getActivity().findViewById(R.id.liftNotes);
TextView date = (TextView) getActivity().findViewById(R.id.todayDate);
EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
editName.setText(liftName.getText().toString());
EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
editNotes.setText(liftNotes.getText().toString());
EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
editWR.setText(weightAndReps.getText().toString());
EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
editMax.setText(maxValue.getText().toString());
EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
editDate.setText(date.getText().toString());
Button cancel =(Button) dialogViewEdit.findViewById(R.id.btnCancel);
Button save =(Button) dialogViewEdit.findViewById(R.id.btnSave);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
builderE.dismiss();
}
});
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
LiftSave liftSave = new LiftSave(longClickedItemLiftSave.getId() , String.valueOf(editName.getText()), String.valueOf(editMax.getText()), String.valueOf(editNotes.getText()), String.valueOf(editWR.getText()), String.valueOf(editDate.getText()));
dbHandler.updateLiftSave(liftSave);
LiftSaves.clear();
LiftSaves.addAll(dbHandler.getAllLiftSaves());
saveAdapter.notifyDataSetChanged();
}
});
builderE.setView(dialogViewEdit);
builderE.show();
break;
case DELETE:
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setMessage("Are you sure you want to delete this save?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
dbHandler.deleteLiftSave(longClickedItemLiftSave);
saveAdapter.remove(longClickedItemLiftSave);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialogD = builder.create();
dialogD.show();
break;
}
return super.
onContextItemSelected(item);
}
}
似乎如果 longClickedItemLiftSave
得到正确填充,您可以将代码更改为这样,它会修复它:
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case EDIT:
final View dialogViewEdit = LayoutInflater.from(this.getActivity()).inflate(R.layout.edit_save, null, false);
final AlertDialog builderE = new AlertDialog.Builder(this.getActivity()).create();
/* not needed
TextView liftName = (TextView) getActivity().findViewById(R.id.liftName);
TextView maxValue = (TextView) getActivity().findViewById(R.id.maxValue);
TextView weightAndReps = (TextView) getActivity().findViewById(R.id.weightAndReps);
TextView liftNotes = (TextView) getActivity().findViewById(R.id.liftNotes);
TextView date = (TextView) getActivity().findViewById(R.id.todayDate);
*/
//Use values from longClickedItemLiftSave
EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
editName.setText(longClickedItemLiftSave.getLiftName());
EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
editNotes.setText(longClickedItemLiftSave.getLiftNotes());
EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
editWR.setText(longClickedItemLiftSave.getRepsAndWeight());
EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
editMax.setText(longClickedItemLiftSave.getMaxValue());
EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
editDate.setText(longClickedItemLiftSave.getTodayDate());