2016-03-07 130 views
-1
FeedItem item=new FeedItem(); 
    protected void parseResult(JSONObject result) { 
     try { 
      JSONObject jsonObject = result.getJSONObject("category"); 
      //Iterate the jsonArray and print the brand_name 
      JSONArray jsonArray1 = jsonObject.getJSONArray("brand_title"); 
      for (int i = 0; i < jsonArray1.length(); i++) { 
       item.setTitle1(jsonArray1.getString(i)); 
       feedsList.add(item);System.out.println("dsc==dc=dsc=ds=c=ds======" + item.getTitle1()); 
      } 
JSONArray jsonArray2 = jsonObject.getJSONArray("address"); 
        for(int i=0; i < jsonArray2.length(); i++) { 
         FeedItem item2=new FeedItem(); 
         item2.setTitle2(jsonArray2.getString(i)); 
         feedsList.add(item2); 
        // System.out.println("dsc==dc=dsc=ds=c=ds======" + item2.getTitle2()); 
        } 

我得到了上述兩個println語句中所需的數據。 getTitle1()& getTtile2()是FeedItem類,這是如下的方法:沒有得到JsonArray數據

然後
public class FeedItem { 
     ArrayList<String> arr=new ArrayList<>(); 
     public String getTitle1() 
     { String s = ""; 
      for(int i=0;i<arr.size();i++){ 
       s=arr.get(i); 
     } 
      return s; 
     } 
     public void setTitle1(String title) 
     { 
      arr.add(title); 
     } 
public String getTitle2() 
    { String s1 = ""; 
     for(int i=0;i<arr_address.size();i++){ 
      s1=arr_address.get(i); 
     } 
     return s1; 
    } 
    public void setTitle2(String adres) 
    { 
     // arr= new ArrayList<>(); 
     arr_address.add(adres); 
    } 

onPostExecute()方法執行與適配器設置爲回收視圖。 MyRecyclerAdapter類是回收的適配器如下播種:

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> { 

     private List<FeedItem> feedItemList; 
     FeedItem feed; 

     public MyRecyclerAdapter() { 
      feedItemList=new ArrayList<>(); 
      feed= new FeedItem(); 
       feedItemList.add(feed); 
     } 

     public MyRecyclerAdapter(List<FeedItem> list){ 
      this.feedItemList=list; 
     } 

     @Override 
       public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
       View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.content_salon, viewGroup, false); 
       ViewHolder viewHolder=new ViewHolder(view); 
       return viewHolder; 
      } 

     @Override 
      public void onBindViewHolder(ViewHolder holder, int i) { 

       feed = feedItemList.get(i); 
       holder.brandName.setText(feed.getTitle1()); 
       holder.brandAddress.setText(feed.getTitle2()); 
       // System.out.println("======jjjj===="+feed.getTitle2()); 
       } 


     @Override 
     public int getItemCount() { 
      return feedItemList.size(); 
     } 

     public class ViewHolder extends RecyclerView.ViewHolder { 
      public ImageView brandImage; 
      public TextView brandName; 
      public TextView brandAddress; 

      public ViewHolder(View itemView) { 
       super(itemView); 


brandAddress = (TextView) itemView.findViewById(R.id.person_age); 
      brandName = (TextView) itemView.findViewById(R.id.person_name); 
     } 
     } 
     } 

的問題是,在onBindViewHolder方法的System.out.println語句()打印什麼,但我需要顯示在個人卡陣列的每個元素。我在同一時間獲取「brandName」的數據,但不是「brandAddress」。那麼,任何人都可以提出或糾正一些能爲我工作的東西嗎?

回答

1

你應該爲每一個迭代LOP創建一個新的項目,否則將覆蓋以前的值以及

for (int i = 0; i < jsonArray1.length(); i++) { 
     item = new FeedItem(); 
     item.setTitle1(jsonArray1.getString(i)); 
     feedsList.add(item);System.out.println("dsc==dc=dsc=ds=c=ds======" + item.getTitle1()); 
} 
+0

感謝。它爲我工作。我編輯了這個問題。你能幫我解決這個問題嗎? –