Banana
Banana

Reputation: 2443

List Not Displaying with RecyclerView in Android

I am relatively new to both coding and Android. Right now I am making an app that has a few lists. I used ListView for this, and I was combining it with TextDrawable. But after reading up on benefits of RecyclerView I decided to replace my ListView with RecyclerView. My listView worked without any problem so I modified it so it is compatible with recyclerView.

The problem I have is that is that the list is not showing now, not a single item, but I do not have any errors displaying.

My Adapter:

public class RestaurantsAdapter extends RecyclerView.Adapter<RestaurantsAdapter.RestaurantsViewHolder> {

    private List<Restaurant> RestaurantItems;

    public RestaurantsAdapter(List<Restaurant> RestaurantList) {
        this.RestaurantItems = RestaurantList;
    }

    public class RestaurantsViewHolder extends RecyclerView.ViewHolder {
    public TextView RestaurantName, date, time;
    public ImageLoader imageLoader;

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

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        ImageView thumbNail = (ImageView) itemView.findViewById(R.id.thumbnail);
        TextView RestaurantName = (TextView) itemView.findViewById(R.id.RestaurantName);
        TextView date = (TextView) itemView.findViewById(R.id.date);
        TextView time = (TextView) itemView.findViewById(R.id.time);

        ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
//       generate random color
        int color1 = generator.getRandomColor();
//       generate color based on a key (same key returns the same color), useful for list/grid views
        int color2 = generator.getColor("[email protected]");
        char firstCharacter = RestaurantName.getText().toString().charAt(0);
        TextDrawable textDrawable = TextDrawable.builder().beginConfig().withBorder(1).endConfig().buildRoundRect(String.valueOf(firstCharacter), color1, 10);
//        TextDrawable textDrawable = TextDrawable.builder().buildRect(String.valueOf(firstCharacter), color1);
        thumbNail.setBackground(textDrawable);
    }
}

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

        return new RestaurantsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RestaurantsAdapter.RestaurantsViewHolder holder, int position) {
        // getting Restaurant data for the row
        Restaurant Restaurant = RestaurantItems.get(position);
        holder.RestaurantName.setText(Restaurant.getRestaurantName());
        holder.date.setText(Restaurant.getDate());
        holder.time.setText(Restaurant.getTime());
//        holder.imageLoader.setText(Restaurant.getThumbnailUrl());
    }

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

}

My Fragment:

public class RestaurantsFragment extends Fragment {

    private static final String TAG = RestaurantsFragment.class.getSimpleName();

    // Restaurants json url
    private ProgressDialog pDialog;
    private List<Restaurant> RestaurantList = new ArrayList<>();
    private RecyclerView listView;
    private RestaurantsAdapter adapter;
    private SQLiteHandler db;

    @Override
    public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_Restaurants, container, false);

        listView = (RecyclerView) view.findViewById(R.id.Restaurants_list);
        adapter = new RestaurantsAdapter(RestaurantList);
        listView.setAdapter(adapter);

        db = new SQLiteHandler(getActivity().getApplicationContext());
        HashMap<String, String> Restaurant = db.getRestaurantDetails();
        final String RestaurantId = Restaurant.get("uid");

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

        Call<RestaurantsResponse> call = apiService.getOtherRestaurants(RestaurantId);
        call.enqueue(new Callback<RestaurantsResponse>() {
            @Override
            public void onResponse(Call<RestaurantsResponse> call, retrofit2.Response<RestaurantsResponse>response) {
                List<Restaurant> Restaurants = response.body().getResults();

                RestaurantList.addAll(Restaurants);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<RestaurantsResponse>call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu, inflater);
    }
}

My restarurant_fragment.xml

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

   <android.support.v7.widget.RecyclerView
       android:id="@+id/Restaurants_list"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:divider="@color/list_divider"
       android:scrollbars="vertical"
       android:dividerHeight="1dp"
       android:listSelector="@drawable/list_row_selector" />      
</LinearLayout>

My list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp">

<ImageView
    android:id="@+id/thumbnailRestaurantPhoto"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp"
    android:background="@drawable/default_profile"
    tools:ignore="RtlHardcoded" />

<TextView
    android:id="@+id/RestaurantName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnailRestaurantPhoto"
    android:layout_toRightOf="@+id/thumbnailRestaurantPhoto"
    android:textStyle="bold"
    tools:ignore="RtlHardcoded,SpUsage" />

<TextView
    android:id="@+id/date_opened"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/RestaurantName"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnailRestaurantPhoto"
    tools:ignore="RtlHardcoded,SpUsage" />

<TextView
    android:id="@+id/time_opened"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/date"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@+id/thumbnailRestaurantPhoto"
    android:textColor="@color/time"
    tools:ignore="RtlHardcoded,SpUsage" />

</RelativeLayout>

Any help is appreciated. Thanks in advance.

Upvotes: 1

Views: 107

Answers (1)

Banana
Banana

Reputation: 2443

My error was in my Adapter. I declared TextViews and ImageViews twice in my RestaurantsViewHolder.

Correct RestaurantViewHolder:

public class RestaurantsViewHolder extends RecyclerView.ViewHolder {
public TextView RestaurantName, date, time;
public ImageLoader imageLoader;

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

    thumbNail = (ImageView) itemView.findViewById(R.id.thumbnail);
    RestaurantName = (TextView) itemView.findViewById(R.id.RestaurantName);
    date = (TextView) itemView.findViewById(R.id.date);
    time = (TextView) itemView.findViewById(R.id.time);
    }
}

Upvotes: 1

Related Questions