Pat*_*tel 1 android json listview toast
我正在研究Android应用程序,但我陷入了一个非常小的问题.
我已经创建了一个动态ListView,它从我的数据库中提取数据.但现在,如果我点击ListView中的特定行,它应该向我显示带有ID的Toast.
但我似乎无法让那个工作..
有人可以帮我一把吗?
干杯,帕特里克
这是我的代码
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;
}
Run Code Online (Sandbox Code Playgroud)
}
很抱歉迟到的回复,谢谢你的代码,但是现在它显示了与列表视图显示完全相同的东西,Tijd(时间)和Wedstrijd(匹配),但我希望在点击时看到ID列表视图项目
我的字段叫做ProgrammaID,我如何才能设法在吐司中显示那个?也许我需要先在我的JSONArray中调用它?
干杯,帕特里克
将其添加到您的代码中
@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().*/
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2083 次 |
| 最近记录: |