0

任何人都可以在这里帮忙。我正在尝试从 json 响应中填充列表视图。响应正在发布,由于某种原因它没有显示列表?有什么帮助它也在列表视图上停止,只是什么也没看到。

extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;

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

ArrayList<HashMap<String, String>> ideasList;

// url to get all products list
private static String url_all_products = "http://theideavault.no-ip.org//android_connect/get_all_products.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "ideas";
private static final String TAG_IID = "iid";
private static final String TAG_NAME = "idea";

// products JSONArray
JSONArray ideas = null;

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

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

    // Loading products in Background Thread
    new LoadAllProducts().execute();

    // Get listview
    ListView lv = getListView();

    // on seleting single product
    // launching Edit Product Screen
    lv.setOnItemClickListener(new OnItemClickListener() {

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

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    EditIdeaActivity.class);
            // sending pid to next activity
            in.putExtra(TAG_IID, iid);

            // starting new activity and expecting some response back
            startActivityForResult(in, 100);
        }
    });

}

// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) {
        // if result code 100 is received 
        // means user edited/deleted product
        // reload this screen again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllProducts extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AllIdeasActivity.this);
        pDialog.setMessage("Reteiving Ideas from database. Wooooooo...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("All Ideas: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                ideas = json.getJSONArray(TAG_PRODUCTS);

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

                    // Storing each json item in variable
                    String iid = c.getString(TAG_IID);
                    String idea = c.getString(TAG_NAME);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_IID, iid);
                    map.put(TAG_NAME, idea);

                    // adding HashList to ArrayList
                    ideasList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),
                        NewIdeaActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllIdeasActivity.this, ideasList,
                        R.layout.list_idea, new String[] { TAG_IID,
                                TAG_NAME},
                        new int[] { R.id.iid, R.id.idea });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

}}
4

1 回答 1

0

您犯的最大错误是在初始化 TAG_PRODUCTS 变量时。

错误:

私有静态最终字符串 TAG_PRODUCTS = "ideas";

正确值:

私有静态最终字符串 TAG_PRODUCTS = "产品";

顺便说一句,您是在获取想法或产品吗?因为您的回复是关于产品的,而您的代码描述了想法 :)

于 2013-05-23T04:45:27.607 回答