0% found this document useful (0 votes)
38 views3 pages

Device List Activity

This Java code defines a DeviceListActivity class that extends the BaseActivity class. The DeviceListActivity class retrieves a list of devices from an API call using Retrofit, stores the list in a DeviceRecyclerAdapter, and displays it in a RecyclerView. It handles the API call asynchronously with callbacks to handle the response or failure cases. On back button press, it displays a toast message instead of closing the activity.

Uploaded by

SAURABH SABLOK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Device List Activity

This Java code defines a DeviceListActivity class that extends the BaseActivity class. The DeviceListActivity class retrieves a list of devices from an API call using Retrofit, stores the list in a DeviceRecyclerAdapter, and displays it in a RecyclerView. It handles the API call asynchronously with callbacks to handle the response or failure cases. On back button press, it displays a toast message instead of closing the activity.

Uploaded by

SAURABH SABLOK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.ryzmedia.tatasky.ui.

activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ryzmedia.tatasky.R;
import com.ryzmedia.tatasky.adapter.DeviceRecyclerAdapter;
import com.ryzmedia.tatasky.helper.RetrofitHelper;
import com.ryzmedia.tatasky.network.dto.response.BaseResponse;
import com.ryzmedia.tatasky.network.dto.response.Devices;
import com.ryzmedia.tatasky.utility.AppConstants;
import com.ryzmedia.tatasky.utility.SharedPreference;
import com.ryzmedia.tatasky.utility.Utility;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DeviceListActivity extends BaseActivity {


@BindView(R.id.recycler_view_device_list)
RecyclerView recyclerViewDeviceList;

private DeviceRecyclerAdapter deviceRecyclerAdapter;


private Context mContext;

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

mContext = DeviceListActivity.this;
linkViewId();

getDevices();
}

private void linkViewId() {

recyclerViewDeviceList = (RecyclerView)
findViewById(R.id.recycler_view_device_list);
}

public void getDevices() {

Call<Devices> call = RetrofitHelper.getCommonApi(mContext).listDevices(


SharedPreference.getString(mContext,
AppConstants.PREF_KEY_SUBSCIBER_ID));
if (Utility.isNetworkConnected(mContext)) {
// show progress dialog
showProgressDialog(getString(R.string.logout_api_message));
call.enqueue(new Callback<Devices>() {
@Override
public void onResponse(Call<Devices> call, Response<Devices>
response) {
// dismiss progress dialog
hideProgressDialog();

if (response.body() != null && response.isSuccessful()) {


if (response.body().code == 0) {
showDeviceList(response.body().data.deviceList);
} else {
Utility.showToast(mContext, response.body().message);
}
}
}

@Override
public void onFailure(Call<Devices> call, Throwable t) {
// dismiss progress dialog
hideProgressDialog();

Utility.showToast(mContext, t.toString());
}
});
} else {
Utility.showToast(mContext,
getString(R.string.no_internet_connection));
}
}

private void showDeviceList(List<Devices.DeviceList> list) {


// ArrayList<Devices.DeviceList> list = new ArrayList<>();
// list.add("device 1");
// list.add("device 2");
// list.add("device 3");
// list.add("device 4");
// list.add("device 5");
// list.add("device 6");
// list.add("device 7");
// list.add("device 8");
// list.add("device 9");
// list.add("device 10");

deviceRecyclerAdapter = new DeviceRecyclerAdapter(list, mContext);


RecyclerView.LayoutManager mLayoutManager = new
LinearLayoutManager(mContext);
recyclerViewDeviceList.setLayoutManager(mLayoutManager);
recyclerViewDeviceList.setItemAnimator(new DefaultItemAnimator());
recyclerViewDeviceList.setAdapter(deviceRecyclerAdapter);
}

/*// override back button to restrict dialog activity from being dismissed
@Override
public void onBackPressed() {
super.onBackPressed();
// dismisses the current activity & all activities in the current stack
finishAffinity();
}*/
// do nothing on back button click
@Override
public void onBackPressed() {
Utility.showToast(mContext,
getString(R.string.device_friendly_name_exceeded));
}
}

You might also like