2014-10-30 18 views
0

我是多人編程中的新手。如何將String設置爲hashmap值?我想從RoomListActivity調用hashmap屬性,並在QuizMaintain活動上設置它的值,並且我想將QuizMaintain類的hashmap值設置爲textview。下面是我的示例代碼Appwarp Android編程從房間列表中獲取屬性

RoomListActivity

public void onJoinNewRoomClicked(View view){ 
    progressDialog = ProgressDialog.show(this,"","Please wait..."); 
    progressDialog.setCancelable(true); 

    HashMap<String, Object> properties = new HashMap<String, Object>(); 
    properties.put("timer", ""); 
    properties.put("question", ""); 
    properties.put("answer", ""); 
    properties.put("foulanswer", ""); 


    theClient.createRoom(""+System.currentTimeMillis(), "Yoshua", 2, properties); 
} 

然後我想設置它從QuizMaintain活動

public class QuizMaintain extends Activity implements RoomRequestListener, NotifyListener { 

private WarpClient theClient; 
private HashMap<String, Object> properties; 
private TextView txttimer,txtquestion; 
private String roomId = ""; 
private HashMap<String, User> userMap = new HashMap<String, User>(); 
String string="5x5#5x4#150:3#500:20#536+59"; 
String[] questions = string.split("#"); 
String question1 = questions[0]; 
String question2 = questions[1]; 
String question3 = questions[2]; 
String question4 = questions[3]; 
String question5 = questions[4]; 


@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_quiz_maintain); 
     txttimer = (TextView)findViewById(R.id.timer); 
     txtquestion = (TextView)findViewById(R.id.questionview); 


     try{ 
      theClient = WarpClient.getInstance(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

     theClient.getLiveRoomInfo("143680827"); 
     Intent intent = getIntent(); 
     roomId = intent.getStringExtra("roomId"); 
     init(roomId); 

     //setquestionview(); 






} 



private void init(String roomId){ 
    if(theClient!=null){ 
     theClient.addRoomRequestListener(this); 
     theClient.addNotificationListener(this); 
     theClient.joinRoom(roomId); 
    } 
} 



@Override 
public void onGetLiveRoomInfoDone(LiveRoomInfoEvent event) { 
    properties = event.getProperties(); 
    properties.put("question", question1); 

} 

價值我想設置的HashMap值,其中的關鍵是「問題」。而我想要設置的值來自拆分字符串。當我詢問他們的支持團隊,如果我想獲得房間屬性,我應該調用getLiveRoomInfo方法並傳遞roomID作爲參數。這裏有點困惑。謝謝。

但似乎我的問題還沒有解決。在調用方法updateRoomProperties之後,我在這裏得到了另一個錯誤。這就是說WarpClient.AddZoneRequestListener(this)返回空指針異常

回答

0

當你創建一個房間時,你傳遞一個散列表。這個HashMap作爲JSON文檔存儲在服務器的房間內。 AppWarp將其稱爲房間屬性。

現在要檢索這些屬性,您必須調用getLiveRoomInfo方法。這將爲您呈現房間的屬性。在這裏,您再次添加/更改某個鍵值。但是你沒有告訴服務器你正在更新這些房間屬性。因此,您的更改仍然是局部的,並且僅限於功能範圍。

因此,當您調用getLiveRoomInfo方法時,由於您沒有在服務器上更新它們,您將看不到更改。要在服務器上更新,您需要調用updateRoomProperties方法。在這種方法中,你可以添加或改變你的hashmap。