私はprogramming/Androidを初めて使い、DialogFragmentウィンドウからlistItemを編集した後、ListViewをリフレッシュしようとしています。リストビューが定義されているのはここです:listItemを変更した後のListViewのリフレッシュ
public class DialogShowAnswer extends DialogFragment {
private Question mQuestion;
private static final String KEY_INDEX = "index";
private static final String TAG = "DialogShowAnswer";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if(savedInstanceState != null) {
mQuestion = savedInstanceState.getParcelable(KEY_INDEX);
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_show_answer, null);
TextView questionText = (TextView) dialogView.findViewById(R.id.questionAnswerDialog);
TextView answerText = (TextView) dialogView.findViewById(R.id.answerAnswerDialog);
TextView dateText = (TextView) dialogView.findViewById(R.id.dateAnswerDialog);
final Button btnClose = (Button) dialogView.findViewById(R.id.btnClose);
final Button btnSave = (Button) dialogView.findViewById(R.id.btnSave);
ImageView editIcon = (ImageView) dialogView.findViewById(R.id.editIcon);
btnSave.setVisibility(View.GONE);
questionText.setText(mQuestion.getQuestion());
answerText.setText(mQuestion.getAnswer());
dateText.setText(mQuestion.getDateString());
builder.setView(dialogView);
editIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewSwitcher switcher = (ViewSwitcher) dialogView.findViewById(R.id.viewSwitcher);
final EditText editAnswer = (EditText) switcher.findViewById(R.id.editAnswerDialog);
if(btnSave.getVisibility() == View.GONE) {
btnSave.setVisibility(View.VISIBLE);
} else {
btnSave.setVisibility(View.GONE);
}
switcher.showNext(); //or switcher.showPrevious();
editAnswer.setText(mQuestion.getAnswer());
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String answer = editAnswer.getText().toString();
if (answer.length() > 0) {
mQuestion.setAnswerDate();
mQuestion.setAnswer(answer);
QuestionBank questionBank = QuestionBank.get(getContext());
questionBank.updateQuestion(mQuestion.getId(), mQuestion);
AnswersList callingActivity = (AnswersList) getActivity();
callingActivity.getAdapter.updateList();
Log.d(TAG, "notifyDataSetChanged() called");
dismiss();
} else {
Toast.makeText(getContext(), R.string.empty_answer_field, Toast.LENGTH_SHORT).show();
}
}
});
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return builder.create();
}
public void sendQuestionSelected(Question question) {
mQuestion = question;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
if(mQuestion != null)
savedInstanceState.putParcelable(KEY_INDEX, mQuestion);
}
}
私は日のために、この上で立ち往生してきた:
public class AnswersList extends AppCompatActivity {
private ListView mListAnswer;
private AnswerAdapter mAnswerAdapter;
private static final String ANSWERS_TO_LISTVIEW = "answers_to_listview";
private static final String TAG = "AnswersList";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_answers);
mAnswerAdapter = new AnswerAdapter();
mListAnswer = (ListView) findViewById(R.id.listView);
mListAnswer.setAdapter(mAnswerAdapter);
// handle clicks on listview
mListAnswer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int whichItem, long id) {
Question tempQuestion = mAnswerAdapter.getItem(whichItem);
// create a new dialog window
DialogShowAnswer dialog = new DialogShowAnswer();
// send in a reference to the Note to be shown
dialog.sendQuestionSelected(tempQuestion);
// Show the dialog window with the note in it
dialog.show(getSupportFragmentManager(), "");
}
});
}
public class AnswerAdapter extends BaseAdapter {
ArrayList<Question> mListAnswers = new ArrayList<Question>();
QuestionBank mQuestionBank;
Context mContext = getApplicationContext();
public AnswerAdapter() {
mQuestionBank = QuestionBank.get(mContext);
mListAnswers = mQuestionBank.getAnswers();
}
public ArrayList<Question> getListAnswers() {
return mListAnswers;
}
@Override
public int getCount() {
return mListAnswers.size();
}
@Override
public Question getItem(int position) {
return mListAnswers.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
** A bunch of code here **
return convertView;
}
public void updateList() {
mListAnswers = mQuestionBank.getAnswers();
notifyDataSetChanged();
}
}
}
ここでダイアログウィンドウのためのコードです。すべての助けに感謝!
もう1つの関連する質問:最初のコードブロックで定義されたupdateList()を2番目のコードブロックから呼び出すことができないのはなぜですか? updateList()は、最初のブロックのネストされたBaseAdapterクラスのメソッドです。 2番目のブロック(callingActivity.getAdapter.updateList();)で呼び出そうとすると、Android Studioはそれを解決できないと言います。 –
私はリストビューを更新するための簡単な解決策があることを知り、私はそれを理解しました。代わりに: callActivity.getAdapter.updateList(); これは次のようになります。 ((AnswersList.AnswerAdapter)callingActivity.getAdapter())。updateList(); –