JSON数组返回数据但不能设置为布局

时间:2015-10-05 16:28:13

标签: android json listview

我正在尝试解析来自在线资源的JSON数据。我使用调试,看到JSON数据返回到活动正常。但是,当我运行应用程序时,应该显示此数据的活动,它显示为空白。我想如何用这些数据给列表充气是有问题的。

我使用的教程是:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

请帮我解决这个问题。

以下是活动代码:

package com.plusworks.appdev.youcancook;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

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

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

public class CuisineDishes extends BaseActivity {
    private ProgressDialog pDialog;
    ListView dishList;
    ArrayList<HashMap<String,String>> cuisineDishList;
    private static String url = "http://youcancook.site90.com/amdishes.json";
    JSONArray dishes=null;

    private static final String ARRAY_DISHES="americandishes";
    private static final String ID="id";
    private static final String NAME="name";
    private static final String SHORTDESC="shortdesc";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout fl = (FrameLayout) findViewById(R.id.main_content);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.activity_cuisine_dishes, null, false);
        fl.addView(contentView);

        Intent mIntent = getIntent();
        String cuisineName = mIntent.getStringExtra("CuisineName");

        getSupportActionBar().setTitle(cuisineName + " Dishes");

        cuisineDishList=new ArrayList<>();
        dishList = (ListView) findViewById(R.id.dish_list);
        new AsyncDishModel().execute();
    }

    private class AsyncDishModel extends AsyncTask<Void,Void,Void>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog=new ProgressDialog(CuisineDishes.this);
            pDialog.setMessage("Fetching dishes...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            ServiceHandler sh=new ServiceHandler();
            String jsonStr=sh.makeServiceCall(url, ServiceHandler.GET);
            if(jsonStr!=null){
                try{
                    JSONObject jObject=new JSONObject(jsonStr);
                    dishes=jObject.getJSONArray(ARRAY_DISHES);
                    for(int i=0;i<dishes.length();i++){
                        JSONObject c=dishes.getJSONObject(i);
                        String id=c.getString(ID);
                        String name=c.getString(NAME);
                        String shortdesc=c.getString(SHORTDESC);

                        HashMap<String,String> templist=new HashMap<String,String>();

                        templist.put(ID,id);
                        templist.put(NAME,name);
                        templist.put(SHORTDESC,shortdesc);
                        cuisineDishList.add(templist);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        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(
                    CuisineDishes.this, cuisineDishList,
                    R.layout.cuisine_dish_list_item, new String[] {ID,NAME,
                    SHORTDESC}, new int[]{ R.id.mId,R.id.title_dish,
                    R.id.desc_dish});
            dishList.setAdapter(adapter);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_cuisine_dishes, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

服务处理程序与教程中给出的相同。

这是我的活动布局代码:

<RelativeLayout 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.plusworks.appdev.youcancook.CuisineDishes">

    <ListView
        android:id="@+id/dish_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></ListView>

</RelativeLayout>

以下是cuisine_dish_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="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView
        android:id="@+id/title_dish"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Title"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/desc_dish"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Short Description"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/mId"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="ID"
        android:visibility="invisible" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您必须通过使用BaseAdapter类扩展您的类来制作自定义适配器,然后将您的客户适配器设置为dishlist。

对于自定义适配器示例,您可以关注http://www.vogella.com/tutorials/AndroidListView/article.html