Aishvarya Jaiswal
Aishvarya Jaiswal

Reputation: 1811

RecyclerView displaying no data

I am trying to set data to a RecyclerView.

Here is the response data.

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

Its parsing and getting it in Arraylist all are working fine but the recycler view is not showing any data. Empty Screen.

Here is the Activity

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);
        }
    }
}

Here is the Adapter:

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();
    }
}

Layout:

<?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>

The response is Fine, ArrayList has the data but recyclerview doesnt show. Any idea whats wrong here.?

Upvotes: 1

Views: 93

Answers (3)

Shalauddin Ahamad Shuza
Shalauddin Ahamad Shuza

Reputation: 3657

When you are using arrayListShops = new ArrayList<Shop>(Arrays.asList(shops)); new memory location will be allocated and the address will be assigned in arrayListShops but your adapterShops is connected with the old memory location. For this reason when you call adapterShops.notifyDataSetChanged() it will check the old memory location and will refresh the list. But you have added the data in newly allocated memory location, so the data is not displaying. You have to add data in old memory location instead of allocating new memory. Update your getResponse method as below

@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();
    }
}

Upvotes: 1

GoRoS
GoRoS

Reputation: 5375

I'm not seeing anything in your code that prevents your RecyclerView and its items to show up. Moreover, you use it in a wrong way making new instances of ShopsAdapter each time getResponse callback is invoked. Just keep the one of the init method and only handle arrayListShops list adding and removing elements. After doing that, don't forget to call adapterShops.notifyDataSetChanged() method.

I think your main problem comes from your layouts. You should see your Shop items following this layouts:

values/styles.xml

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

layout/activity_main.xml

In your case better use RelativeLayout. You used LinearLayout but you missed the important android:orientation="vertical" property which for sure wouldn't show your items on screen in your example.

<?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>

layout/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>

Upvotes: 0

Charlie Niekirk
Charlie Niekirk

Reputation: 1143

Common mistake. You need to set the adapter in your init() function with the empty list first. Then in fetchShops() method add the data to the list and call adapterShops.notifyDataSetChanged() after to update the view.

Upvotes: 0

Related Questions