0

I have created RecyclerView and showing data from JSON.

Issue I'm facing is, while Toast data is showing correctly, but in RecyclerView same data is not appear.

Here is code:

public class MainActivity extends AppCompatActivity {

private static final int NUM_LIST_ITEMS = 100;
private static final String TOKEN = 
"71cf2d3dec294394e267fbb0bf28916f4198f8d6";
private CuloAdapter culoAdapter;
List<Hotel> lh = new ArrayList<>();
RecyclerView recyclerView;


@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(newBase);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 recyclerView = findViewById(R.id.rv_tiketapi);
 LinearLayoutManager layout = new LinearLayoutManager(this);
    layout.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layout);
    CuloAdapter ar = new CuloAdapter(lh);
    recyclerView.setAdapter(ar);
    loadHotelLocation();
 }
 private void loadHotelLocation() {
    final search apiService = ApiService.getService(search.class);
    retrofit2.Call<SingleResult<Hotel>> call = apiService.findHotel(TOKEN, 
 "json");
    call.enqueue(new Callback<SingleResult<Hotel>>() {
        @Override
        public void onResponse(retrofit2.Call<SingleResult<Hotel>> call, 
  Response<SingleResult<Hotel>> response) {
            if (response.body().getDiagnostic().isSuccess()) {
                //SingleResult.ResultList list = 
  response.body().getResults();
                List<Hotel> mHotels = (List<Hotel>) 
  response.body().getResults().getResult();
                lh.addAll(mHotels);
                Toast.makeText(getApplicationContext(), "OK" +lh, 
  Toast.LENGTH_LONG).show();
            }
        }
        @Override
        public void onFailure(retrofit2.Call<SingleResult<Hotel>> call, 
  Throwable t) {
        }
    });
  }

RecyclerView Adapter :

    public class CuloAdapter extends 
    RecyclerView.Adapter<CuloAdapter.ViewHolder> {
    public CuloAdapter(List<Hotel> lh) {
        this.items = lh;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
      public TextView txtTitle;
      public TextView txtSubTitle;
      public ImageView imgIcon;

      public ViewHolder(final View container) {
         super(container);
         txtTitle = (TextView) container.findViewById(R.id.airportName);
         txtSubTitle = (TextView) container.findViewById(R.id.airportCode);
      }
    }

    private List<Hotel> items;
    public CuloAdapter(final Activity activity, List<Hotel> items) {
      this.items = items;
    }
    @Override
    public int getItemCount() {
      return items.size();
    }
    @Override
    public void onBindViewHolder(CuloAdapter.ViewHolder holder, int position) {
      Hotel item = items.get(position);
      holder.txtTitle.setText(item.getLabel());
      holder.txtSubTitle.setText(item.getId());
    }
    @Override
    public CuloAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int 
    viewType) {
      // create a new view
      View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_flight, parent, false);
      return new ViewHolder(v);
    }
}

MainActivity XML :

<?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"
android:orientation="vertical"
tools:context="com.example.admin.exampletiketapi.MainActivity">

<EditText
    android:id="@+id/et_find"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center" />

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

</android.support.v7.widget.RecyclerView>
</LinearLayout>

Item List XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/airportsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">

<TextView
    android:id="@+id/airportName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:text="Soekarno Hatta" />

<TextView
    android:id="@+id/airportCode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:text="20" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="5dp"
    android:background="#DDD"
    android:visibility="visible" />

</LinearLayout>

2 Answers2

0

In your code you are trying to set adapter before loading data.

CuloAdapter ar = new CuloAdapter(lh);
recyclerView.setAdapter(ar);
loadHotelLocation();

What i found is You are getting data in loadHotelLocation(), but trying to set adpter before that,

Visakh
  • 259
  • 3
  • 15
0

Call notifyDatasetChanged after lh.addAll(mHotels). And check for null's else you are heading for crash in case search results are zero/null

Amit
  • 2,389
  • 22
  • 29