2016-03-28 3 views
0

私はオブジェクトクラス(public static List<Restaurant> res;)を使用して、サーバーからjsonデータを取得しています。アプリの流れは以下の通りです。リスト内の最後のアイテム<Object>はRecyclerViewの特定のテキストビューで繰り返されます

ステップ1:(MainActivity.class)サーバーから

取得データ

ステップ2:

List<Object>クラスにすべてのJSONデータを設定します。そしてMainActivityでのpublic staticとしてList<Object>

ステップ3に宣言:で設定しながら、私は公共のstaticとして宣言にRecyclerview.DueにList<Object>クラス値を移入する必要が

ここ

を、私はこれを直接呼ばれますMainActivityを親として持つフラグメント内のアダプタ。

adapter = new CardAdapter(MainActivity.res, context); 
    recyclerView.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 

しかし、問題はRecyclerViewを移入しながら、私は最後の項目がList<Restaurant>大き件まで繰り返され、取得していますということです。

MainActivity(AsyncTask法):

private class TabNameSync extends AsyncTask<Void, Void, String> { 
     String BASE_URL = Config.DATA_URL; 
     ProgressDialog nDialog; 
     HashMap<String, String> hashMapPost = new HashMap<>(); 

     @Override 
     protected void onPreExecute() { 
      nDialog = new ProgressDialog(MainActivity.this); 
      nDialog.setMessage("Loading..."); 
      nDialog.setIndeterminate(true); 
      nDialog.setCancelable(true); 
      nDialog.show(); 
     } 

     @Override 

     protected String doInBackground(Void... params) { 

      HttpURLConnection con = null; 
      InputStream is = null; 

      StringBuffer buffer; 
      String bufferVariable = null; 

      hashMapPost.put("tag", "onload"); 
      hashMapPost.put("lat", "18.71851808"); 
      hashMapPost.put("log", "97.74131474"); 

      try { 
       con = (HttpURLConnection) (new URL(BASE_URL)).openConnection(); 
       con.setDoInput(true); 
       con.setDoOutput(true); 
       con.setRequestMethod("POST"); 
       con.connect(); 
       OutputStream os = con.getOutputStream(); 
       BufferedWriter writer = new BufferedWriter(
         new OutputStreamWriter(os, "UTF-8")); 
       writer.write(commonUtil.getPostDataString(hashMapPost)); 

       writer.flush(); 
       writer.close(); 
       os.close(); 

       buffer = new StringBuffer(); 
       int responseCode = con.getResponseCode(); 
       if (responseCode == HttpsURLConnection.HTTP_OK) { 
        is = con.getInputStream(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
        String line; 
        while ((line = br.readLine()) != null) 
         buffer.append(line).append("\r\n"); 
        is.close(); 
       } 
       con.disconnect(); 
       bufferVariable = buffer.toString(); 
       return buffer.toString(); 
      } catch (Throwable t) { 
       t.printStackTrace(); 
      } finally { 
       try { 
        if (is != null) { 
         is.close(); 
        } 
       } catch (Throwable t) { 
       } 
       try { 
        if (con != null) { 
         con.disconnect(); 
        } 
       } catch (Throwable t) { 
       } 
       if (!bufferVariable.equalsIgnoreCase(" ")) { 
        try { 

         JSONArray jArray = new JSONArray(bufferVariable); 
         int jsonLength = jArray.length(); 

         for (int j = 0; j < jArray.length(); j++) { 

          Restaurant_Beam item; 
          JSONObject jsonObj = jArray.getJSONObject(j); 
          item = new Restaurant_Beam(); 
          item.setIG_LIKECOUNT(jsonObj.getInt(Config.LIKE)); 
          item.setIG_DELIVERY(jsonObj.getInt(Config.DELIVERYTIME)); 
          item.setIG_PRODUCTID(jsonObj.getString(Config.PRODUCTID)); 
          item.setIG_SALES_PRICE(jsonObj.getString(Config.SALESPRICE)); 
          item.setIG_VOUCHER_ID(jsonObj.getString(Config.VOUCHERID)); 
          item.setIG_VOUCHEROFFER(jsonObj.getString(Config.VOUCHEROFFER)); 
          item.setIG_CATEGORY_ID(jsonObj.getString(Config.CATEID)); 
          item.setIG_IMAGEURL(jsonObj.getString(Config.IMGID)); 
          item.setIG_PRODUCTNAME(jsonObj.getString(Config.PRODUCTNAME)); 
          item.setIG_CATEGORYNAME(jsonObj.getString(Config.CATENAME)); 

          Restaurant.add(item); 

          listSuperHeroes = Restaurant; 

          strTabName = jsonObj.getString("Cate Name"); 
          String strProductID = jsonObj.getString("Pro_Id"); 
          String strProductName = jsonObj.getString("Product_Name"); 
          String strSalesPrice = jsonObj.getString("Sales Price"); 
          String strVoucherId = jsonObj.getString("Voucher Id"); 
          String strVoucherOffer = jsonObj.getString("voucher Offer"); 
          String strCatId = jsonObj.getString("Cat Id"); 
          String strImage = jsonObj.getString("Image"); 
          String strLikes = jsonObj.getString("likes"); 
          String strDeliveryTime = jsonObj.getString("deliverytime"); 
          strDbName = jsonObj.getString("dbname"); 


          tabName.add(strTabName); 

          DbName.add(strDbName); 

          if (jsonLength == jArray.length()) { 
           JSONObject jsonObj2 = jArray.getJSONObject(jsonLength - 1); 
           Config.IMAGE_URL = jsonObj2.getString("url"); 
          } 
         } 


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

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

      final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
      tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); 

      values = new ArrayList<String>(); 
      values = tabName; 
      HashSet<String> hashSet = new HashSet<String>(); 
      hashSet.addAll(values); 
      values.clear(); 
      values.addAll(hashSet); 

      nDialog.dismiss(); 

      // setTime_ToSlide(); 
      viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); 
      tabLayout.post(new Runnable() { 
       @Override 
       public void run() { 
        tabLayout.setupWithViewPager(viewPager); 
       } 
      }); 
      SharedPreferences.Editor editor = pref.edit(); 
      editor.putString("FIRST_TAB", values.get(1)); 
      editor.putString("SECOND_TAB", values.get(0)); 
      editor.putString("THIRD_TAB", values.get(2)); 
      editor.commit(); 

     } 
    } 

enter image description here

ここのみRED rounderdデータを繰り返し、他のデータは、正しいデータに関連しています。

私もコードを掲載しました。確認してください。

+0

のアダプタコード –

+0

を投稿あなたのリストが正しく読み込まれていますか? –

+0

ありがとうコード –

答えて

1

CardAdapterコンストラクタで送信リストオブジェクト

new CardAdapter(MainActivity.res, context, List<object> abc); 

、それを使用しています。

+0

MainActivity.resはリストです @Vinay –

+0

リスト res - >すべてが格納されており、アダプタに渡しました –

+0

resは静的で、パーザにする必要はありません.Uは直接使用できます。 in CardAdapter。 – Vinay

1

コードに「参照」問題があります。ループ内でRestaurant item= new Restaurant();を移動します。このように:

public static List<Restaurant> res; 
    try { 
          JSONArray jArray = new JSONArray(bufferVariable); 
          int jsonLength = jArray.length(); 
          for (int j = 0; j < jArray.length(); j++) { 
           JSONObject jsonObj = jArray.getJSONObject(j); 

           strTabName = jsonObj.getString("Cate Name"); 
           Restaurant item= new Restaurant(); 

           item.setIG_LIKECOUNT(jsonObj.getInt(Config.LIKE)); 
           item.setIG_DELIVERY(jsonObj.getInt(Config.DELIVERYTIME)); 
           item.setIG_PRODUCTID(jsonObj.getString(Config.PRODUCTID)); 
           item.setIG_PRODUCTNAME(jsonObj.getString(Config.PRODUCTNAME)); 
           item.setIG_SALES_PRICE(jsonObj.getString(Config.SALESPRICE)); 
           item.setIG_VOUCHER_ID(jsonObj.getString(Config.VOUCHERID)); 
           item.setIG_VOUCHEROFFER(jsonObj.getString(Config.VOUCHEROFFER)); 
           item.setIG_CATEGORY_ID(jsonObj.getString(Config.CATEID)); 
           item.setIG_CATEGORYNAME(jsonObj.getString(Config.CATENAME)); 
           item.setIG_IMAGEURL(jsonObj.getString(Config.IMGID)); 

           res.add(item); 

           if (jsonLength == jArray.length()) { 
            JSONObject jsonObj2 = jArray.getJSONObject(jsonLength - 1); 
            Config.IMAGE_URL = jsonObj2.getString("url"); 
           } 
          } 
+0

ポップアップするコードを投稿しました。リスト。確認してください。 –

+0

私の答えを更新しました。 –

+0

申し訳ありません...以前と同じように繰り返しています。 –

1

自分でバグを修正しました。私が変更したのは、すべての変数(製品名、カテゴリ名)をpublic static StringとしてList<Object>クラスに設定したことです。

静的を除去した後、私は正確なresult.My RecyclerViewは完璧なように適応されました。次

などの代わりに

public static String ProductName; 

使用この

public String ProductName; 
関連する問題