Activity
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/item_item" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
app:fabSize="normal"
app:srcCompat="@drawable/ic_add" />
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView rvItem;
private ProgressBar progressBar;
private FloatingActionButton fabAdd;
private static final String EXTRA_STATE = "EXTRA_STATE";
ArrayList<Item> items = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fabAdd = findViewById(R.id.fab_add);
rvItem = findViewById(R.id.rv_item);
rvItem.setLayoutManager(new LinearLayoutManager(this));
rvItem.setHasFixedSize(true);
fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddItemActivity.class);
startActivity(intent);
}
});
loadAllData();
}
@Override
protected void onResume() {
super.onResume();
loadAllData();
}
public void loadAllData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait ...");
progressDialog.show();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.URL_API)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService apiService = retrofit.create(APIService.class);
final Call<Result> result = apiService.getAll(Constants.TOKEN);
result.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
progressDialog.dismiss();
Result jsonResult = response.body();
Log.d("MainActivity", jsonResult.toString());
items = jsonResult.getItems();
Log.d("MainActivity", "Size : " + items.size());
ItemAdapter itemAdapter = new ItemAdapter(MainActivity.this);
rvItem.setAdapter(itemAdapter);
if (items != null) {
itemAdapter.setListItem(items);
}
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
progressDialog.dismiss();
Log.e("MainActivity", t.getMessage());
}
});
}
}