2013-07-15 18 views
1

我能夠檢索我的聯繫人選擇器的聯繫人列表,現在如何傳遞stringbuilder並在另一個類中顯示出來。我想到了使用意圖,但沒有奏效。 請幫忙。如何傳遞stringbuilder並在另一個類中顯示

@Override 
     public void onClick(View v) { 
      StringBuilder checkedcontacts= new StringBuilder(); 
      System.out.println("............"+ma.mCheckStates.size()); 
      for(int i = 0; i < name1.size(); i++) 
       { 
       if(ma.mCheckStates.get(i)==true) 
       { 
        checkedcontacts.append(name1.get(i).toString()); 
        checkedcontacts.append("\n"); 
       } 
       else 
       { 
        System.out.println("..Not Checked......"+name1.get(i).toString()); 
       } 

      } 

      finish(); 

      Intent i = new Intent (this,SecondActivity.class); 
     i.putExtra("str",checkedcontacts); 
     startActivity(i); 


     }  
    }); 
} 

錯誤:

Intent i = new Intent (this,SecondActivity.class); 
      i.putExtra("str",checkedcontacts); 
      startActivity(i); 

回答

2

使用StringBuilder.toString()來傳遞的StringBuilder進入意向。

Intent i = new Intent (this,SecondActivity.class); 
i.putExtra("str",checkedcontacts.toString()); 
startActivity(i); 

而你應該在上面的代碼片段後使用finish();。像

//finish() //Remove this finish 
Intent i = new Intent (this,SecondActivity.class); 
i.putExtra("str",checkedcontacts.toString()); 
startActivity(i); 
finish() 

而且在聽者不能以此作爲上下文對象。你應該使用getApplicationContext()或者ActivityName.this。像

Intent i = new Intent (YourACTIVITYName.this, SecondActivity.class); 

//OR 

Intent i = new Intent (getApplicationContext(), SecondActivity.class); 
+0

感謝。現在這條線有錯誤。 「Intent i = new Intent(this,SecondActivity.class);」它說構造函數的意圖是不確定的。我做什麼? –

+0

是的,這將是錯誤的。使用getContext或Activityname.this –

+0

錯誤。你能告訴我如何編寫getContext或Activityname嗎?我是新來的android編碼。謝謝 –

相關問題