2014-03-13 112 views
0

我的JSON輸出:android系統中的ListView填充JSON

[{"FirstName":"childname2"},{"FirstName":"childname4"}] 

我試圖填充我的ListView使用JSON DATAS我得到了通過使用POST方法, 通過這樣做,我有我的總JSON [{"FirstName":"childname2"},{"FirstName":"childname4"}]在postexecute.And輸出,當我試圖通過使用循環它沒有顯示任何錯誤,,,以及不填充我的名單,, 建議通過在列表中該值,,,,

我的Java :

private ListView lv=(ListView) findViewById(R.id.listView1); 
    @Override 
    protected void onPostExecute(String result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 
    Toast.makeText(Certify_Child_Count.this,result, Toast.LENGTH_LONG).show(); 
    if (result.equals("[]")) 
    { 
     Toast.makeText(Certify_Child_Count.this, 
       "Wrong Username or Password", Toast.LENGTH_LONG).show(); 

    } 
    else 
    { 
     Toast.makeText(Certify_Child_Count.this,result, Toast.LENGTH_LONG).show(); 
     JSONArray arr=new JSONArray(); 
     JSONObject obj = null; 
     //ArrayAdapter<String> adapter=new ArrayAdapter<String>(Certify_Child_Count.this, android.R.layout.simple_list_item_1, list); 
     //lv.setAdapter(adapter); 
     for(int i=0;i<arr.length();i++) 
     { 
      Toast.makeText(Certify_Child_Count.this,"going into for loop", Toast.LENGTH_LONG).show(); 

     try { 
      obj=arr.getJSONObject(i); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      list.add(obj.getString("FirstName")); 
      Toast.makeText(Certify_Child_Count.this,obj.getString("FirstName"), Toast.LENGTH_LONG).show(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     } 

    } 
    } 
+0

你應該開始解析json中包含的信息 – Blackbelt

+0

我不明白..怎麼做 – arun

+0

我的意思是,你在試圖在谷歌上尋找它之前問? – Blackbelt

回答

0

您的適配器在哪裏?您需要一個適配器來顯示列表的數據。如果是單個文本,則可以使用基本適配器,並在每次完成請求並解析數據時通知它。

+0

在我for循環,,,我已經嘗試Toast.makeText(Certify_Child_Count.this,「進入循環」,Toast.LENGTH_LONG).show(); 。,,,,即使它沒有工作.. – arun

+0

你能看到吐司。makeText(Certify_Child_Count.this,result,Toast.LENGTH_LONG).show();烤麪包? –

+0

如果你可以看到第一個烤麪包和結果字符串是正確的,然後開始設置你的json數組對象。由於您創建了一個空對象,因此您的數組將爲空。使用JSONArray arr = new JSONArray(result);代替。 –

0

你應該改變

JSONArray arr=new JSONArray(); 

JSONArray arr=new JSONArray(result); 

你不添加結果創建一個空的JSONArray

1

您需要使用適配器來填充您的ListView。

你需要創建一個類來實現ArraryAdapters<> 例如:

public class ListAdapters extends ArraryAdapters<string> 

這個類實現getView()方法。在這裏你需要填充一個列表視圖。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 

     LayoutInflater inflater = context.getLayoutInflater(); 

     convertView = inflater.inflate(R.layout.list_rows, null); 

     holder = new ViewHolder(); 

     holder.tectview = (TextView) convertView 
       .findViewById(R.id.questionary_name); 
     convertView.setTag(holder); 
    } 
    holder = (ViewHolder) convertView.getTag(); 


    String s = items[position]; 

    holder.tectview.setText(s); 
    return convertView; 
} 
0

你很幸運,我只是完成了一個與你幾乎相同的演示。希望它能幫助你解決問題。

package com.ecnu.vendingmachine; 

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.ListFragment; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

import com.ecnu.vendingmachine.widgets.JSONParser; 

public class VendingFragment extends ListFragment { 

    private String Tag = "VendingFragment"; 
    private static final String TAG_SUCCESS = "success"; 

    private ListView listView; 

    // Progress Dialog 
    private ProgressDialog pDialog; 
    // Creating JSON Parser object 
    JSONParser jsonParser = new JSONParser(); 
    JSONArray vendingmachine = null; 

    ArrayList<HashMap<String, String>> vendinglist; 

    // url to get all products list 
    MainActivity main = new MainActivity(); 
    private String url_all_vendingmachine = main.getIP() 
      + "vendingmachine/get_all_vendingmachine.php"; 

    // products JSONArray 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     return inflater.inflate(R.layout.vending_main, container, false); 
    } 

    @Override 
    public void onViewCreated (View view, Bundle savedInstanceState) { 

     vendinglist = new ArrayList<HashMap<String, String>>(); 
     //listView = (ListView) view.findViewById(R.id.list); 

     listView = getListView(); 
     new get_all_vendingmachine().execute(); 
    } 

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

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(getActivity()); 
      pDialog.setMessage("Creating Product.."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
      Log.i(Tag, "pDialog"); 
     } 

     protected String doInBackground(String... args) { 

      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 

      Log.i(Tag, url_all_vendingmachine); 
      // getting JSON Object 
      // Note that create product url accepts POST method 

      JSONObject json = jsonParser.makeHttpRequest(
        url_all_vendingmachine, "GET", params); 

      // check log cat for response 
      Log.i(Tag, json.toString()); 

      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // Ada record Data (SUCCESS = 1) 
        // Getting Array of vendingmachine 
        vendingmachine = json.getJSONArray("vendings"); 

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

         // Storing each json item in variable 
         String id = c.getString("VMid"); 
         String name = c.getString("Name"); 
         String address = c.getString("Address"); 
         Log.i(Tag, id); 
         Log.i(Tag, name); 
         Log.i(Tag, address); 
         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 
         // adding each child node to HashMap key => value 
         map.put("VMid", id); 
         map.put("Name", name); 
         map.put("Address", address); 

         // adding HashList to ArrayList 
         vendinglist.add(map); 
        } 

       } else { 
        // failed to create product 
        getActivity().finish(); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once done 
      pDialog.dismiss(); 
      getActivity().runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        // updating listview 
        // HashMap<String, String>中的key 
        String[] from = { "Name", "Address", "VMid" }; 
        // list_item.xml中對應的控件ID 
        int[] to = { R.id.vending_name, R.id.vending_address, 
          R.id.vending_id }; 
        Log.i(Tag, from[0]); 
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), 
          vendinglist, R.layout.vending_list, from, to); 
        listView.setAdapter(adapter); 
       } 
      }); 
     } 

    } 

} 

的vending_main_xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/whiteColor" > 

    <include 
     android:id="@+id/layout_buy_title_actionbar" 
     layout="@layout/headbar_title" /> 

    <ListView 
     android:id="@id/android:list" 
     android:layout_below="@id/layout_buy_title_actionbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="@android:color/transparent" 
     android:divider="@drawable/reader_item_divider" 
     android:listSelector="@android:color/transparent" > 
    </ListView> 

</RelativeLayout> 

的vending_list.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="@dimen/selectVmListCell_Height" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="@dimen/selectVmListCell_Height" 
     android:layout_gravity="center_vertical" 
     android:background="@drawable/bg_listview" 
     android:gravity="center_vertical" 
     android:paddingLeft="@dimen/layout_Margin_10" > 

     <LinearLayout 
      android:id="@+id/imageTypeLayout" 
      android:layout_width="@dimen/smallImage_HW_45" 
      android:layout_height="fill_parent" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:gravity="center_vertical" 
      android:orientation="vertical" > 

      <ImageView 
       android:id="@+id/vendingImage" 
       style="@style/smallImageStyle" 
       android:src="@drawable/buy_main_not_shop" /> 

      <TextView 
       android:id="@+id/distanceText" 
       style="@style/smallTxtStyle" 
       android:layout_gravity="center_horizontal" 
       android:gravity="center" 
       android:singleLine="true" 
       android:textColor="@color/lightOrangeColor" /> 
     </LinearLayout> 

     <RelativeLayout 
      android:id="@+id/vendingSave" 
      android:layout_width="47.0dip" 
      android:layout_height="fill_parent" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" > 

      <ImageView 
       android:id="@+id/vendingSaveImage" 
       android:layout_width="27.0dip" 
       android:layout_height="27.0dip" 
       android:layout_centerInParent="true" 
       android:layout_centerVertical="true" 
       android:background="@drawable/bg_favor_btn" /> 
     </RelativeLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="@dimen/layout_Margin_10" 
      android:layout_toLeftOf="@id/vendingSave" 
      android:layout_toRightOf="@id/imageTypeLayout" 
      android:gravity="center_vertical" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/vending_name" 
       style="@style/midDarkGrayTxtStyle" 
       android:layout_width="fill_parent" /> 

      <TextView 
       android:id="@+id/vending_address" 
       style="@style/smallLightGrayTxtStyle" 
       android:layout_width="fill_parent" 
       android:maxLines="2" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <TextView 
        style="@style/smallLightGrayTxtStyle" 
        android:text="Number:" /> 

       <TextView 
        android:id="@+id/vending_id" 
        style="@style/smallLightGrayTxtStyle" /> 
      </LinearLayout> 
     </LinearLayout> 
    </RelativeLayout> 

</LinearLayout> 

希望它可以幫助你找到如何把列表視圖成片段。 我的listview中的元素很複雜,如果你只想要一行,它比我的代碼更簡單。