Android Studio - How to change you Category Name in adapter class?
In an adapter class, you can change the category by modifying the data stored in the adapter's dataset and then notifying the adapter that the data has changed. For example, if you have a list of categories stored in an ArrayList, you can update the category at a particular position using the set()
method and then call the notifyItemChanged()
method on the adapter to update the view.
To change the category in the adapter, you can create a method that takes the new category data as input and updates the dataset. This method can be called from the activity or fragment that uses the adapter, in response to user input or other events. Once the dataset has been updated, the adapter can be notified to refresh the view.
In summary, changing the category in an adapter involves updating the data in the dataset and then notifying the adapter of the change. By following this approach, you can dynamically update the view in response to user input and keep your UI up-to-date with the latest data.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<soup.neumorphism.NeumorphCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.Neumorph.CardView">
<EditText
android:id="@+id/edtPTitleName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="পণ্যের নাম"
android:padding="14dp"
android:paddingEnd="10dp"
android:layout_weight="1"
android:fontFamily="@font/g3f"
android:textColor="@color/black"
android:textSize="16sp" />
</soup.neumorphism.NeumorphCardView>
</LinearLayout>
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.AuthFailureError;
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.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.quaintfantasies.ohisgallery.AdminPanel.CategoryEditDelete.CategoryWise_ProductList;
import com.quaintfantasies.ohisgallery.NavigationBar.NavigationModel;
import com.quaintfantasies.ohisgallery.ProductsFolder.ProductModel;
import com.quaintfantasies.ohisgallery.R;
import com.quaintfantasies.ohisgallery.VolleySingleton;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import soup.neumorphism.NeumorphButton;
public class CategoryAdminAdapter extends RecyclerView.Adapter<CategoryAdminAdapter.ViewHolder> {
List<NavigationModel> navigationModelsList;
Context mCtx;
public CategoryAdminAdapter(List<NavigationModel> navigationModelsList, Context mCtx) {
this.navigationModelsList = navigationModelsList;
this.mCtx = mCtx;
}
@NonNull
@Override
public CategoryAdminAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.admin_item_layout,viewGroup,false);
return new CategoryAdminAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CategoryAdminAdapter.ViewHolder viewHolder, int pos) {
NavigationModel model = navigationModelsList.get(pos);
viewHolder.categoryName.setText(model.getNavigation_name());
Picasso.get().load(model.getNavigation_icon_link()).into(viewHolder.categoryicon);
viewHolder.categoryicon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(pos !=0){
Intent categoryWiseProList = new Intent(mCtx, CategoryWise_ProductList.class);
categoryWiseProList.putExtra("PTitleName", model.getNavigation_name());
mCtx.startActivity(categoryWiseProList);
}
}
});
viewHolder.navigation_Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(mCtx);
builder.setTitle("Edit your category ");
builder.setMessage("Do you want to change the category?");
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditNavigation(model);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
public void EditNavigation (NavigationModel navigationModel){
String url = "https://quaintfantasies.com/ohisg/vlink/editnavcategory.php";
AlertDialog.Builder builder = new AlertDialog.Builder(mCtx);
builder.setTitle("Edit Navigation");
EditText edtPTitleNames;
View view = LayoutInflater.from(mCtx).inflate(R.layout.admin_edit_nav_category_item, null);
edtPTitleNames = view.findViewById(R.id.edtPTitleName);
edtPTitleNames.setText(navigationModel.getNavigation_name());
builder.setView(view);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = edtPTitleNames.getText().toString();
String pNavId = navigationModel.getPItemID();
// Create a request queue
final ProgressDialog progressDialog = new ProgressDialog(mCtx);
progressDialog.setMessage("দয়াকরে কিছুক্ষণ অপেক্ষা করুন। ধন্যবাদ");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
Toast.makeText(mCtx, "আপলোর্ড করা হয়েছে, ধন্যবাদ", Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(mCtx, "আপনার ইন্টারনেট সংযোগটি হইতো বিছন্ন আছে, দয়া করে আবার চেষ্টার করুন। ধন্যবাদ", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("PTitleName", name);
params.put("PItemID", pNavId);
return params;
}
};
VolleySingleton.getInstance(mCtx).addToRequestQueue(stringRequest);
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
@Override
public int getItemCount() {
return navigationModelsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView categoryName;
private ImageView categoryicon,navigation_Edit;
public ViewHolder(@NonNull View itemView) {
super(itemView);
categoryicon = (ImageView) itemView.findViewById(R.id.navigation_icon);
categoryName = itemView.findViewById(R.id.navigation_name);
navigation_Edit = itemView.findViewById(R.id.navigation_Edit);
}
}
}
public class NavigationModel {
String PItemID, navigation_icon_link,navigation_name;
public NavigationModel() {
}
public NavigationModel(String PItemID, String navigation_icon_link, String navigation_name) {
this.PItemID = PItemID;
this.navigation_icon_link = navigation_icon_link;
this.navigation_name = navigation_name;
}
public String getPItemID() {
return PItemID;
}
public void setPItemID(String PItemID) {
this.PItemID = PItemID;
}
public String getNavigation_icon_link() {
return navigation_icon_link;
}
public void setNavigation_icon_link(String navigation_icon_link) {
this.navigation_icon_link = navigation_icon_link;
}
public String getNavigation_name() {
return navigation_name;
}
public void setNavigation_name(String navigation_name) {
this.navigation_name = navigation_name;
}
}