Android将jSON数据解析为listview

时间:2012-08-21 16:25:04

标签: android json listview

好的,所以我发现本教程将mi JSON数据解析为ListView(http://www.androidhive.info/2012/01/android-json-parsing-tutorial/),但我无法得到它工作,我不知道发生了什么,我不断得到的错误是“exeption:你的内容必须有一个listview,这是'android.r.id.list'”,这是我的代码:

package com.example.tweev;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Tweev extends ListActivity {

    // url to make request
    //private static String url = "http://api.androidhive.info/contacts/ ";
    private static String url = "http://www.tweevenvivo.com/service/Tweev.getActiveShows";

    // JSON Node names
    private static final String TAG_DATA = "data";
    private static final String TAG_TWEEVERS = "tweevers";
    private static final String TAG_ID = "id";
    private static final String TAG_CATEGORY = "category";
    private static final String TAG_DESCRIPTION = "description";
    private static final String TAG_NAME = "name";
    private static final String TAG_HASHTAG = "hashtag";
    private static final String TAG_CHANNELS = "channels";  //contains "operators", "hour and "operator name"
    private static final String TAG_OPERATORS = "operators";    //contains "channel" with "code"
    private static final String TAG_OPCHANNEL = "channel";
    private static final String TAG_OPCODE = "code";
    private static final String TAG_CHHOUR = "hour";
    private static final String TAG_CHNAME = "name";
    private static final String TAG_IMAGE_SMALL = "image_small";
    private static final String TAG_IMAGE = "image";
    private static final String TAG_SHOW_ID = "show_id";

    // tweevs JSONArray
    JSONArray tweevs = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tweev);
        TextView view = (TextView) findViewById(R.id.TextView01);
        String s="";
        for (int i=0; i < 500; i++) {
            s += "TEST "+i+" ";
        }
        view.setText(s);
        getData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tweev, menu);
        return true;
    }

    private void getData() {

        // Hashmap for ListView
        ArrayList<HashMap<String, String>> tweevList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        //Log.d(TAG_DATA, "json " + json);

        try {
            // Getting Array of Contacts
            tweevs =  json.getJSONArray(TAG_DATA);

            Log.d(TAG_DATA, "tweevs " + tweevs);

            // looping through All tweevs
            for(int i = 0; i < 10; i++) {//tweevs.length()
                JSONObject c = tweevs.getJSONObject(i);

                // Storing each json item in variable
                String tweevers = c.getString(TAG_TWEEVERS);
                String id = c.getString(TAG_ID);
                String category = c.getString(TAG_CATEGORY);
                String description = c.getString(TAG_DESCRIPTION);
                String name = c.getString(TAG_NAME);
                String hashtag = c.getString(TAG_HASHTAG);
                //String channels = c.getString(TAG_CHANNELS);
                String image_small = c.getString(TAG_IMAGE_SMALL);
                String image = c.getString(TAG_IMAGE);
                String show_id = c.getString(TAG_SHOW_ID);

                // operators is again a JSON Object
                JSONArray channels = c.getJSONArray(TAG_CHANNELS);


                for(int j = 0; j < channels.length(); j++) {
                    JSONObject channel = channels.getJSONObject(j);
                    JSONArray operators = channel.getJSONArray(TAG_OPERATORS);

                    for(int k = 0; k < operators.length(); k++){
                        JSONObject operator = operators.getJSONObject(k);

                        String channelName = operator.getString(TAG_OPCHANNEL);
                        String channelCode = operator.getString(TAG_OPCODE);
                    }
                    String chhour = channel.getString(TAG_CHHOUR);
                    String chname = channel.getString(TAG_CHNAME);

                    //Log.d(TAG_DATA, "operator channel "+ k + " ::: " +channelName+" __CODE__ "+channelCode);
                    //Log.d(TAG_DATA, "channel hour "+ j + " ::: " +chhour+" --channel name-- "+chname);
                    //Log.d(TAG_DATA, "operators "+ j + " ::: " +channel+" --- "+code);
                    //Log.d(TAG_DATA, "operators "+ j + " ::: " +operators);
                }

                //Log.d(TAG_DATA, "tweev "+ i + " ::: " +channels);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_HASHTAG, hashtag);
                map.put(TAG_IMAGE_SMALL, image_small);

                // adding HashList to ArrayList
                tweevList.add(map);
            }

        } catch (JSONException e) {
            Log.d(TAG_DATA, "MOTHERFUCKER!");
            e.printStackTrace();
        }

        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(this, tweevList,
                R.layout.list_items,
                new String[] { TAG_NAME, TAG_HASHTAG, TAG_IMAGE_SMALL }, new int[] {
                        R.id.name, R.id.name, R.id.hashtag });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        // Launching new screen on Selecting Single ListItem
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                /*
                // getting values from selected ListItem
                String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
                String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
                String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(TAG_NAME, name);
                in.putExtra(TAG_EMAIL, cost);
                in.putExtra(TAG_PHONE_MOBILE, description);
                startActivity(in);
                */
            }
        });

    }


}

我的猜测是我的XML文件有问题,在他们的代码中我看到他们使用OnCreate函数的布局和创建ListAdapter时的另一个布局,我不明白为什么这个而且我没有关于这些布局的内容的线索......

1 个答案:

答案 0 :(得分:2)

您的布局XML文件应包含ID为ListView的{​​{1}}。像这样的东西

@android:id/list

上面提到的网站使用3种不同的布局 - main.xml,这是上面粘贴的代码所引用的内容。它用于主要活动。

第二个是list_item.xml。此布局定义列表视图中每个项目的显示方式。这是该布局的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <!-- Main ListView 
         Always give id value as list(@android:id/list)
    -->
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

第三个也是最后一个布局是single_list_item.xml。此布局定义了单击list_item.xml上的单个项目后列表中每个项目的显示方式。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">  
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- Name Label -->
        <TextView
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#43bd00"
            android:textSize="16sp"
            android:textStyle="bold"
            android:paddingTop="6dip"
            android:paddingBottom="2dip" />
        <!-- Description label -->
        <TextView
            android:id="@+id/email"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#acacac"
            android:paddingBottom="2dip">
        </TextView>
        <!-- Linear layout for cost and price Cost: Rs.100 -->
        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!-- Cost Label -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#5d5d5d"
            android:gravity="left"
            android:textStyle="bold"
            android:text="Mobile: " >
        </TextView>
        <!-- Price Label -->
        <TextView
            android:id="@+id/mobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#acacac" 
            android:textStyle="bold"
            android:gravity="left">
        </TextView>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>