进行JSON解析时list_item无法解析或不是字段 [英] list_item cannot be resolved or is not a field while doing JSON parsing
问题描述
在通过包含某些数据的URL进行json解析时,在list_item以及id,title,url,offer和enddate上都有它.
ListAdapter adapter = new SimpleAdapter(context, jsonlist,
R.layout.list_item, new String[] { ID, TITLE,
URL, OFFER, ENDDATE }, new int[] {
R.id.id, R.id.title, R.id.url,
R.id.offer, R.id.enddate });
完整的代码是我正在做的所有主类的代码!
package com.example.googlemapandroidv2;
import java.util.ArrayList;
import java.util.List;
import com.example.googlemapandroidv2.R;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Promotions extends ListActivity {
private Context context;
private static String url = "http://expressdiner.ifeelhungry.co.uk/pushadmin/webservice/getPermotions.php";
private static final String ID = "idj";
private static final String TITLE = "titlej";
private static final String URL = "urlj";
private static final String OFFER = "offerj";
private static final String ENDDATE = "enddatej";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ProgressTask(Promotions.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private ListActivity activity;
// private List<Message> messages;
public ProgressTask(ListActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(context, jsonlist,
R.layout.list_item, new String[] { ID, TITLE,
URL, OFFER, ENDDATE }, new int[] {
R.id.id, R.id.title, R.id.url,
R.id.offer, R.id.enddate });
setListAdapter(adapter);
// select single ListView item
lv = getListView();
}
protected Boolean doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
// get JSON data from URL
JSONArray json = jParser.getJSONFromUrl(url);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String idj = c.getString(ID);
String titlej = c.getString(TITLE);
String urlj = c.getString(URL);
String offerj = c.getString(OFFER);
String enddatej =c.getString(ENDDATE);
HashMap<String, String> map = new HashMap<String, String>();
// Add child node to HashMap key & value
map.put(ID, idj);
map.put(TITLE, titlej);
map.put(URL, urlj);
map.put(OFFER, offerj);
map.put(ENDDATE, enddatej);
jsonlist.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}
}
XML代码为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.googlemapandroidv2.Promotions" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
推荐答案
list_item在执行JSON时无法解析或不是字段 解析
list_item cannot be resolved or is not a field while doing JSON parsing
可能您没有在res/layout/
中使用list_item
名称创建布局.
Probably you have not created layout in res/layout/
with list_item
name.
使用list_item.xml
在其中做什么?
what to do in it ?
SimpleAdapter
接受我们要在ListView行中显示的布局的第三个参数id
SimpleAdapter
takes third params id of layout which we want to show in ListView row
您要将R.id.id, R.id.title, R.id.url,R.id.offer,R.id.enddate
id传递给适配器以映射Map
中的数据,因此在list_item.xml
中添加所有TextView,其中您要显示与最后一个参数传递给适配器的id相同的值
You are passing R.id.id, R.id.title, R.id.url,R.id.offer,R.id.enddate
id's to adapter to map data from Map
so Add all TextViews in list_item.xml
in which you want to show values with same id's which you are passing as last parameter to adapter
这篇关于进行JSON解析时list_item无法解析或不是字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!