Skip to main content

Android RecyclerView Tutorial - Fetching JSON

First add dependencies to android file 

Add recyclerview library in build.gradle file

implementation 'androidx.recyclerview:recyclerview:1.1.0'

Step 2
activity_complaints.xml
This XML file holds recyclerview component
<?xml version="1.0" encoding="utf-8"?>

<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/rvComplaintsList"
>

</androidx.recyclerview.widget.RecyclerView>

Step 3
This will contain Components to be repeated via RecyclerView.
complainsts_list_layout.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgIcon"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
android:layout_gravity="center"
android:id="@+id/txtTitle"
android:text="@string/app_name"
android:paddingLeft="16sp"/>

</LinearLayout>

Step 4
ComplaintsActivity.java 
This will hold different tasks related to RecyclerView
public class ComplaintsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaints);
RecyclerView complaintsList = (RecyclerView) findViewById(R.id.rvComplaintsList);
complaintsList.setLayoutManager(new LinearLayoutManager(this));
String[] complaint_type = {"Mechanical Fault", "Electrical Fault","Water Supply"};
complaintsList.setAdapter(new ComplaintsAdapter(complaint_type));
}
}
Step 5
Add Class ComplaintsAdapter.java
This is the main class. This is to include View Holder & Adapter Component
The Class will look like this
package in.xxxx.xxxxxx;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import org.w3c.dom.Text;

public class ComplaintsAdapter extends RecyclerView.Adapter<ComplaintsAdapter.ComplaintsViewHolder> {

private String[] data;
public ComplaintsAdapter(String[] data){
this.data = data;
}
@NonNull
@Override
public ComplaintsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.complaints_list_layout,parent,false);
return new ComplaintsViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ComplaintsViewHolder holder, int position) {
String title = data[position];
holder.txtTitle.setText(title);

}

@Override
public int getItemCount() {
return data.length;
}

public class ComplaintsViewHolder extends RecyclerView.ViewHolder {
ImageView imgIcon;
TextView txtTitle;
public ComplaintsViewHolder(@NonNull View itemView) {
super(itemView);
imgIcon = (ImageView) itemView.findViewById(R.id.imgIcon);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}
}
}

This RecyclerView Tutorial worth a read as it helped me to learn this staff pretty easily.

Comments

Popular posts from this blog

How to enable pinch zoom on a web view loading a url with pdf(Android)?

How to enable pinch zoom on a web view loading a url with pdf(Android)? Ref: https://stackoverflow.com/questions/16630382/how-to-enable-pinch-zoom-on-a-web-view-loading-a-url-with-pdfandroid   WebView mWebView = (WebView) findViewById(R.id.webView);         mWebView.getSettings().setJavaScriptEnabled( true ); WebSettings webSettings = mWebView.getSettings(); webSettings.setBuiltInZoomControls( true ); webSettings.setSupportZoom( true );

[SOLVED] Is Android Studio supported on Mac OS 10.15 (Catalina)? Android Studio Not Working on MAC

 Technically Yes! But not that all versions work the same way. While I upgraded to Android Studio to Version 4.1.2. But it did not work in OS 10.15. I deleted my previous android installations following this guide , but still nothing seemed to work. The hell bent installer seemed to get stuck in the middle. I contacted few experts who also appear clueless. A lot of time in my developers' life, I prompt to self - "Read Sid, Read!'. This was no different this time. Instead of searching for help online / offline, I started reading the Android Studio Release Note for version 4.1.2 . It clearly says ,  System Requirements  For MAC:  Mac® OS X® 10.10 (Yosemite) or higher, up to 10.14 (macOS Mojave) So, it may not work with MAC OS Catalina anyway. And it did not work as in my case. Strangely, my previous Android Version 4.0 worked. It is bit tricky to find old releases. But here is where you can find old Android Studio Archives . I downloaded Android Version 4.0, and it wor...

How to Align Views at The Bottom of The Screen

This is h ow you can Align Views at The Bottom of The Screen You know, you have to go to layout_main.xml file (If this is the layout you want to tweak with). Here we add a LinearLayout Component. Then place a TextView Component within that liner layout. Most important note: Add android:gravity="bottom" in the LinearLayout Component Add  android :gravity ="center_horizontal" in the TextView Component You are done! Here goes your SAMPLE code. < LinearLayout android :layout_width ="match_parent" android :layout_height ="match_parent" android :gravity ="bottom" android :orientation ="horizontal" > < TextView android :id ="@+id/bottomText" android :layout_width ="match_parent" android :padding ="2dp" android :textSize ="18sp" android :layout_height ="wrap_content" android :background ="@color/cardview_...