1

我正在开发一个 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(Time) 和 Wedstrijd(Match) 但我想在单击时查看 ID列表视图项

我的字段名为 ProgrammaID 我怎样才能设法让那个显示在吐司中?也许我需要先在我的 JSONArray 中调用它?

干杯,帕特里克

4

1 回答 1

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().*/



}
于 2012-07-22T18:11:18.613 回答