我是java的新手,我对解析的JSON数据有一点问题,下面你可以看到我的代码,我将JSON数据解析为ListView
,你可以在我的代码中看到,现在我想添加点击ListView
中每个项目的监听器,当用户点击他想要的项目时,应打开所选项目的URL链接,我尝试通过意图,但我不知道如何更新来自JSON的 uri 字段,提前感谢。
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email",
"mobile"}, new int[]{R.id.name,
R.id.email, R.id.mobile});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = "email";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
lv.setAdapter(adapter);
}
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "senjo",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c203",
"name": "John Wayne",
"email": "john_wayne@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
答案 0 :(得分:0)
要打开您应该尝试的网址
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
答案 1 :(得分:0)
以下是您将如何做到这一点:
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email", "mobile"},
new int[]{R.id.name, R.id.email, R.id.mobile});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = contactList.get(position).get("url");
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
lv.setAdapter(adapter);
编辑:好的,在您的编辑中,您的JSON中实际上没有url
字段。如果您要打开的url
实际上是电子邮件地址,那么您必须使用此代码:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String email = contactList.get(position).get("email");
if (email != null && !email.isEmpty()) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:" + email));
startActivity(i);
}
}
});