2017-07-17 79 views
0

我一直在嘗試使用凌空庫來獲取JSON數據。一切正常,但數據不會顯示在CustomListAdapter上。我認爲問題出在自定義適配器上。 JSON的Url可以,數據成功獲取。代碼如下。自定義列表適配器在通過JSON獲取數據後不顯示

CustomListAdapter.java代碼

public class CustomListAdapter extends ArrayAdapter<food> { 

private Activity act; 
private ArrayList<food>data; 
private int layoutResource; 
public CustomListAdapter(@NonNull Activity activity, int resource, ArrayList<food>items) { 
    super(activity, resource, items); 

    act=activity; 
    data=items; 
    layoutResource=resource; 

} 

@Override 
public int getCount() { 
    return data.size(); 
} 

@Nullable 
@Override 
public food getItem(int position) { 
    return super.getItem(position); 
} 

@Override 
public int getPosition(@Nullable food item) { 
    return super.getPosition(item); 
} 

@Override 
public long getItemId(int position) { 
    return super.getItemId(position); 
} 

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

    if(row==null || (row.getTag()==null)){ 
     LayoutInflater inflater=LayoutInflater.from(act); 
     row=inflater.inflate(layoutResource,parent,false); 

     holder=new ViewHolder(); 

     //get references to views 
     holder.ItemName=(TextView)row.findViewById(R.id.listName); 

     row.setTag(holder); 

    }else{ 
     holder=(ViewHolder)row.getTag(); 
    } 

    holder.foodItem=data.get(position); 

    //we can now display data 
    holder.ItemName.setText(holder.foodItem.getItemName()); 

    final ViewHolder finalHolder = holder; 
    row.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i=new Intent(act, FoodDetails.class); 
      Bundle mbundle=new Bundle(); 
      mbundle.putSerializable("bundleOnj", finalHolder.foodItem); 
      i.putExtras(mbundle); 
      act.startActivity(i); 
     } 
    }); 
    return row; 
} 

public class ViewHolder{ 
    TextView ItemName; 
    food foodItem; 
} 
} 

ListActivity.java代碼;在此代碼中,當我爲獲取JSON對象設置文本時,它正常運行。

public class ListActivity extends AppCompatActivity { 

private CustomListAdapter adapter; 
private ListView listview; 
private ArrayList<food>foodItems=new ArrayList<>(); 
private String LEFT_URL="https://api.nutritionix.com/v1_1/search/"; 
private String RIGHT_URL="?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_calories%2Cnf_total_fat&appId=MY_IDd&appKey=MY_API"; 
private TextView selectedItem; 

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

    listview=(ListView)findViewById(R.id.listView); 
    selectedItem=(TextView)findViewById(R.id.selectedItem); 
    adapter=new CustomListAdapter(ListActivity.this,R.layout.row,foodItems); 
    listview.setAdapter(adapter); 

    getFoodDetails("Butter"); 
} 

private void getFoodDetails(String item){ 
    //clear data first 
    foodItems.clear(); 

    String FINAL_URL=LEFT_URL+item+RIGHT_URL; 

    JsonObjectRequest foodObjectRequest=new JsonObjectRequest(Request.Method.GET, 
      FINAL_URL, (JSONObject) null, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 

      try{ 
       JSONArray foodArray= response.getJSONArray("hits"); 

       for(int i=0;i<foodArray.length();i++){ 
        JSONObject JsonObject=foodArray.getJSONObject(i); 
        JSONObject fieldsObject=JsonObject.getJSONObject("fields"); 

        //get the Json Details 

        String itemName=fieldsObject.getString("item_name"); 
        //selectedItem.setText("item Selected: "+itemName); 
        String brand_name=fieldsObject.getString("brand_name"); 
        selectedItem.setText("item Selected: "+brand_name); 
        String servingSizeUnit=fieldsObject.getString("nf_serving_size_unit"); 
        double calories=fieldsObject.getDouble("nf_calories"); 
        //selectedItem.setText("item Selected: "+calories); 
        double fat=fieldsObject.getDouble("nf_total_fat"); 


        //putting data to set functions 
        food foodInfo=new food(); 
        foodInfo.setBrandName(brand_name); 
        foodInfo.setItemName(itemName); 
        foodInfo.setCalories(calories); 
        foodInfo.setFat(fat); 
        foodInfo.setServingSizeunit(servingSizeUnit); 

        foodItems.add(foodInfo); 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
    AppController.getInstance().addToRequestQueue(foodObjectRequest); 
} 
} 
+1

嘗試'notifyDataSetChanged()'添加一個新的對象,以你的'foodItems'名單之後。 – beeb

+0

add ** adapter.notifyDataSetChanged(); ** after foodItems.add(foodInfo); –

回答

0

呼叫adapter.notifyDataSetChanged()對象添加到您的列表

+0

感謝,像一個魅力工作。 – Panther

0

後您獲取數據之前設置適配器。 檢索數據後設置適配器。

中的onCreate刪除此行

listview.setAdapter(adapter); 

在getFoodDetails for循環後添加此行

listview.setAdapter(adapter); 
+2

這不是問題。您還可以使用空列表初始化適配器。然後,您可以在顯示進度對話框的同時獲取數據。 – beeb

相關問題