2017-04-11 60 views
0

設置列表視圖ID從ID屬性,我希望你能幫助我。從我在開始學習java和androd編程MySQL連接

我想設置一個ID列表視圖元素。有了這個ID我想改變我的活動。該ID應是,還站在一個MySQL數據庫的ID。

我的連接到MySQL數據庫的工作。我有數組中的所有信息,並且每個listelement顯示以下信息「名稱ID:1」。 onclick監聽器也可以工作。

但我怎麼能設置id到ListView?我怎樣才能從ArrayAdapter適配器獲取id信息?

protected void onPostExecute(String result) { 

    if(result.equals("") == true || result.length() == 0 || result.equals("") == true || result == "|"){ 
     System.out.println("Hallo Welt!"); 

     result = "In der Datenbank gibt es keine Kategorie!"; 

    } 


    // Senkrechter Strich entfernen 
    String[] arr = result.split("\\|"); 
    ArrayAdapter adapter = new ArrayAdapter (this.context, android.R.layout.simple_list_item_1, arr); 
    this.list.setAdapter(adapter); 

    //TODO Onclick Listener 
    this.list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      Toast.makeText(context, "my id to pass along the subview is " + position,Toast.LENGTH_LONG).show(); 

      Intent intent = new Intent(context, Activity.class); 
      context.startActivity(intent); 
     } 
    }); 


} 

謝謝您的幫助...

回答

0

希望這會給你一個想法

@Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      Intent intent = new Intent(getActivity(), Activity2.class); 
      intent.putExtra("ID", YourAdapter.YourItems.get(position).getId()); 
      startActivity(intent); 
     } 
+0

感謝您發表評論......但我不知道我應該設置「YourAdapter」和「YourItems」...你能解釋一下嗎? –

+0

我用一個字符串測試它,這個函數 - 但是用「YourAdapter.YourItems.get(position).getId())」不起作用。^^ –