如何仅使用JSON获取第一级和第二级列表

时间:2013-11-07 06:30:05

标签: java android json

我使用JSON将数据导入ListView,但对于第一个Level List,我使用静态代码,简而言之,第一级列表没有使用JSON,请参阅下面的代码,用于显示第一级List。

类别屏幕 (以静态方式显示数据)

enter image description here

产品屏幕 (使用JSON获取数据)

enter image description here

CategoryActivity.java:

public class CategoriesActivity extends Activity implements OnItemClickListener {

ListView lview3;
ListViewCustomAdapter adapter;

private ArrayList<Object> itemList;
private ItemBean bean;

ImageButton checkOut;
ImageButton back;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menus);    

    prepareArrayLits();
    lview3 = (ListView) findViewById(R.id.listView1);
    adapter = new ListViewCustomAdapter(this, itemList);
    lview3.setAdapter(adapter);
    lview3.setOnItemClickListener(this);  

}
private static final int Sony = 0;
private static final int Panasonic = 1;

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {

         // Set up different intents based on the item clicked: 
         switch (position)
         {
             case Sony:
                 Intent intent1 = new Intent(CategoriesActivity.this, ProductsActivity.class);
                 intent1.putExtra("category", "Sony");
                    startActivity(intent1); 
                 break;
             case Panasonic:
                 Intent intent2 = new Intent(CategoriesActivity.this, ProductsActivity.class);
                 intent2.putExtra("category", "Panasonic");
                    startActivity(intent2); 
                 break;

             default:
                 break;
         }

     }

public void prepareArrayLits()
{
    itemList = new ArrayList<Object>();

    AddObjectToList( "Sony" );
    AddObjectToList( "Panasonic" );

}

// Add one item into the Array List
public void AddObjectToList(String title)
{
    bean = new ItemBean();
    bean.setTitle(title);
    itemList.add(bean);
}

}

ItemBean.java:

public class ItemBean 
{
    String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

ListViewCustomAdapter.java:

public class ListViewCustomAdapter extends BaseAdapter{

    ArrayList<Object> itemList;

    public Activity context;
    public LayoutInflater inflater;

    public ListViewCustomAdapter(Activity context,ArrayList<Object> itemList) {
        super();

        this.context = context;
        this.itemList = itemList;     
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return itemList.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return itemList.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        TextView txtViewTitle;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.listrow_categories, null);
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();

        ItemBean bean = (ItemBean) itemList.get(position);
        holder.txtViewTitle.setText(bean.getTitle());

        return convertView;
    }

}

并使用以下代码将数据输入二级列表(即:产品列表)

ProductsActivity.java: -

public class ProductsActivity extends Activity {


    static String URL = "http://10.0.2.2/android/test.json";

    static String KEY_CATEGORY = "item";
    static final String KEY_TITLE = "ProductName";

    ListView list;
    LazyAdapter adapter;



    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menus);

        final ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>();
        list = (ListView) findViewById(R.id.listView1);
        adapter = new LazyAdapter(this, itemsList);
        list.setAdapter(adapter);

        if (isNetworkAvailable()) {
            new MyAsyncTask().execute();
        } else {

            AlertDialog alertDialog = new AlertDialog.Builder(ProductsActivity.this).create();
            alertDialog.setMessage("The Internet connection appears to be offline.");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            alertDialog.show();
        }
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        return (info != null);
    }

    class MyAsyncTask extends
            AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
        private ProgressDialog progressDialog = new ProgressDialog(
                ProductsActivity.this);

        @Override
        protected void onPreExecute() {
            progressDialog.setMessage("Loading, Please wait.....");
            progressDialog.show();
        }

        final ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>();

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(
                String... params) {
            HttpClient client = new DefaultHttpClient();
            // Perform a GET request for a JSON list
            HttpUriRequest request = new HttpGet(URL);
            // Get the response that sends back
            HttpResponse response = null;
            try {
                response = client.execute(request);
            } catch (ClientProtocolException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Convert this response into a readable string
            String jsonString = null;
            try {
                jsonString = StreamUtils.convertToString(response.getEntity()
                        .getContent());
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Create a JSON object that we can use from the String
            JSONObject json = null;
            try {
                json = new JSONObject(jsonString);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {

                JSONArray jsonArray = json.getJSONArray(KEY_CATEGORY);

                for (int i = 0; i < jsonArray.length(); i++) {

                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    map.put("KEY", String.valueOf(i));
                    map.put(KEY_TITLE, jsonObject.getString(KEY_TITLE));

                    itemsList.add(map);

                }
                return itemsList;
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
            return null;
        }


        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            list = (ListView) findViewById(R.id.listView1);
            adapter = new LazyAdapter(ProductsActivity.this, itemsList);
            list.setAdapter(adapter);

            this.progressDialog.dismiss();
            list.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                }

            });
        }
    }
}

LazyAdapter.java:

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.listrow_products, null);

        TextView title = (TextView)vi.findViewById(R.id.title); 

        HashMap<String, String> item = new HashMap<String, String>();
        item = data.get(position);

        // Setting all values in listview
        title.setText(item.get(ProductsActivity.KEY_TITLE));

        return vi;
    }
}

StreamUtils.java: -

public class StreamUtils {

    public static String convertToString(InputStream inputStream) throws IOException {
        if (inputStream != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 1024);
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                inputStream.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }
}

问题:

如何使用JSON获得第一级列表,而不是像我在现有程序中编写的那样编写静态代码?

test.json:

{
    "Sony": [{
        "ProductID": "1",
        "ProductName": "Sony - LED TV"
    }, {
        "ProductID": "2",
        "ProductName": "Sony - Laptop"
    }],

    "Panasonic": [{
        "ProductID": "1",
        "ProductName": "Panasonic - LED TV"
    }, {
        "ProductID": "2",
        "ProductName": "Panasonic - Laptop"
    }]
}

3 个答案:

答案 0 :(得分:0)

您可以使用json解析编写用于生成第一级列表的asynctask。现在,在您的活动中,您可以拥有一个数据结构,如map,arraylist等,用于保存第一级列表。在应用程序的onCreate中,您可以调用asynctask来执行json解析,可以填充本地数据结构,然后可以使用它来显示第一级列表。

或者

您可以通过更简单的方式重新设计应用。使用适配器保留单个列表视图。点击列表中的任何项目,您可以更改列表视图的数据集。

这样的事情:

public void onItemClick(AdapterView父视图,View视图,                     int position,long id){

if(isLevel1Column1){    mAdapter_1.setListData(mSubMenu1);    isLevel1Column1 = false;    mAdapter_1.notifyDataSetChanged();

我之前已经取得了类似的成就。在我的情况下,我也使用了延迟加载代码:)

答案 1 :(得分:0)

您似乎从Web服务获取了json。更好的方法是以一种给出类别名称和数组的方式实际修改你的json响应。目前你的json结构的方式可能不正确。现在,你只有两种产品,即索尼和松下,但可能会增加。因此,最好以一种方式修改你的json,它可能以下面提到的方式给出类别名称及其列表:

[
    {
         "categoryName" : "Sony",
         "categoryList" : [{"ProductID": "1",
                            "ProductName": "Sony - LED TV"},
                           {"ProductID": "2",
                            "ProductName": "Sony - Laptop"}]
   },

   {
         "categoryName" : "Panasonic",
         "categoryList" : [{"ProductID": "1",
                            "ProductName": "Panasonic - LED TV"},
                           {"ProductID": "2",
                            "ProductName": "Panasonic - Laptop"}]
   }    
]

通过这种方式,您可以动态获取名称以及列表。希望这会帮助你。

如果您只想用静态字符串填充listview,那么您可以创建一个字符串数组并使用下面提到的代码:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,    android.R.layout.simple_list_item_1, android.R.id.text1, values);
    listView.setAdapter(adapter);

答案 2 :(得分:0)

    As per my understanding you require ExpandableListView which contains Parent at its first view .. When you click on any parent you get list of child under it.

   Dynamically populate parent and child to ExpandableListView using key name for parent and its object as child from JSON... ( Just change api from which you need to access data ).

    Your main class where you require to show all stuffs 
    ------------------------------------------------------

    public class ViewProfileService extends Activity{

        /** Define Async Task variables over here ..vvvv*/
        HttpClient httpclient;
        HttpGet httpget;
        HttpResponse httpresponse;
        HttpResponse hhttpresponse;
        JSONObject myJsonObject = null;
        JSONArray myJsonArray = null;
        String myJsonString = "";

        JSONObject nmyJsonObject = null;
        JSONArray nmyJsonArray = null;
        String nmyJsonString = "";
        String service;
        String name;
        ServiceList sl;
        String[] head;

        //private ProgressDialog pDialog;
    //  private ArrayList<ServiceList> m_ArrayList = null;
        ///////////////////////////////////////////////////ArrayList<String[]> header = new ArrayList<String[]>();

        /** Expandable list credentials */
        ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        HashMap<String, List<String>> listDataChild;
         String bo_uid;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.view_profile_service);

            SharedPreferences sp = getSharedPreferences("all_id", 0);
            bo_uid = sp.getString("bo_uid", "");

            new BussinessOwnerHttpAsyncTask().execute();

            //setGroupIndicatorToRight();

             /*// Create a Drawable object with states
            Drawable icon =
        this.getResources().getDrawable(R.drawable.expander_group);

            // Set the newly created Drawable object as group indicator.
            // Now you should be seeing your icons as group indicators.
           getExpandableListView().setGroupIndicator(icon);*/
        }

        class BussinessOwnerHttpAsyncTask extends AsyncTask<String, Void, String> {

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();

                /*pDialog = new ProgressDialog(getParent());
                pDialog.setMessage("Please wait ...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();*/
            }


            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub

                StaticVars sv = new StaticVars();
                // Starting Async Task work over here ...
                HttpClient httpclient = new DefaultHttpClient();
                @SuppressWarnings("static-access")
                String myUrl = "your url " ;//set you url over here

                myUrl = myUrl.replaceAll("\n", "");
                myUrl = myUrl.replaceAll(" ", "%20");
                Log.d("checkurl", myUrl);
                HttpGet httpget = new HttpGet(myUrl.trim());

                try {
                    httpresponse = httpclient.execute(httpget);
                    System.out.println("httpresponse" + httpresponse);
                    Log.i("response", "Response" + httpresponse);
                    InputStream is = httpresponse.getEntity().getContent();
                    InputStreamReader isr = new InputStreamReader(is);
                    BufferedReader br = new BufferedReader(isr);
                    StringBuilder sb = new StringBuilder();
                    String recievingDataFromServer = null;
                    while ((recievingDataFromServer = br.readLine()) != null) {
                        Log.i("CHECK WHILE", "CHECK WHILE");
                        sb.append(recievingDataFromServer);
                    }

                    myJsonString = sb.toString();

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return null;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
            //  pDialog.dismiss();

                if (myJsonString.length() > 0) {
                    try {
                        myJsonObject = new JSONObject(myJsonString);
                        JSONObject object = myJsonObject.getJSONObject("services");

                        Iterator<String> iterate = object.keys();
                        //m_ArrayList = new ArrayList<ServiceList>();

                        listDataHeader = new ArrayList<String>();
                        listDataChild = new HashMap<String, List<String>>();
                        List<String> toptodown = null;

                        while(iterate.hasNext()){
                             service = iterate.next();
                            Log.d("goging", service);
                            listDataHeader.add(service);
                            myJsonArray = object.getJSONArray(service);
                            toptodown = new ArrayList<String>();
                            for (int i = 0; i < myJsonArray.length(); i++) {
                                  JSONObject okay = myJsonArray.getJSONObject(i);
                                  name = okay.getString("name");
                                  Log.d("goging","Name is: " + name);
                                  toptodown.add(name);
                                  }

                            listDataChild.put(service, toptodown);

                        }


                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                }
                start();

            }

        }



        public void start() {
            // TODO Auto-generated method stub
            expListView = (ExpandableListView) findViewById(R.id.lvExp);

            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

            // setting list adapter
            expListView.setAdapter(listAdapter);

        }

    }

    your view_profile_service.xml file used in ViewProfileService.java class
    -----------------------------------------------------------------------

    <?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" >

        <ExpandableListView
                    android:id="@+id/lvExp"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"/>  

    </LinearLayout>


    your ExpandableListAdapter.java class ..
    -------------------------------------

    import java.util.HashMap;
    import java.util.List;

    import com.app.ruzanamah.R;

    import android.content.Context;
    import android.graphics.Typeface;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.TextView;

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private Context _context;
        private List<String> _listDataHeader; // header titles
        // child data in format of header title, child title
        private HashMap<String, List<String>> _listDataChild;

        public ExpandableListAdapter(Context context, List<String> listDataHeader,
                HashMap<String, List<String>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
        }

        @Override
        public Object getChild(int groupPosition, int childPosititon) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .get(childPosititon);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getChildView(int groupPosition, final int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {

            final String childText = (String) getChild(groupPosition, childPosition);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item, null);
            }

            TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.lblListItem);

            txtListChild.setText(childText);
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return this._listDataHeader.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return this._listDataHeader.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }

            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }


    your list_item.xml file used in ExpandableListAdapter.java class
    ---------------------------------------------------------------

    <?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="17dip"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

    </LinearLayout>

    your list_group.xml file used in ExpandableListAdapter.java class
    -------------------------------------------------------------------

    <?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="vertical"
        android:padding="8dp"
        >
     <!-- android:background="#000000" -->

        <TextView
            android:id="@+id/lblListHeader"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
            android:textSize="17dp"/>

    </LinearLayout>

    Above mentioned contains all you need to perform your stuff very efficiently..

    Enjoy .. !