2017-12-16 14 views
1

データをRecyclerViewに設定しようとしています。データが表示されないRecyclerView

ここに応答データがあります。

[ 
    { 
"shopName": "Hello World.", 
"shopTeluguName": "మమ్మీ", 
"shopAddress": "Bomanahalli" 
}, 
    { 
"shopName": "Hello World.", 
"shopTeluguName": "మమ్మీ", 
"shopAddress": "Bomanahalli" 
}, 
    { 
"shopName": "Hello World.", 
"shopTeluguName": "మమ్మీ", 
"shopAddress": "Bomanahalli" 
}, 
    { 
"shopName": "Hello.", 
"shopTeluguName": "మమ్మీ", 
"shopAddress": "Bomanahalli" 
} 
] 

その解析とArrayListの中でそれを取得し、すべて正常に動作しているが、リサイクルのビューは、任意のデータを示していません。空の画面。レイアウト

public class ShopsAdapter extends RecyclerView.Adapter<ShopsAdapter.MyViewHolder> { 

    Activity activity; 
    private List<Shop> shopList; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     LinearLayout linearLayoutParent; 
     public TextView textViewShopName, textViewShopTeluguName, textViewShopAddress; 

     public MyViewHolder(View view) { 
      super(view); 
      linearLayoutParent = (LinearLayout) view.findViewById(R.id.linearLayoutParent); 
      textViewShopName = (TextView) view.findViewById(R.id.textViewShopName); 
      textViewShopTeluguName = (TextView) view.findViewById(R.id.textViewShopTeluguName); 
      textViewShopAddress = (TextView) view.findViewById(R.id.textViewShopAddress); 
     } 
    } 


    public ShopsAdapter(Activity activity, List<Shop> shopList) { 
     this.activity = activity; 
     this.shopList = shopList; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.row_shop, parent, false); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     final Shop shop = shopList.get(position); 

     holder.textViewShopName.setText(shop.getShopName()); 
     holder.textViewShopTeluguName.setText(shop.getShopTeluguName()); 
     holder.textViewShopAddress.setText(shop.getShopAddress()); 

     holder.linearLayoutParent.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(activity, shop.getShopName(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

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

はここで活動ここ

public class MainActivity extends AppCompatActivity implements WebServiceInterface { 

    Toolbar toolbar; 
    RecyclerView recyclerViewShops; 
    private int FETCH_SHOPS_REQUEST_CODE = 1; 
    ArrayList<Shop> arrayListShops; 
    ShopsAdapter adapterShops; 

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

     init(); 

     fetchShops(); 
    } 

    private void init() { 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setTitle("Shops List"); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
     getSupportActionBar().setDisplayShowHomeEnabled(false); 
     toolbar.setTitleTextColor(Color.WHITE); 

     recyclerViewShops = (RecyclerView) findViewById(R.id.recyclerView); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); 
     recyclerViewShops.setLayoutManager(mLayoutManager); 
     recyclerViewShops.setItemAnimator(new DefaultItemAnimator()); 
     recyclerViewShops.setHasFixedSize(true); 

     arrayListShops = new ArrayList<>(); 

    } 

    private void fetchShops() { 

     HashMap<String, String> paramsList = new HashMap<>(); 

     WebServiceController webServiceController = new WebServiceController(
       this, this); 
     String hitURL = LinksAndKeys.SHOPS_URL; 
     webServiceController.sendGETRequest("", "Loading..", hitURL, paramsList, FETCH_SHOPS_REQUEST_CODE); 

    } 

    @Override 
    public void getResponse(int responseCode, String responseString, String requestType, int requestCode) { 
     if (requestCode == FETCH_SHOPS_REQUEST_CODE && responseCode == 200) { 

      Gson gson = new Gson(); 
      Shop[] shops = gson.fromJson(responseString, Shop[].class); 
      arrayListShops = new ArrayList<Shop>(Arrays.asList(shops)); 

      adapterShops = new ShopsAdapter(this, arrayListShops); 
      recyclerViewShops.setAdapter(adapterShops); 
     } 
    } 
} 

がアダプターである場合の応答がファインです

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.saravanaeggdistributors.activities.MainActivity"> 

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical" /> 

</LinearLayout> 

、ArrayListには、データを持っていますが、 recyclerview does not show 。 ここで何が間違っていますか?

答えて

2

arrayListShops = new ArrayList<Shop>(Arrays.asList(shops));を使用すると、新しいメモリの場所が割り当てられ、アドレスはarrayListShopsに割り当てられますが、adapterShopsは古いメモリの場所に接続されます。このため、adapterShops.notifyDataSetChanged()を呼び出すと、古いメモリ位置がチェックされ、リストが更新されます。しかし、新しく割り当てられたメモリ位置にデータを追加したので、データは表示されません。新しいメモリを割り当てる代わりに、古いメモリの場所にデータを追加する必要があります。私が表示されるまで、あなたのRecyclerViewとそのアイテムを防ぎ、あなたのコード内で何かを見ていないよ

@Override 
public void getResponse(int responseCode, String responseString, String requestType, int requestCode) { 
    if (requestCode == FETCH_SHOPS_REQUEST_CODE && responseCode == 200) { 

     Gson gson = new Gson(); 
     Shop[] shops = gson.fromJson(responseString, Shop[].class); 
     ArrayList<Shop> tmp = new ArrayList<>(Arrays.asList(shops)); 
     arrayListShops.addAll(tmp); 
     Toast.makeText(this, arrayListShops.size() + " Shops", Toast.LENGTH_SHORT).show(); 
     adapterShops.notifyDataSetChanged(); 
    } 
} 
+0

私はチェックしました。データはそこにあります。アダプタの内部でもデータは適切です。デバッガが2回目のアダプタのOnBindViewHolderのsettextメソッドに行くと消えます。 – Akshat

+0

プロジェクトをデバッグしてテストできるようにプロジェクトを共有できますか? –

+0

ここにあります。私はそれがあなたの側で正しく動作しないことを願っています! https://www.dropbox.com/s/cyr67cvmlwkj31q/Saravana.zip?dl=0 – Akshat

0

よくある間違い。まず、空のリストを使用してinit()関数でアダプタを設定する必要があります。次にfetchShops()メソッドでは、リストにデータを追加してから、adapterShops.notifyDataSetChanged()を呼び出してビューを更新します。

+0

。その方法でも動作しません。問題は、setText関数の実行中にアダプタのどこかで発生しています。 – Akshat

0

次のようにあなたのgetResponse方法を更新します。さらに、getResponseコールバックが呼び出されるたびに、それを間違った方法で使用してShopsAdapterの新しいインスタンスを作成します。 initメソッドの1つを保持して、要素を追加および削除するリストだけをarrayListShopsとして処理してください。その後、adapterShops.notifyDataSetChanged()メソッドを呼び出すことを忘れないでください。

あなたの主な問題はあなたのレイアウトから来ていると思います。あなたは、このレイアウト、次のあなたのショップのアイテムが表示されます。

値/のstyles.xml

<resources> 
    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light" /> 
</resources> 

レイアウト/ activity_main.xml

をお使いの場合には、より良いRelativeLayoutを使用しています。 LinearLayoutを使用しましたが、重要なandroid:orientation="vertical"プロパティを見落としました。この例では、画面上にアイテムが表示されません。

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

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical" /> 

</RelativeLayout> 

私が試したレイアウト/ row_shop.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayoutParent" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/holo_orange_dark" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" 
    > 

    <TextView 
     android:id="@+id/textViewShopName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 

    <TextView 
     android:id="@+id/textViewShopTeluguName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 

    <TextView 
     android:id="@+id/textViewShopAddress" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 


</LinearLayout> 
関連する問題