In my recyclerview, I get data from internet and when pulled down it refreshes the data but during the process of retrieving the information from internet, the refresh icon seems to freeze.
Not sure what I am doing wrong. TicketActivity.java
public class TicketActivity extends AppCompatActivity {
SwipeRefreshLayout swipeLayout;
String LOG_TAG = "ONLINE TICKET: ";
ArrayList<String> ticketIDList, issueList, statusList, created_dateList;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ticket);
ticketIDList = new ArrayList<>();
issueList = new ArrayList<>();
statusList = new ArrayList<>();
created_dateList = new ArrayList<>();
// Getting SwipeContainerLayout
swipeLayout = findViewById(R.id.swipeContainer);
recyclerView = findViewById(R.id.rvItems);
getTicketDetails();
// Adding Listener
swipeLayout.setOnRefreshListener(() -> {
getTicketDetails();
Toast.makeText(getApplicationContext(), "Refreshed!", Toast.LENGTH_LONG).show();
swipeLayout.setRefreshing(false);
});
}
void getTicketDetails() {
ticketIDList.clear();
issueList.clear();
statusList.clear();
created_dateList.clear();
Thread thread = new Thread(() -> {
try {
String response_code;
String response_data = NetworkUtils.ticket_details("dev", "General");
if (response_data != null) {
JSONObject obj_response = new JSONObject(response_data);
response_code = obj_response.getString("ResponseCode");
String issue, ticket_number, created_date, status;
if (response_code.equals("200")) {
JSONArray response_all_data = obj_response.getJSONArray("ResponseData");
for (int j = 0; j < response_all_data.length(); j++) {
JSONObject row_details = response_all_data.getJSONObject(j);
ticket_number = row_details.getString("ticket_number");
issue = row_details.getString("issue");
created_date = row_details.getString("created_date");
status = row_details.getString("status");
ticketIDList.add(ticket_number);
issueList.add(issue);
statusList.add(status);
created_dateList.add(created_date);
}
}
}
} catch (Exception e) {
Log.i(LOG_TAG, "Error" + e.toString());
}
});
thread.start();
try {
thread.join();
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
recyclerView.setLayoutManager(gridLayoutManager);
CustomAdapter_Tickets customAdapter = new CustomAdapter_Tickets(this, ticketIDList, issueList, statusList, created_dateList);
recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView
} catch (Exception e) {
Log.i(LOG_TAG, "Error" + e.toString());
}
}
}
CustomAdapter_Tickets.java
class CustomAdapter_Tickets extends RecyclerView.Adapter<CustomAdapter_Tickets.MyViewHolder> {
ArrayList<String> ticketIDList, issueList, statusList, created_dateList;
Context context;
public CustomAdapter_Tickets(Context context, ArrayList<String> ticketIDList, ArrayList<String> issueList,
ArrayList<String> statusList, ArrayList<String> created_dateList) {
this.context = context;
this.ticketIDList = ticketIDList;
this.issueList = issueList;
this.statusList = statusList;
this.created_dateList = created_dateList;
}
@NonNull
@Override
public CustomAdapter_Tickets.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// inflate the item Layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ticket_layout, parent, false);
return new CustomAdapter_Tickets.MyViewHolder(v);
}
@Override
public void onBindViewHolder(CustomAdapter_Tickets.MyViewHolder holder, final int position) {
// set the data in items
holder.txt_ticket_number.setText(String.format("ID: %s", ticketIDList.get(position)));
holder.txt_issue.setText(String.format("%s", issueList.get(position)));
holder.txt_status.setText(String.format("%s", statusList.get(position)));
holder.txt_created_date.setText(String.format("%s", created_dateList.get(position)));
}
@Override
public int getItemCount() {
return ticketIDList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView txt_ticket_number, txt_issue, txt_status, txt_created_date;
CardView card_view;
public MyViewHolder(View itemView) {
super(itemView);
txt_ticket_number = itemView.findViewById(R.id.txt_ticket_number);
txt_issue = itemView.findViewById(R.id.txt_issue);
txt_status = itemView.findViewById(R.id.txt_status);
txt_created_date = itemView.findViewById(R.id.txt_created_date);
card_view = itemView.findViewById(R.id.card_view);
}
}
}
ticket_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tickets_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:textSize="16sp">
<TextView
android:id="@+id/txt_ticket_number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_issue"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_status"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_created_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="?android:attr/listDivider" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
activity_ticket.xml
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>