0

我在 android 中有一个应用程序,它可以从远程服务器获取数据并将其显示在 ListFragment 上。它成功地获取了我的 LogCat 上显示的数据,但它没有显示在 ListFragment 所在的 Activity 本身上。它只是显示加载循环,无论我等待多长时间,它仍然会做同样的事情。我正在使用 httprequest 获取数据并使用 AsyncTask 来获取它们。

这是主要活动代码:

package com.thesis.menubook;


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

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;


public class MenuMain extends ListActivity {
    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();
    private ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_main);

        // Populate Category List from database
        new GetCategories().execute();

        //set list on click listener for category
            // on category list click update fragment using dbase query

        // set swipe listener
            // on swipe update fragment using dbase query

        // set add button listener
            // on click add to order list list adapter

        // set order list listener
            // on click remove from list

        // set send button listener
            // update remote database using order list
    }

    class GetCategories extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
             super.onPreExecute();
             pDialog = new ProgressDialog(MenuMain.this);
             pDialog.setMessage("Loading Categories. Please wait...");
             pDialog.setIndeterminate(false);
             pDialog.setCancelable(false);
             pDialog.show();
        }

        /**
         * Getting product details in background thread
         * */
        protected String doInBackground(String... param) {
            Bundle b = getIntent().getExtras();
            String ipaddress = b.getString("IPAddress");

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            Log.d("IP ADDRESS", ipaddress +" ");

            JSONObject json = jsonParser.makeHttpRequest("http://"+ipaddress+"/MenuBook/selectCategories.php", "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Categories: ", json.toString() + " ");
            int num = 1;
            try {
                // Checking for SUCCESS TAG
                int success = json.getInt("success");

                if (success == 1) {
                    // products found
                    // Getting Array of Products

                    JSONArray category_list = json.getJSONArray("category_list");

                    // looping through All Products
                    for (int j = 1; j < category_list.length(); j++) {
                        JSONObject c = category_list.getJSONObject(j);

                        // Storing each json item in variable
                        String category = c.getString("category");

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

                        // adding each child node to HashMap key => value
                        map.put("category", category);

                        Log.d("category #"+num+"", category + " ");
                        num = num + 1;
                        // adding HashList to ArrayList
                        if(categoryList.contains(map) != true)
                        {
                            categoryList.add(map);
                        }
                        Log.d("Category List", categoryList.toString() + " ");
                    }
                } 
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        @Override
        protected void onPostExecute(String result) {
            pDialog.dismiss();

            ArrayAdapter<ArrayList<HashMap<String, String>>> arrayAdapter = new ArrayAdapter<ArrayList<HashMap<String, String>>>(MenuMain.this, R.layout.activity_menu_category);
            arrayAdapter.add(categoryList);

            if(arrayAdapter.isEmpty() == true)
            {
                setListAdapter(null);
            }
            else
            {
                setListAdapter(arrayAdapter);
            }
        }

    }



}

这是 ListFragments 代码:

package com.thesis.menubook;

import android.annotation.TargetApi;
import android.app.ListFragment;
import android.os.Build;
import android.os.Bundle;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MenuCategory extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedState) {
        super.onActivityCreated(savedState);

    }



}

这是 ListFragments XML:

<?xml version="1.0" encoding="utf-8"?>
<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

如果有帮助,这里是 LogCat :

02-13 22:57:35.422: V/TLINE(1349): new: android.text.TextLine@4066c0c0
02-13 22:57:36.702: V/TLINE(1349): new: android.text.TextLine@40674c00
02-13 22:57:59.142: D/dalvikvm(1349): GC_FOR_ALLOC freed 161K, 5% free 6481K/6791K, paused 162ms
02-13 22:57:59.184: I/dalvikvm-heap(1349): Grow heap (frag case) to 6.926MB for 513744-byte allocation
02-13 22:57:59.562: D/dalvikvm(1349): GC_CONCURRENT freed 13K, 5% free 6969K/7303K, paused 50ms+26ms
02-13 22:58:02.334: D/NetWork Info(1349): NetworkInfo: type: mobile[UMTS], state: CONNECTED/CONNECTED, reason: simLoaded, extra: internet, roaming: false, failover: false, isAvailable: true
02-13 22:58:08.841: D/Response(1349): org.apache.http.message.BasicHttpResponse@406867f8--response from apache
02-13 22:58:08.841: D/IP Address(1349): 192.168.10.149:80
02-13 22:58:09.181: D/NEXT VALUE --in Async on postexecute(1349): true
02-13 22:58:18.232: D/dalvikvm(1349): GC_CONCURRENT freed 139K, 4% free 7258K/7559K, paused 40ms+13ms
02-13 22:58:18.802: D/URL(1349): http://192.168.10.149:80/MenuBook/checkTable.php
02-13 22:58:23.082: D/Check Table(1349): {"success":1,"tabledb":[{"table_location":"UP","table_seat":"5","table_status":"AVAILABLE","table_ID":"001"}]}
02-13 22:58:23.082: D/Table Status in doinbg(1349): AVAILABLE
02-13 22:58:23.192: D/Table Status in post execute(1349): AVAILABLE
02-13 22:58:25.622: D/IP ADDRESS(1349): 192.168.10.149:80 
02-13 22:58:28.432: D/dalvikvm(1349): GC_CONCURRENT freed 353K, 7% free 7356K/7879K, paused 14ms+264ms
02-13 22:58:32.556: D/All Categories:(1349): {"success":1,"category_list":[{"category":"MAIN DISH"},{"category":"MAIN DISH"},{"category":"DESSERT"}]} 
02-13 22:58:33.273: D/Category List JSON Array(1349): [{"category":"MAIN DISH"},{"category":"MAIN DISH"},{"category":"DESSERT"}]
02-13 22:58:33.852: D/category #1(1349): MAIN DISH 
02-13 22:58:34.982: D/Category List(1349): [{category=MAIN DISH}] 
02-13 22:58:35.164: D/category #2(1349): DESSERT 
02-13 22:58:35.382: D/Category List(1349): [{category=MAIN DISH}, {category=DESSERT}] 
02-13 22:58:35.664: D/Category List(1349): [{category=MAIN DISH}, {category=DESSERT}]
4

0 回答 0