Android:ListView OnItemClick显示Toast

时间:2012-07-22 18:00:36

标签: android json listview toast

我正在开发一款Android应用,但我确实陷入了困境。

我创建了一个动态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中调用它?

干杯, 帕特里克

1 个答案:

答案 0 :(得分: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().*/



}