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) が表示されていますが、リストビュー項目

私のフィールドは 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 に答える