Android ListView 未使用 notifyDataSetChanged 立即刷新

Android ListView not refreshed right away using notifyDataSetChanged

我正在构建一个 UI,我在其中使用 ListView 保存有关用户提出的问题的数据。每当用户创建新问题时,我都会尝试更新列表视图。现在的问题是数据更新了,但不是马上。添加问题后,我需要导航到另一个屏幕,然后返回查看更新后的列表。这也导致了一些我想修复的其他问题(布局未正确显示) 这是我的活动代码:

public class QuestionsScreen extends Activity {

.....
.....
.....

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions_screen);

        .....

        questions=new ArrayList<String>();
        qId=new ArrayList<Integer>();

        .....
        .....

        //building API
        resourceApi = ApiBuilder.getResourceApi(token);

        // calling API function to get questions from db

        resourceApi.listQuestions(groupId, new Callback<List<Question>>(){

            @Override
            public void failure(RetrofitError arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void success(List<Question> arg0, Response arg1) {
                // TODO Auto-generated method stub
                for(Question q:arg0)
                {
                    questions.add(q.getTitle());
                    qId.add(q.getId());

                }

            }

        });


        adapter= new ArrayAdapter<String>this,android.R.layout.simple_list_item_1,questions);
        lv.setAdapter(adapter);

        .....

        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        final View x;
        x = inflater.inflate(R.layout.groupdialog, null);
        dialog.setView(x);
        dialog.setTitle("Add question");
        TextView t= (TextView)x.findViewById(R.id.dialogTv);
        t.setText("Please type in the question");

        dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface d, int which) {
                // TODO Auto-generated method stub
                name = (EditText) x.findViewById(R.id.dialog_groupName);
                out = name.getText().toString();
                addQuestion();
                d.dismiss();
            }
        });

        dialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface d, int which) {
                        // TODO Auto-generated method stub
                        d.cancel();
                    }
                });

        alert = dialog.create();

        //button to ask a new question
        newQ.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alert.show();
            }
        });

    }

这是按下 "Add new question" 按钮时调用的函数:

public void addQuestion()
    {
        resourceApi.createQuestion(groupId, out, new Callback<Question>(){

            @Override
            public void failure(RetrofitError err) {
                // TODO Auto-generated method stub

            }

            @Override
            public void success(Question q, Response response) {
                // TODO Auto-generated method stub
//              adapter.add(q.getTitle());
                questions.add(q.getTitle());
                qId.add(q.getId());
            }

        });

        adapter.notifyDataSetChanged();
        Toast.makeText(QuestionsScreen.this, "New Question created",Toast.LENGTH_LONG).show();
    }

吐司显示。但是,列表视图不会随着新问题而更新。我也尝试使用 adapter.add() 但它只是导致了一些其他问题所以我不想使用它。 谁能提出更好的方法或指出我做错了什么?

你必须在回调中通知你数据(成功函数)

尝试这样的事情:

resourceApi.createQuestion(groupId, out, new Callback<Question>(){

        @Override
        public void failure(RetrofitError err) {
            // TODO Auto-generated method stub

        }

        @Override
        public void success(Question q, Response response) {
            // TODO Auto-generated method stub
            adapter.add(q.getTitle());
            questions.add(q.getTitle());
            qId.add(q.getId());
//Notify once you have add something in questions
            adapter.notifyDataSetChanged();
        }

});