Reputation: 3246
RecyclerView not showing any item.
I have the following code for
a) Layout
<?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"
android:orientation="vertical"
tools:context=".views.ServiceListActivity">
<include
android:id="@+id/topMenu"
layout="@layout/content_top_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:text="Service List"
android:textAlignment="center"
android:textSize="@dimen/_12sdp"
android:textStyle="bold"></TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
b) RecyclerView Adapter
public class InstallationListAdapter extends RecyclerView.Adapter<InstallationListAdapter.ServiceViewHolder> {
private Context mCtx;
private List<TransactionRecord> transactionRecordList;
public InstallationListAdapter(Context mCtx, List<TransactionRecord> transactionRecords, String ActivityName) {
this.mCtx = mCtx;
this.transactionRecordList = transactionRecords;
}
@NonNull
@Override
public ServiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
return new ServiceViewHolder(ListItemInstallationBinding.inflate(LayoutInflater.from(parent.getContext()),
parent, false));
}
@Override
public void onBindViewHolder(@NonNull ServiceViewHolder holder, int position) {
TransactionRecord transactionRecord = transactionRecordList.get(position);
holder.binding.txtCustomerName.setText(transactionRecord.getCustomer_name());
holder.binding.txtAddress.setText(transactionRecord.getCustomer_address());
holder.binding.txtDate.setText(transactionRecord.getService_date());
holder.binding.txtProducts.setText(transactionRecord.getProduct_name());
holder.binding.txtTechnicianRemarks.setText(transactionRecord.getTechnician_notes());
holder.binding.txtCustomerName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + transactionRecord.getCustomer_mobile()));
mCtx.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return transactionRecordList == null ? 0 :
transactionRecordList.size();
}
public void updateList(List<TransactionRecord> list) {
transactionRecordList = list;
notifyDataSetChanged();
}
public class ServiceViewHolder extends RecyclerView.ViewHolder{
ListItemInstallationBinding binding;//Name of the test_list_item.xml in camel case + "Binding"
public ServiceViewHolder(ListItemInstallationBinding b){
super(b.getRoot());
binding = b;
}
}
}
c) In Activity
public class ServiceListActivity extends BaseActivity {
ActivityServiceListBinding binding;
List<TransactionRecord> transactionRecordList;
RecyclerView recyclerView;
EditText txtFilter;
InstallationListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityServiceListBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
binding.topMenu.btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
new getServiceData().execute();
}
private class getServiceData extends AsyncTask<Void, Void, ReturnClass<List<TransactionRecord>>> {
@Override
public void onPreExecute() {
ProgressDialogHandler.getInstance().showCustomProgressDialog(activity);
}
@Override
public ReturnClass<List<TransactionRecord>> doInBackground(Void... voidArr) {
ReturnClass<List<TransactionRecord>> returnClass = new ReturnClass<>();
try {
returnClass = APIProcessor.getInstallationData();
} catch (Exception e) {
e.printStackTrace();
returnClass.setStatus(false);
returnClass.setMessage(e.getMessage());
}
return returnClass;
}
@Override
public void onPostExecute(ReturnClass<List<TransactionRecord>> returnClass) {
if (!returnClass.getStatus().booleanValue()) {
ProgressDialogHandler.getInstance().dismissCustomProgressDialog(activity);
if (returnClass.getMessage().equals("Return response code is not 200")) {
activity.runOnUiThread(new Runnable() {
public void run() {
//some code
}
});
} else {
CommonUtils.showAlertDialog(activity, "Error", returnClass.getMessage());
}
return;
}
activity.runOnUiThread(new Runnable() {
public void run() {
transactionRecordList = returnClass.getValue();
adapter = new InstallationListAdapter(activity, transactionRecordList, "completed");
binding.recylcerView.setAdapter(adapter); ProgressDialogHandler.getInstance().dismissCustomProgressDialog(activity);
}
});
}
}
}
Upvotes: 1
Views: 1234
Reputation: 2496
you don't need runonUiThread method in onPostExecute method.
And you didn't set layoutmanager in recyclerview.
Upvotes: 1
Reputation: 2296
You should set layout manager
to RecyclerView.
First way in xml layout:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
or in Java code:
recyclerView.setLayoutManager(new LinearLayoutManager(context));
Upvotes: 4