在执行JSON解析时,list_item无法解析或不是字段

时间:2015-02-12 06:48:47

标签: android xml json eclipse

通过包含一些数据的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>

2 个答案:

答案 0 :(得分:0)

在布局文件夹中创建list_item.xml。我声明了textview,但您可以将imageview或自定义内容放入其中。

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:paddingTop="3dp"
        android:paddingBottom="3dp"
        android:textColor="#000000"/>

</LinearLayout>

答案 1 :(得分:0)

  执行JSON时,

list_item无法解析或不是字段   解析

可能您没有使用res/layout/名称在list_item创建布局。

使用list_item.xml

创建布局
  

该怎么做?

SimpleAdapter获取我们想要在ListView行中显示的布局的第三个参数id

您正在将R.id.id, R.id.title, R.id.url,R.id.offer,R.id.enddate ID传递给适配器以映射来自Map的数据,因此请在list_item.xml中添加所有要显示您将作为最后一个参数传递的相同ID的值的TextView适配器