2012-07-22 47 views
1

我正在開發一個Android應用程序,但我被困在一個非常小的東西。Android:ListView OnItemClick顯示Toast

我創建了一個動態ListView,它從我的數據庫中提取數據。 但是現在,如果我點擊ListView中的特定行,它應該向我顯示帶有ID的敬酒。

但我似乎無法得到該工作..

有人可以給我一個手呢?

乾杯, 帕特里克

這是我的代碼

package wvv.zondag.app; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 

public class WedstrijdenJSONParser extends ListActivity{ 

/** Called when the activity is first created. */ 
@SuppressWarnings("unchecked") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.wedstrijdenlist); 

    setListAdapter(new ArrayAdapter(
      this,android.R.layout.simple_list_item_1, 
      this.populate())); 
} 

private ArrayList<String> populate() { 
    ArrayList<String> items = new ArrayList<String>(); 

    try { 
     URL url = new URL 
     ("http://www.wvvzondag2.nl/android/fillwedstrijden.php"); 
     HttpURLConnection urlConnection = 
      (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.connect(); 
     // gets the server JSON data 
     BufferedReader bufferedReader = 
      new BufferedReader(new InputStreamReader(
        urlConnection.getInputStream())); 
     String next; 
     while ((next = bufferedReader.readLine()) != null){ 
      JSONArray ja = new JSONArray(next); 

      for (int i = 0; i < ja.length(); i++) { 
       String var = ""; 
       JSONObject jo = (JSONObject) ja.get(i); 
       var = jo.getString("Tijd"); 
       var = var+" - "+jo.getString("Wedstrijd"); 
       items.add(var); 
       } 
     } 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return items; 
} 

}


很抱歉這麼晚纔回復,感謝您的一段代碼,但現在它顯示的準確與列表視圖顯示的內容相同,Tijd(時間)和Wedstrijd(匹配),但我希望在點擊列表視圖項目時看到ID

我的領域被稱爲ProgrammaID我該如何設法讓這一個顯示在吐司? 也許我需要先在我的JSONArray中調用它?

乾杯, 帕特里克

+0

代碼的部分缺失 - 一個與麪包... – 2012-07-22 18:08:27

回答

5

添加到您的代碼

@Override 
protected void onListItemClick(ListView l, View v, int position, long id){ 
    super.onListItemClick(l, v, position, id); 
      Toast.makeText(getApplicationContext(), 
      ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); 
       /*v passed in as an argument is the view in your listitem which is clicked.If you want to get access to the adapter just use this.getListAdapter().*/ 



} 
+0

關閉。您不需要在視圖上調用getText(),因爲您已經掌握了id,這就是他想要顯示的內容。但好的回答+1 – 2012-07-22 18:22:28

+0

@FrankSposaro謝謝,我希望OP能夠自己弄清楚他自己:-) – Rasmus 2012-07-22 18:26:49