2

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

当用户按下添加新活动时,表单开始填写。当用户完成表单时,他/她按 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);
    }
}
4

2 回答 2

2

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

  • DatabaseActivity.onClick()应该调用startActivityForResult()而不是startActivity().

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

  • 辅助活动结束后,DatabaseActivity将返回前台,结果将由DatabaseActivity.onActivityResult().

于 2013-10-11T16:38:35.183 回答
0

I think you haven't to start new activity in WizardActivity

try this :

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();

}

and in

 DatabaseActivity

add if you want

@Override
protected void onResume (){

......
adapter.notifyDataSetChanged();
}
于 2013-10-11T16:51:29.047 回答