ListView in android with api and volley Network Library in android Studio.

 


In a previous tutorial, you are learning what is a listview and how to make listview static in Android Studio. 
Now we are learning How to make Dynamic Listview in android  Using Network library and restful API.
In this tutorial, I am using Volley Network Library.
Step 1: make a MainActivity.xml file in the project and add Listview in the file just like this XML code. 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/container"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

Step 2: we Are Implement dependencies In the android project build.guide file as it is copied and paste it.

implementation 'com.android.volley:volley:1.0.0'

step 3: Now making VolleySingleton class in your project and copies this code and paste as it is.

package com.example.schoolapp;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;


public class VolleySingleton {
private static VolleySingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;

private VolleySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}

public static synchronized VolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleySingleton(context);
}
return mInstance;
}

public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
}

Step 4: Now Start coding MainActivity. java file in your project in the android app.
Note: in a MainActivity.java file use only useful code and remove extra code in this file

package com.example.schoolapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.schoolapp.Adapter.CreateFeeTypeAdapter;
import com.example.schoolapp.Fragments.DepositeStuFee;
import com.example.schoolapp.Fragments.SectionFragment;
import com.example.schoolapp.Model.CreateFeeTypeModel;

import org.json.JSONArray;
import org.json.JSONObject;

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

public class MainActivity extends AppCompatActivity {
ProgressDialog progressDoalog;

List<CreateFeeTypeModel> createFeeTypeModelList;
ListView list;
ArrayAdapter<String> adapter;
ArrayList<String> listoffeetype;
ArrayList<String> listoffeeid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
list=findViewById(R.id.list);
createFeeTypeModelList=new ArrayList<>();
listoffeetype=new ArrayList<>();
listoffeeid=new ArrayList<>();

progressDoalog=new ProgressDialog(this);
LoadFeeTypeDetails();


}

private void LoadFeeTypeDetails() {
//getting the progressbar

//making the progressbar visible
progressDoalog.setMessage("Loading....");
progressDoalog.setTitle("Fetch Data");
progressDoalog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDoalog.show();

//creating a string request to send request to the urland this is only get methord Request code
StringRequest stringRequest = new StringRequest(Request.Method.GET, "Put your API Key ",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//hiding the progressbar after completion
progressDoalog.dismiss();


try {

//getting the whole json object from the response
JSONObject obj = new JSONObject(response);
if(obj.getBoolean("IsSuccess")) {
adapter=null;
listoffeeid.clear();
listoffeetype.clear();
JSONArray stuarray = obj.getJSONArray("ResponseData");

//
// //now looping through all the elements of the json array
createFeeTypeModelList.clear();

for (int i = 0; i < stuarray.length(); i++) {
//getting the json object of the particular index inside the array
JSONObject studentObject = stuarray.getJSONObject(i);
try {
createFeeTypeModelList.add(new CreateFeeTypeModel(
studentObject.getString("Id"),
studentObject.getString("FeeType1"),
studentObject.getBoolean("IsActive"),
studentObject.getBoolean("IsDeleted")));
listoffeetype.add( studentObject.getString("FeeType1")) ;
listoffeeid.add( studentObject.getString("Id")) ;
}
catch (Exception e){
createFeeTypeModelList.add(new CreateFeeTypeModel(
studentObject.getString("Id"),
"null",
true,
false));
}

}
adapter=new ArrayAdapter<>(UpdateStudentFee.this,android.R.layout.simple_list_item_1,listoffeetype);
list.setAdapter(adapter);


// UpadateFee();
}


// Log.d("ashishsikarwar",response);


} catch (Exception e) {

e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDoalog.dismiss();

//displaying the error in toast if occurrs
Toast.makeText(UpdateStudentFee.this, "Your Internet Connection is Failed ", Toast.LENGTH_SHORT).show();

}
});

//creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(UpdateStudentFee.this);

//adding the string request to request queue
requestQueue.add(stringRequest);
}


}
step 5: creat a model class 
package com.example.schoolapp.Model;

public class CreateFeeTypeModel {


public String getID() {
return ID;
}

public String getFeeType1() {
return FeeType1;
}

public boolean isActive() {
return IsActive;
}

public boolean isDeleted() {
return IsDeleted;
}



public CreateFeeTypeModel(String ID, String feeType1, boolean isActive, boolean isDeleted) {
this.ID = ID;
FeeType1 = feeType1;
IsActive = isActive;
IsDeleted = isDeleted;
}
String ID;
String FeeType1;
boolean IsActive;
boolean IsDeleted;
}

final Step: Run this code in your project if any problem in this code comment this and I solve your problem   

  

Comments

Popular posts from this blog

How to Integrate or Work with Open Street Map (OSM) in an Android App (Kotlin)

how to implement OpenStreetMap in android