2013-05-23 3 views
0

누구든지 도와 드릴 수 있습니까? json 응답에서 listview를 채우려고합니다. 응답이 게시 중이므로 어떤 이유로 목록이 표시되지 않습니까? 아무것도 도움이 또한 그냥 아무것도 볼 수없는 목록보기에서 중지됩니다.목록보기가 채워지지 않습니다.

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); 
      } 
     }); 

    } 

}} 
+0

오류가 있습니까? 이 메서드는 항상 UI 스레드에서 실행되기 때문에 onPostExecute에서 runOnUiThread를 사용할 필요가 없습니다. –

답변

0

큰 실수는 TAG_PRODUCTS 변수를 초기화하는 것입니다.

실수 :

개인 정적 최종 문자열 TAG_PRODUCTS = "아이디어";

올바른 값 :

개인 정적 최종 문자열 TAG_PRODUCTS = "제품";

그런데 아이디어 나 제품을 가져 왔습니까? 귀하의 응답은 제품에 대한 것이고 코드는 아이디어를 설명하기 때문에 :

+0

ok 사실은 내 목적을 위해 일부 코드를 변환하는 것입니다. 예, 나는 잘못된 것을 지적하고있었습니다. 그걸 알아 냈어. 이제는 바로 json을 돌려 받고 있습니다. 한 번에 하나의 가치 만 끌어 당기는 것만으로 이제는 목록보기를 보여줍니다. – user2360766

관련 문제