Android:ListView和McClick show Toast
我正在开发一款Android应用程序,但我被一件很小的事情困住了 我创建了一个动态列表视图,它从我的数据库中提取数据。 但是现在,如果我单击ListView中的某一行,它应该会显示一个ID为的祝酒词 但我似乎没法让它发挥作用 谁能帮我一下吗 干杯, 帕特里克 这是我的密码Android:ListView和McClick show Toast,android,json,listview,toast,Android,Json,Listview,Toast,我正在开发一款Android应用程序,但我被一件很小的事情困住了 我创建了一个动态列表视图,它从我的数据库中提取数据。 但是现在,如果我单击ListView中的某一行,它应该会显示一个ID为的祝酒词 但我似乎没法让它发挥作用 谁能帮我一下吗 干杯, 帕特里克 这是我的密码 package wvv.zondag.app; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea
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;
}
}
很抱歉延迟回复,感谢您提供的代码,但是现在它显示的内容与列表视图显示的内容完全相同,TijdTime和WedstrijdMatch,但是我希望在单击listview项目时看到ID
我的领域叫ProgrammaID,我怎么能让它出现在祝酒辞中呢?
也许我需要先在我的JSONArray中调用它
干杯,
Patrick将此添加到您的代码中
@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().*/
}
将此添加到您的代码中
@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().*/
}
代码的一部分丢失了-带吐司的一部分…代码的一部分丢失了-带吐司的一部分…关闭。您不需要在视图上调用getText,因为您已经获得了id的句柄,而这正是他想要显示的。但答案很好+1@FrankSposaro谢谢,我希望OP现在能自己解决这个问题。您不需要在视图上调用getText,因为您已经获得了id的句柄,而这正是他想要显示的。但答案很好+1@FrankSposaro谢谢,我希望OP现在能自己弄明白:-