onActivityResult()和notifyDataSetChanged()都不起作用

时间:2013-10-11 16:28:54

标签: android

我在同一个问题上阅读了很多答案,但我仍然不明白为什么我的代码无法正常工作。我(我认为)在两个活动之间交换数据时遇到问题。 我有2个活动 - 第一个包含ListView和一个Add按钮。

当用户按下添加新活动时,将从要填写的表单开始。当用户完成表单时,他/她按下OK并且我的第一个活动再次启动(确实如此)并且它应该包含新项目(但它不包含)。

这是我的第一个活动:

public class DatabaseActivity extends Activity implements OnClickListener {

    ArrayList<Student> students;
    StudentDatabaseAdapter adapter;
    ListView lvStudentList; 

    ImageButton imgBtnAdd;


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

        students = new ArrayList<Student>();
        fillArrayList();

        adapter = new StudentDatabaseAdapter(this, students);
        lvStudentList = (ListView)findViewById(R.id.lvStudentsList);
        lvStudentList.setAdapter(adapter);

        imgBtnAdd = (ImageButton)findViewById(R.id.imagBtnAddStudent);

        imgBtnAdd.setOnClickListener(this);

    }


    public void fillArrayList() {
        //code here     
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        intent = new Intent(this, WizardActivity.class);
        startActivity(intent);
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if(resultCode==RESULT_OK)
        {
            if(requestCode==2)
            {
                if (intent == null) {return;}
                Student newStudent = new Student(intent.getStringExtra("name"), intent.getStringExtra("surname"),
                        intent.getStringExtra("last_name"), Integer.parseInt(intent.getStringExtra("year_of_birth")), R.drawable.default_ava); 
                addStudent(newStudent);
                adapter.notifyDataSetChanged();
            }
        }
    }
}

这是我的第二项活动:

public class WizardActivity extends Activity implements OnClickListener {

    EditText etName, etSurname, etLastName, etYearOfBirth;

    ImageButton imgBtnOK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.wizard);    

        etName = (EditText)findViewById(R.id.etName);
        //initializing other edit texts

        imgBtnOK = (ImageButton)findViewById(R.id.imgBtnOK);

        imgBtnOK.setOnClickListener(this);          
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, DatabaseActivity.class);

        intent.putExtra("name", etName.getText().toString());
        intent.putExtra("surname", etSurname.getText().toString());
        intent.putExtra("last_name", etLastName.getText().toString());
        intent.putExtra("year_of_birth", etYearOfBirth.getText().toString());
        setResult(RESULT_OK, intent);


        startActivityForResult(intent, 2);
    }
}

2 个答案:

答案 0 :(得分:2)

notifyDataSetChanged()没有任何问题。您似乎错过了辅助活动将结果传回其调用者活动的方式。

  • DatabaseActivity.onClick()应致电startActivityForResult()而不是startActivity()

  • WizardActivity.onClick()应该在finish()之后拨打setResult()(删除startActivityForResult()电话,那里没有意义)。另请注意,您向setResult()提供的意图可能是空意图,即Intent intent = new Intent();

  • 辅助活动完成后,DatabaseActivity将返回前台,结果将由DatabaseActivity.onActivityResult()处理。

答案 1 :(得分:0)

我认为您不必在WizardActivity中启动新活动

试试这个:

WizardActivity


    ......
    @Override
public void onClick(View v) {
    Intent intent = new Intent(this, DatabaseActivity.class);

        intent.putExtra("name", etName.getText().toString());
        intent.putExtra("surname", etSurname.getText().toString());
        intent.putExtra("last_name", etLastName.getText().toString());
        intent.putExtra("year_of_birth", etYearOfBirth.getText().toString());
        setResult(RESULT_OK, intent);


    finish();

}

并在

 DatabaseActivity

add if you want

@Override
protected void onResume (){

......
adapter.notifyDataSetChanged();
}