2012-06-21 113 views
0

我可以使用您用於在不同活動之間傳遞數據的相同過程嗎?這可以用於在活動和cursoradapter之間傳遞數據。 產生不運行時的錯誤是編譯將一個整數從一個customcursoradapter傳遞給一個活動

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

號內不能發送意向的適配器。活動創建了適配器,以便它能夠與它進行通信。通過調用的方法,傳遞參數的構造函數等

編輯:添加代碼示例

如果適配器需要調用該活動的方法,你可以做這樣的事情:

在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 
    ... 
} 

我希望你明白。您只需要一種方法讓適配器獲得對該活動的引用,以便它可以根據需要調用它的方法。

注:更好的編程風格將包括活動在適配器構造函數的參數,但因爲你沒有爲你的適配器構造張貼代碼我不想混淆你太多。

+0

你能舉一個例子,請大衛看看我是否明白答案。 – alex

+0

我添加了一個代碼示例。你解決了你的問題嗎?你需要更多的幫助嗎? –

相關問題