2016-01-15 83 views
0

我想要檢索的項目的的objectID(具體而言,這是一個名爲「房間」,在我們的解析模型類)一ListView內點擊。 而在這之後,我想檢索對象ID傳遞到使用Intent另一個類。檢索解析對象ID在OnTimeClickListener方法

我試圖解析文檔getObjectId();方法,但似乎不起作用。 我應該如何檢索它?

這是我的代碼。

Button createBtn; 
Button searchBtn; 
Button myGroupBtn; 
Button settingBtn; 


String[] courses; 
List<String> listItems; 
ArrayAdapter<String> adapter; 
ListView listView; 
TextView textView; 
// when CREAT button is tapped 
public void createBtn(View view){ 
    Intent i = new Intent(getApplicationContext(), Create.class); 
    // Removes animation 
    i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
    startActivity(i); 
} 
// when Setting button is tapped 
public void settingBtn(View view) { 
    Intent i = new Intent(getApplicationContext(), Setting.class); 
    // Removes animation 
    i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
    startActivity(i); 
} 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    Intent intent = getIntent(); 
    String courseName = intent.getStringExtra("courseName"); 
    String courseNumber = intent.getStringExtra("courseNumber"); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search_result); 
    // Making Links to Buttons on Create 
    createBtn = (Button) findViewById(R.id.createBtn); 
    searchBtn = (Button) findViewById(R.id.searchBtn); 
    myGroupBtn = (Button) findViewById(R.id.myGroupBtn); 
    settingBtn = (Button) findViewById(R.id.settingBtn); 


    //Chaning the button colors 
    searchBtn.setTextColor(0xFFFFFFFF); 
    createBtn.setTextColor(0xFFBFBFBF); 
    myGroupBtn.setTextColor(0xFFBFBFBF); 
    settingBtn.setTextColor(0xFFBFBFBF); 

    listView= (ListView)findViewById(R.id.listView); 
    textView= (TextView)findViewById(R.id.textView2); 
    textView.setText(courseName + " " + courseNumber); 

    listItems = new ArrayList<>(); 


    ParseQuery<ParseObject> roomQuery = ParseQuery.getQuery("Room"); 
    roomQuery.whereEqualTo("course" , courseName); 
    roomQuery.whereEqualTo("number" , courseNumber); 

    roomQuery.findInBackground(new FindCallback<ParseObject>() { 
     @Override 
     public void done(List<ParseObject> objects, ParseException e) { 

      if (e == null) { 
       for (ParseObject room : objects) { 

        Log.i("Appinfo", String.valueOf(room.get("title"))); 
        String stringToAdd = ""; 

        String opened = String.valueOf(room.get("opened")); 
        String x; 
        if(opened.equals(true)){ 
         x = "Open"; 
        }else{ 
         x = "Closed"; 
        } 

        stringToAdd = stringToAdd + String.valueOf(room.get("studyDate")) + " " + 
          String.valueOf(room.get("category")) + " " + x + "\n" 
          + String.valueOf(room.get("title")) + 
          "   " 
        ; 
        listItems.add(stringToAdd); 
        Log.i("Appinfo", "A"); 
       } 


      } else { 
       Log.i("Appinfo", "B"); 
       e.printStackTrace(); 
      } 

     } 
    }); 
    initList(); 

} 
public void initList() { 

    Log.i("Appinfo", "C"); 



    adapter = new ArrayAdapter<String>(this, R.layout.list_two, R.id.txtvw, listItems); 
    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(this); 


} 


@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Intent intent = new Intent(getApplicationContext(), Room.class); 
    String category = listItems.get(position); 
} 

}

回答

0

你的問題是,你失去解析的objectId在 「Adapter」:您使用Strings和基本的Android Adapter(有:new ArrayAdapter<String>(this, R.layout.list_two, R.id.txtvw, listItems);),以顯示你的元素;問題是,你的轉換複雜的Room對象爲String不包含大量的信息會讓你失去了objectId你想保留。

解決你的問題是一個叫做Adapter或更專門爲這種情況下Custom Adapter非常的Android十歲上下的格局,這將在設備解釋如何使您的Room元素融入到你的應用程序的ListView的每一個細胞。

這是非常有據可查的(更好,我可以寫在這篇文章中的所有東西)所以here是通用官方文檔,在這裏,對我來說,the best tutorial來獲得這個概念。

PS:對於初學者,我推薦Base Adapter,它對我來說是最簡單的:)