How to make Dynamic Recycle view In android using Volley Library Get Mothord
As you Learn the Previous Tutorial What is RecycleView And How to Make Static RecycleView in our Project In android App Now Hears We Are Learn How to Make Dynamic Recycle View In android And Alos Learn to Hear JSON Parsing.
Step 1: Copy Volley Library Dependancy and Senglation Class Our Previous Tutorial.
Step 2: Now Firs Add XML file in Recycle View And This Dependency but Now Version Android Studio Only Required Google Degine Material Library
implementation 'com.google.android.material:material:1.2.0'
Step 3: Now Add Recycle View In Android App,
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/itemrecycylemen"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>Step 4: Now Make Recycle View Layout Degine
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/homelayoute"
app:cardCornerRadius="10dp"
android:layout_margin="5dp"
android:background="@drawable/productebackground"
android:layout_width="match_parent"
android:layout_height="300dp"
android:padding="2dp">
<LinearLayout
android:orientation="vertical"
android:background="@drawable/productebackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_weight="2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginBottom="5dp"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:background="@drawable/buttonback"
app:cardCornerRadius="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:scaleType="fitXY"
android:id="@+id/homrproductimg"
android:src="@drawable/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center_vertical"
android:layout_gravity="center"
android:layout_weight="1"
android:id="@+id/homeproductname"
android:text="@string/ITEMNAME"
android:textSize="15dp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:layout_gravity="end"
android:layout_width="28dp"
android:layout_height="28dp"
android:id="@+id/mostlikepost"
android:src="@drawable/vector_heart_red"
/>
<!-- <LinearLayout-->
<!-- android:orientation="horizontal"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content">-->
<!-- <TextView-->
<!-- android:text="Available in:"-->
<!-- android:textSize="18dp"-->
<!-- android:textColor="@color/black"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- <TextView-->
<!-- android:layout_marginLeft="5dp"-->
<!-- android:text="1"-->
<!-- android:textSize="18dp"-->
<!-- android:textColor="@color/black"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- </LinearLayout>-->
<!-- <LinearLayout-->
<!-- android:background="@drawable/buttonback"-->
<!-- android:gravity="center"-->
<!-- android:layout_gravity="end"-->
<!-- android:orientation="horizontal"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="30dp">-->
<!-- <TextView-->
<!-- android:id="@+id/addclick"-->
<!-- android:textStyle="bold"-->
<!-- android:background="@drawable/buttonback"-->
<!-- android:text="Add"-->
<!-- android:gravity="center"-->
<!-- android:textSize="15dp"-->
<!-- android:textColor="@color/White"-->
<!-- android:layout_width="50dp"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- <Button-->
<!-- android:id="@+id/decrement"-->
<!-- android:background="@drawable/buttonback"-->
<!-- android:visibility="gone"-->
<!-- android:text="-"-->
<!-- android:textSize="18dp"-->
<!-- android:textStyle="bold"-->
<!-- android:textColor="@color/White"-->
<!-- android:layout_width="30dp"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- <TextView-->
<!-- android:id="@+id/addvalue"-->
<!-- android:visibility="gone"-->
<!-- android:layout_marginLeft="5dp"-->
<!-- android:text="0"-->
<!-- android:textSize="18dp"-->
<!-- android:textColor="@color/White"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- <Button-->
<!-- android:id="@+id/increment"-->
<!-- android:textStyle="bold"-->
<!-- android:background="@drawable/buttonback"-->
<!-- android:text="+"-->
<!-- android:textSize="18dp"-->
<!-- android:textColor="@color/White"-->
<!-- android:layout_width="30dp"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- </LinearLayout>-->
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="20dp">
<ImageView
android:layout_gravity="center"
android:layout_width="12dp"
android:layout_height="12dp"
android:src="@drawable/rupy"
/>
<TextView
android:layout_weight="2"
android:textStyle="bold"
android:gravity="start|center_vertical"
android:text="390"
android:textSize="15dp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:background="@drawable/substract"
android:layout_weight="2"
android:gravity="start|center_vertical"
android:text="120"
android:textSize="12dp"
android:singleLine="true"
android:textColor="@color/gray"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:gravity="start|center_vertical"
android:text="(Included Tex)"
android:textSize="12sp"
android:textColor="@color/gray"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>Now We are create Adopter Class in app
package com.vivid.myshop.home;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.vivid.myshop.R;
import com.vivid.myshop.fullcontantview.fullitemview;
import java.util.ArrayList;
import static com.vivid.myshop.R.id.homrproductimg;
public class homeAdopter extends RecyclerView.Adapter<homeAdopter.MyViewHolder> {
ArrayList personNames;
ArrayList personImages;
Activity fragmentActivity;
homeItem[] homeItem;
public static final String SHARED_PREF_NAME = "bucket";
int a=0;
// private Clickebucketvalue clickebucketvalue;
public homeAdopter(Activity fragmentActivity, homeItem[] homeItem) {
this.fragmentActivity=fragmentActivity;
this.homeItem=homeItem;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.recycyleviewitem,parent,false);
return new homeAdopter.MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
final homeItem homeitem= homeItem[position];
holder.productname.setText(homeItem[position].getProductName());
holder.productImage.setImageResource(homeItem[position].getImageurl());
// holder.itemView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent intent=new Intent(fragmentActivity, fullitemview.class);
// fragmentActivity.startActivity(intent);
// }
//});
}
@Override
public int getItemCount() {
return homeItem.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView productname,addclick;
ImageView productImage;
CardView homelayout;
TextView peoducevalue;
// Button increment,decrement;
// final SharedPreferences sharedPreferences1 = fragmentActivity.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
// public static final String b=" 0";
public MyViewHolder(@NonNull View itemView) {
super(itemView);
productImage=itemView.findViewById(homrproductimg);
productname=itemView.findViewById(R.id.homeproductname);
homelayout=itemView.findViewById(R.id.homelayoute);
peoducevalue=itemView.findViewById(R.id.addvalue);
// increment=itemView.findViewById(R.id.increment);
// decrement=itemView.findViewById(R.id.decrement);
// addclick=itemView.findViewById(R.id.addclick);
//
// increment.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// addclick.setVisibility(View.GONE);
// decrement.setVisibility(View.VISIBLE);
// a=a+1;
// if(a>=0)
// {
// sharedprefences();
// //Creating editor to store values to shared preferences
// peoducevalue.setVisibility(View.VISIBLE);
// // Toast.makeText(fragmentActivity,"Button clicked first time!"+a, Toast.LENGTH_LONG).show();
// peoducevalue.setText(""+a);
// }
// else
// {
// }
// }
// });
// decrement.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// a=a-1;
// if(a>=0){
// peoducevalue.setText(""+a);
// sharedprefences();
// if(a==0){
// // peoducevalue.setText(""+0);
// decrement.setVisibility(View.GONE);
// peoducevalue.setVisibility(View.GONE);
// // peoducevalue.setText(""+a);
// addclick.setVisibility(View.VISIBLE);
// }
// }
// else{
// Toast.makeText(fragmentActivity,"No Item Added In Card", Toast.LENGTH_LONG).show();
// decrement.setVisibility(View.GONE);
// peoducevalue.setVisibility(View.GONE);
// addclick.setVisibility(View.VISIBLE);
// }
//
// }
// });
}
}
// public interface Clickebucketvalue{
// void onClicked(homeItem item );
// }
}Now Create Model Class IN app
package com.vivid.myshop.home;
public class homeItem {
private String ProductName;
private String PeoducetId;
private String ProductColor;
private String ProductSize;
private int Imageurl;
private String ProductDiscraption;
public homeItem(String productName, String peoducetId, String productColor, String productSize, int imageurl, String productDiscraption) {
ProductName = productName;
PeoducetId = peoducetId;
ProductColor = productColor;
ProductSize = productSize;
Imageurl = imageurl;
ProductDiscraption = productDiscraption;
}
public String getProductName() {
return ProductName;
}
public void setProductName(String productName) {
ProductName = productName;
}
public String getPeoducetId() {
return PeoducetId;
}
public void setPeoducetId(String peoducetId) {
PeoducetId = peoducetId;
}
public String getProductColor() {
return ProductColor;
}
public void setProductColor(String productColor) {
ProductColor = productColor;
}
public String getProductSize() {
return ProductSize;
}
public void setProductSize(String productSize) {
ProductSize = productSize;
}
public int getImageurl() {
return Imageurl;
}
public void setImageurl(int imageurl) {
Imageurl = imageurl;
}
public String getProductDiscraption() {
return ProductDiscraption;
}
public void setProductDiscraption(String productDiscraption) {
ProductDiscraption = productDiscraption;
}
}Now We Are Create Main Class IN app
package com.bspl.suriyasareeparselldelevery;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);recyclerView=findViewById(R.id.itemrecycylemen);
}registerClass( ) ;
public void registerClass( ) {
progressDoalog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, "Your Api Key",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject object=new JSONObject(response);
JSONArray jsonArray=object.getJSONArray("saleVDetails");
// for (int i=0;i<=jsonArray.length();i++) {// json = jsonArray.getJSONObject(i);
//
// modelclassdobject(json);
//
// }
//
} catch (Exception e) {
// e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDoalog.dismiss();
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Log.e("responce",error.toString());
} else if (error instanceof AuthFailureError) {
Log.e("responce",error.toString());
} else if (error instanceof ServerError) {
Log.e("responce",error.toString());
} else if (error instanceof NetworkError) {
Log.e("responce",error.toString());
} else if (error instanceof ParseError) {
Log.e("responce",error.toString());
}
Log.e("responce",error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
return params;
}
};
VolleySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
}
private void modelclassdobject(JSONObject json) {
try {
modelClasses.add(new proModelclass(
json.getString("product"),
json.getString("decription"),
json.getString("barcode"),
json.getString("hsnCode"),
json.getString("purchase"),
json.getString("mrp"),
json.getString("qty")
));
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());recyclerView.setLayoutManager(linearLayoutManager);
homeAdopter customAdapter = new homeAdopter(getActivity(), homeItem);
recyclerView.setAdapter(customAdapter);
}}
Comments
Post a Comment