我是编程/ Android的新手,我在从DialogFragment窗口编辑listItem后尝试刷新ListView。这是定义ListView的地方:
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();
}
}
}
以下是对话窗口的代码:
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);
}
}
我已经被困在这几天了。所有帮助表示赞赏!
答案 0 :(得分:0)
I assume Question Bank是contentprovider / contentresolver。请参阅链接中的notifyChange方法?此方法将触发数据集更改事件。侦听器such as this听到事件并重新加载数据/光标。也许不完全是你想要的,但也许这将有助于引导你走上正确的道路。