我是否可以使用您在不同活动之间传递数据的相同过程,这可用于在活动和cursoradapter之间传递数据。 生成的错误不是运行时编译
The constructor Intent(MyAdapterQuestion, Class<Basic_database_questionsActivity>) is undefined
Intent i = new Intent(MyAdapterQuestion.this, Basic_database_questionsActivity.class);
Bundle b = new Bundle();
// get the current value of timerStart variable and store it in timerlogic
//Log.e(LOGS, "Whatis the value of timerstart inside the intentcalls method" + aInt);
b.putInt("timerlogic", aInt);
我有一个名为MyAdapterQuestion的适配器和一个名为Basic_database_questionsActivity的活动。
我有一个在方法bindView方法
中的计数器public void bindView(View v, Context context, Cursor c) {
if(radiopos1.isChecked())
{
// i want to update my main activity
// this method increment the correct answer by one I want to get that value and //pass it back to the activity
correctAnswer();
}
}
答案 0 :(得分:2)
没有。您无法将Intent发送到适配器。 Activity创建了适配器,因此它应该能够与它通信。通过调用方法,在构造函数中传递参数等等。
编辑:添加代码示例
如果适配器需要调用Activity中的方法,则可以执行以下操作:
在MyAdapterQuestion中:
// Stores a reference to the owning activity
private Basic_database_questionsActivity activity;
// Sets the owning activity (caller should call this immediately after constructing
// the adapter)
public void setActivity(Basic_database_questionsActivity activity) {
this.activity = activity;
}
// When you want to call a method in your activity (to get or set data), you do
// something like this:
activity.setCorrectAnswer(answer);
在Basic_database_questionsActivity中:
// In the place where you create the adapter, do this:
MyAdapterQuestion adapter = new MyAdapterQuestion(parameters...);
adapter.setActivity(this); // Passes a reference of the Activity to the Adapter
public void setCorrectAnswer(int answer) {
// Here is where the adapter calls the activity back
...
}
我希望你明白这个主意。您只需要一种方法让Adapter获取对Activity的引用,以便它可以在必要时调用它上的方法。
注意:更好的编程风格是将Activity作为参数包含在适配器构造函数中,但由于您没有发布适配器构造函数的代码,所以我不想让您感到困惑太多了。