2014-11-01 55 views
1

我的問題是我不能看到我的數據在我的活動頁面,但我可以在日誌貓看到它沒有結果出現在我的活動頁面

三月11日至1日:14:25.295:d /所有產品:(11015):{「success」:1,「scb」:[{「id」:「14」,「title」:「test1」},{「id」:「15」,「title」 title「},{」id「:」16「,」title「:」title「},{」id「:」17「,」title「:」we「},

and my php files很好,我知道,因爲我測試他們。

package com.example.studentcookbook; 

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 com.example.androidhive.AllProductsActivity; 
//import com.example.androidhive.EditProductActivity; 
//import com.example.androidhive.NewProductActivity; 
//import com.example.androidhive.R; 
//import com.example.androidhive.AllProductsActivity.LoadAllProducts; 



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

public class GetAllRecipesActivity extends ListActivity { 

    // Progress Dialog 
     private ProgressDialog pDialog; 

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

     ArrayList<HashMap<String, String>> productsList; 

     // url to get all products list 
     private static String url_all_products = "http://studentcookbook.comoj.com/android_connect/get_all_products.php"; 

     // JSON Node names 
     private static final String TAG_SUCCESS = "success"; 
     private static final String TAG_PRODUCTS = "products"; 
     private static final String TAG_SCB_ID = "id"; 
     private static final String TAG_TITLE = "title"; 

     // products JSONArray 
     JSONArray products = null; 

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

     // Hashmap for ListView 
     productsList = 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 pid = ((TextView) view.findViewById(R.id.GetAllscbid)).getText().toString(); 

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

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

    }//end onCreate method 

    // 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(GetAllRecipesActivity.this); 
      pDialog.setMessage("Loading products. Please wait..."); 
      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 Products: ", json.toString()); 

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

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

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

         // Storing each json item in variable 
         String id = c.getString(TAG_SCB_ID); 
         String name = c.getString(TAG_TITLE); 

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

         // adding each child node to HashMap key => value 
         map.put(TAG_SCB_ID, id); 
         map.put(TAG_TITLE, name); 

         // adding HashList to ArrayList 
         productsList.add(map); 
        } 
       } else { 
        // no products found 
        // Launch Add New product Activity 
        Intent i = new Intent(getApplicationContext(),AddRecipeActivity.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(GetAllRecipesActivity.this, productsList, 
          R.layout.list_recipe, new String[] { TAG_SCB_ID,TAG_TITLE}, 
          new int[] { R.id.GetAllscbid, R.id.GetAllTitle }); 
        // updating listview 
        Toast.makeText(GetAllRecipesActivity.this, "onPostExecute", Toast.LENGTH_SHORT).show(); 
        setListAdapter(adapter); 
       } 
      }); 

     } 

    } 

}//end GetAllRecipesActivity class 

這兩位是我的XML文件 list_recipe.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" > 
<TextView 
      android:id="@+id/GetAllscbid" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 
<TextView 
android:id="@+id/GetAllTitle" 
android:layout_width="269dp" 
android:layout_height="34dp" /> 

這另一個activity_get_all_recipes.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"> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

回答

1

看起來像你正在做的事情與食譜。

我想你應該表名稱更改爲自己的數據庫表名:

private static final String TAG_PRODUCTS = "products"; 
+2

感謝的人,幫助我:) – Moodi1409 2014-11-01 03:09:53

+1

歡迎您:) – sasuri 2014-11-01 03:11:11

相關問題