I Am Trying to Implement RecyclerView With EditText. First I Am Fetching Members List From Database And then for each member I want to get value from user input(EditText).
But when I submit data, values for the members are getting shuffled.
I Am Using TextWatcher Class, When Text Get changed it adds value in the model class.
Values are getting shuffled when I scroll the RecyclerView.
I tried all the solutions on the stack overflow but none of it worked
following is my code:
For Fetching the data:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ProgressBar progressBar;
LinearLayoutManager manager;
String URL = "members_fetch.php?club_id=98";
List<MemberListDetails> memberListDetailsList = new ArrayList<>();
private LinearLayoutManager layoutManager;
MemberListDetails memberListDetails;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
Button button = findViewById(R.id.button);
recyclerView = findViewById(R.id.my_recycler_view);
manager = new LinearLayoutManager(this);
initRecyclerView();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String z = ((Adapter) recyclerView.getAdapter()).getValue().toString();
Log.wtf("aaa", z);
}
});
getMembersDetails();
}
private void initRecyclerView() {
layoutManager = new LinearLayoutManager(this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
}
private void getMembersDetails() {
final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Loading Members Details");
pd.show();
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
pd.dismiss();
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
Log.d("ssdf", "fsdf" + response);
JSONObject o = jsonArray.getJSONObject(i);
memberListDetails = new MemberListDetails();
memberListDetails.setStatus(o.getString("status"));
memberListDetails.setMembership_id(o.getString("membership_id"));
memberListDetails.setFirst_name(o.getString("first_name"));
memberListDetails.setMiddle_name(o.getString("middle_name"));
memberListDetails.setLast_name(o.getString("last_name"));
memberListDetails.setClub_post(o.getString("club_post"));
memberListDetails.setDistrict_post(o.getString("district_post"));
memberListDetails.setGender(o.getString("gender"));
memberListDetails.setPost_name(o.getString("post_name"));
memberListDetailsList.add(memberListDetails);
Log.d("hhhhh", "xxxx" + memberListDetailsList);
}
recyclerView.setAdapter(new Adapter(memberListDetailsList, MainActivity.this));
} catch (Exception e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("errorMsg", "dddd" + error.getMessage());
pd.dismiss();
}
});
queue.add(stringRequest);
}
}
For Adapter Class :
public class Adapter extends RecyclerView.Adapter<Adapter.VHolder> {
private List<MemberListDetails> data;
Context context;
EditText text;
Integer pos;
public Adapter(List<MemberListDetails> data, Context context) {
this.data = data;
this.context = context;
}
#NonNull
#Override
public Adapter.VHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item, viewGroup, false);
return new VHolder(view);
}
#Override
public int getItemCount() {
return data.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
public List<MemberListDetails> getItems() {
return data;
}
#Override
public void onBindViewHolder(#NonNull Adapter.VHolder vHolder, int i) {
vHolder.setIsRecyclable(false);
vHolder.textView.setText(data.get(i).getFirst_name());
text = vHolder.editText;
pos = i;
text.setText(data.get(i).getValue());
text.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// text.setError(null);
data.get(pos).setValue(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// text.setError(null);
data.get(pos).setValue(s.toString());
}
});
}
public class VHolder extends RecyclerView.ViewHolder {
TextView textView;
EditText editText;
public VHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_textView);
editText = itemView.findViewById(R.id.editText2);
}
}
String getValue() {
StringBuffer x = new StringBuffer();
for (MemberListDetails a : data) {
x.append(a.getValue()+",");
}
return x.substring(0,x.length()-1);
}
}
I think you need to remove global edit text and position. Also, add text watcher in holder class. Please check below code. it may help you.
public class Adapter extends RecyclerView.Adapter<Adapter.VHolder> {
private List<MemberListDetails> data;
Context context;
public Adapter(List<MemberListDetails> data, Context context) {
this.data = data;
this.context = context;
}
#NonNull
#Override
public Adapter.VHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item, viewGroup, false);
return new VHolder(view);
}
#Override
public int getItemCount() {
return data.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
public List<MemberListDetails> getItems() {
return data;
}
#Override
public void onBindViewHolder(#NonNull Adapter.VHolder vHolder, int i) {
vHolder.setIsRecyclable(false);
vHolder.textView.setText(data.get(i).getFirst_name());
vHolder.editText.removeTextChangedListener(vHolder.textWatcher);
vHolder.editText.setText(data.get(i).getValue());
vHolder.editText.addTextChangedListener(vHolder.textWatcher);
}
public class VHolder extends RecyclerView.ViewHolder {
TextView textView;
EditText editText;
TextWatcher textWatcher;
public VHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_textView);
editText = itemView.findViewById(R.id.editText2);
textWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// text.setError(null);
data.get(getAdapterPosition()).setValue(s.toString());
}
};
}
}
String getValue() {
StringBuffer x = new StringBuffer();
for (MemberListDetails a : data) {
x.append(a.getValue()+",");
}
return x.substring(0,x.length()-1);
}
Here is the solution. It may contain some error because I am not able to compile if due unavailable of resources like xml file and Model class But it definitely works for you. Because I have implemented the same in my project and working fine
public class Adapter extends RecyclerView.Adapter<Adapter.VHolder> {
private List<MemberListDetails> data;
Context context;
Integer pos;
public Adapter(List<MemberListDetails> data, Context context) {
this.data = data;
this.context = context;
}
#NonNull
#Override
public Adapter.VHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item, viewGroup, false);
return new VHolder(view);
}
#Override
public int getItemCount() {
return data.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
public List<String> getItems() {
return data;
}
#Override
public void onBindViewHolder(#NonNull Adapter.VHolder vHolder, int i) {
vHolder.setIsRecyclable(false);
vHolder.textView.setText(data.get(i).getFirst_name());
pos = i;
vHolder.myCustomEditTextListener.updatePosition(vHolder.getAdapterPosition());
vHolder.editText.setText(data.get(vHolder.getAdapterPosition()));
}
public class VHolder extends RecyclerView.ViewHolder {
TextView textView;
EditText editText;
public MyCustomEditTextListener myCustomEditTextListener;
public VHolder(View itemView, MyCustomEditTextListener myCustomEditTextListener) {
super(itemView);
textView = itemView.findViewById(R.id.tv_textView);
editText = itemView.findViewById(R.id.editText2);
this.editText.addTextChangedListener(myCustomEditTextListener);
}
}
String getValue() {
StringBuffer x = new StringBuffer();
for (MemberListDetails a : data) {
x.append(a.getValue() + ",");
}
return x.substring(0, x.length() - 1);
}
private class MyCustomEditTextListener implements TextWatcher {
private int position;
public void updatePosition(int position) {
this.position = position;
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// no op
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
data.set(position, charSequence.toString());
}
#Override
public void afterTextChanged(Editable editable) {
// no op
}
}
}
Related
So I have a RecyclerView consisting of EditTexts that uses an ArrayList as its data set, when the EditTexts are edited I want that ArrayList to be updated as well and I want to be able to use it in the Main Activity currently I've tried implementing a TextChangedListener however I have no clue how to get the data and transfer it to my list. Here's the code:
Main Activity:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
public ArrayList playerList;
private Button runItButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerList = new ArrayList();
for(int i = 0; i < 4; i++){
playerList.add("Player " + (i + 1));
}
runItButton = findViewById(R.id.buttonStartGame);
runItButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DrinktasksActivity.class);
intent.putExtra("playerList", playerList);
startActivity(intent);
}
});
recyclerView = findViewById(R.id.recyclyerViewNames);
MyAdapter myAdapter = new MyAdapter(this, playerList);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public void addPlayers(View view){
playerList.add("Player " + (playerList.size() + 1));
recyclerView.getAdapter().notifyDataSetChanged();
}
}
The adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
Context context;
ArrayList playerList;
public MyAdapter(Context ct, List players){
context = ct;
playerList = (ArrayList) players;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.enter_name, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
String current = playerList.get(position).toString();
holder.editText.setHint(current);
holder.editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// The code I assume should go here but I don't know how to implement
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public int getItemCount() {
return playerList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
EditText editText;
public ViewHolder(#NonNull View itemView) {
super(itemView);
editText = itemView.findViewById(R.id.editname);
}
}
}
All you have to do is to implement a custom callback to your activity with the index in which the edittext is edited and what is the new text.
Create a callback interface inside adapter
public interface TextChangeCallback {
void textChangedAt(int position, String text);
}
Pass the inteface implementation to Adapter via constructor
public MyAdapter(Context ct, List players,TextChangeCallback callback){
context = ct;
playerList = (ArrayList) players;
this.callback = callback;
}
Trigger callback to activity whenever you edit the text using TextWatcher
holder.editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.equals("")){
return;
}
data.set(position, String.valueOf(s));
callback.textChangedAt(position, String.valueOf(s));
}
#Override
public void afterTextChanged(Editable s) {
}
});
Add this interface implementation to your adapter
MyAdapter myAdapter = new MyAdapter(this, playerList, new MyAdapter.TextChangeCallback() {
#Override
public void textChangedAt(int position, String text) {
playerList.set(position, text);
Log.d("UPDATED LIST : ", String.valueOf(data));
}
});
recyclerView.setAdapter(myAdapter);
This will now update the ArrayList in adapter and activity as you edit the edittext from recyclerview.
You can find the complete implementation from this link
I'm using recycler view and I implement this functionality as per project requirement. But in my case item is highlighted but not unhighlighted again.
I was confused where I click the item for creating the clicking functionality, and how to unhighlighted the item. Please suggest me the right way to solve the code
public class LoadVehicleTypeAdapter extends RecyclerView.Adapter<LoadVehicleTypeAdapter.CarTypesHolder> {
private List<TaxiTypeResponse.Message> CarTypesModelsList;
private Context mContext;
VehicleTypeView vehicleTypeView;
setOnitemclick listener;
private SparseBooleanArray selectedItems = new SparseBooleanArray();
int I=-1;
public class CarTypesHolder extends RecyclerView.ViewHolder {
public CustomTextView mCarType;
public CircleImageView mCarTypeImage;
LinearLayout llRoot;
CardView cardView;
private SparseBooleanArray selectedItems = new SparseBooleanArray();
setOnitemclick listener;
public CarTypesHolder(final View view) {
super(view);
mCarType = (CustomTextView) view.findViewById(R.id.frag_cartypes_inflated_name);
mCarTypeImage = (CircleImageView) view.findViewById(R.id.frag_cartype_inflated_frameImage);
llRoot = (LinearLayout)view.findViewById(R.id.root1);
cardView=(CardView) view.findViewById(R.id.cardf);
}
public void setOnItemClickListner(setOnitemclick listener12) {
listener=listener12;
}
}
public void setOnItemClickListner(setOnitemclick listener12) {
listener=listener12;
}
public LoadVehicleTypeAdapter(Context context, List<TaxiTypeResponse.Message> CarTypesModelsList, VehicleTypeView vehicleTypeView) {
this.CarTypesModelsList = CarTypesModelsList;
mContext = context;
this.vehicleTypeView = vehicleTypeView;
}
#Override
public CarTypesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.frag_cartype_inflated_view, parent, false);
return new CarTypesHolder(itemView);
}
#SuppressLint("ResourceType")
#Override
public void onBindViewHolder(final CarTypesHolder holder, final int position) {
TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
I=CarTypesModelsList.get(position).getID();
holder.mCarType.setText(carTypesModel.getName());
holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
int color = Color.parseColor(PreferenceHandler.readString(mContext,PreferenceHandler.SECONDRY_COLOR,"#006fb6"));
holder.llRoot.setSelected(selectedItems.get(position, false));
holder.mCarType.setTextColor(color);
holder.setOnItemClickListner(listener);
holder. llRoot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
I=position;
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
notifyDataSetChanged();
}
});
if (I==position)
{
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
}
else
{
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#999999"));
}
Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);
}
#Override
public long getItemId(int position) {
return CarTypesModelsList.get(position).getID();
}
#Override
public int getItemCount() {
return CarTypesModelsList.size();
}
public void setSelection(LinearLayout imageView,CustomTextView textView,boolean value,int position){
if(value){
imageView.setBackgroundColor(Color.parseColor("#999999"));
textView.setTextColor(Color.parseColor("#FFFFFF"));
}else{
System.out.println("11111111111111111000000111111111111");
imageView.setBackgroundColor(Color.parseColor("#f3f3f3"));
textView.setTextColor(Color.parseColor("#2196F3"));
}
}
public interface setOnitemclick{
void ImageClick(int position, String Name,String Description,int id);
void ImageClickfade(int position, String Name,String Description,int id);
}
#Override
public int getItemViewType(int position) {
return position;
}
}
I'll use to update gradle
maven { url "https://maven.google.com" }
and update the code
public class LoadVehicleTypeAdapter extends RecyclerView.Adapter<LoadVehicleTypeAdapter.CarTypesHolder> {
private List<TaxiTypeResponse.Message> CarTypesModelsList;
private Context mContext;
VehicleTypeView vehicleTypeView;
int I=-1;
public class CarTypesHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public CustomTextView mCarType;
public CircleImageView mCarTypeImage;
LinearLayout llRoot;
CardView cardView;
setOnitemclick listener;
public void setOnItemClickListner(setOnitemclick listener)
{
this.listener=listener;
}
public CarTypesHolder(View view) {
super(view);
mCarType = (CustomTextView) view.findViewById(R.id.frag_cartypes_inflated_name);
mCarTypeImage = (CircleImageView) view.findViewById(R.id.frag_cartype_inflated_frameImage);
llRoot = (LinearLayout)view.findViewById(R.id.root1);
cardView=(CardView) view.findViewById(R.id.cardf);
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
listener.ImageClick(v,getAdapterPosition());
}
}
public LoadVehicleTypeAdapter(Context context, List<TaxiTypeResponse.Message> CarTypesModelsList, VehicleTypeView vehicleTypeView) {
this.CarTypesModelsList = CarTypesModelsList;
mContext = context;
this.vehicleTypeView = vehicleTypeView;
}
#Override
public CarTypesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.frag_cartype_inflated_view, parent, false);
return new CarTypesHolder(itemView);
}
#SuppressLint("ResourceType")
#Override
public void onBindViewHolder( final CarTypesHolder holder, int position) {
TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
holder.mCarType.setText(carTypesModel.getName());
holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
int color = Color.parseColor(PreferenceHandler.readString(mContext,PreferenceHandler.SECONDRY_COLOR,"#006fb6"));
holder.mCarType.setTextColor(color);
holder.setOnItemClickListner(new setOnitemclick() {
#Override
public void ImageClick(View v,int position1) {
I=position1;
notifyDataSetChanged();
System.out.println("11100011111");
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
}
});
if (I==position)
{
System.out.println("11100011111....");
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
}
else
{
System.out.println("11100011111----");
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);
}
#Override
public long getItemId(int position) {
return CarTypesModelsList.get(position).getID();
}
#Override
public int getItemCount() {
return CarTypesModelsList.size();
}
public void setSelection(LinearLayout imageView,CustomTextView textView,boolean value,int position){
if(value){
imageView.setBackgroundColor(Color.parseColor("#999999"));
textView.setTextColor(Color.parseColor("#FFFFFF"));
}else{
System.out.println("11111111111111111000000111111111111");
imageView.setBackgroundColor(Color.parseColor("#f3f3f3"));
textView.setTextColor(Color.parseColor("#2196F3"));
}
}
public interface setOnitemclick{
void ImageClick(View view,int position);
}
#Override
public int getItemViewType(int position) {
return position;
}
}
I have a RecyclerView Fragment to which I added a Filter Search in an edit text. It works, but when I click in a Card of the Recycler, it goes to the wrong detail. My best guess is that it's getting the wrong position from the getAdapterPosition since let's say I have this list {a,b,c,d,e,f,g,h}. when I filter and get two itmes left like {d,g}. If I click d it redirects to a, if I click g it redirects to b.
This is my RecyclerView
public class RecyclerProfile extends Fragment implements
Adapter.AdapterListener, com.example.cake.profiling.Adapter.SearchListener
{
private RecyclerListener recyclerListener;
private List<Profile> profiles = (new DAOProfile()).getProfile();
private Adapter recyclerAdapter= new Adapter(profiles, this);
public RecyclerProfile() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_recycler_profile,
container, false);
RecyclerView recyclerView = view.findViewById(R.id.recyclerProfile);
EditText editText = view.findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int
count) {
}
#Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new
LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerAdapter);
setHasOptionsMenu(true);
return view;
}
private void filter(String text){
ArrayList<Profile> filteredList = new ArrayList<>();
for (Profile profile: profiles){
if (profile.getName().toLowerCase().contains(text.toLowerCase())){
filteredList.add(profile);
}
}
recyclerAdapter.filterList(filteredList);
profiles = new ArrayList<>(filteredList);
}
#Override
public void listen(Profile profile, Integer position) {
recyclerListener.send(profile, position);
}
#Override
public void profileSelected(Profile profile) {
}
//INTERFACE
public interface RecyclerListener {
void send(Profile profile, Integer position);
}
//ON ATTACH
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.recyclerListener = (RecyclerListener) context;
}
}
This is my Adapter
public class Adapter extends RecyclerView.Adapter<Adapter.ProfileViewHolder> {
private List<Profile> profiles;
private AdapterListener adapterListener;
//CONSTRUCTOR
public Adapter(List<Profile> profiles,AdapterListener adapterListener) {
this.profiles = profiles;
this.adapterListener = adapterListener;
}
#NonNull
#Override
public Adapter.ProfileViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.card_profile, parent, false);
ProfileViewHolder profileViewHolder = new ProfileViewHolder(view);
return profileViewHolder;
}
#Override
public void onBindViewHolder(#NonNull Adapter.ProfileViewHolder holder, int position) {
Profile profile = profiles.get(position);
holder.setter(profile);
}
#Override
public int getItemCount() {
return profiles.size();
}
public void filterList (ArrayList<Profile> filteredList){
profiles = filteredList;
notifyDataSetChanged();
}
//VIEWHOLDER
class ProfileViewHolder extends RecyclerView.ViewHolder{
private ImageView image;
private TextView name;
public ProfileViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.nameProfile);
image = itemView.findViewById(R.id.imageProfile);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Profile profile = profile.get(getAdapterPosition());
adapterListener.receive(profile, getAdapterPosition());
}
});
}
public void setter (Profile profile){
name.setText(profile.getName());
image.setImageResource(profile.getImage());
}
}
public interface AdapterListener {
void receive(Profile profile, Integer position);
}
}
getAdapterPosition()
this will return the position of the item of current data set. This is what I'd do
class ProfileViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView image;
private TextView name;
Profile mProfile;
public ProfileViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.nameProfile);
image = itemView.findViewById(R.id.imageProfile);
name.setOnClickListener(this);
image.setOnClickListener(this);
}
public void setter (Profile profile){
if(profile != null) {
mProfile = profile;
name.setText(mProfile.getName());
image.setImageResource(mProfile.getImage());
}
}
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.item_nameProfile:
case R.id.item_imageProfile:
for(int i = 0 ; i < profiles.size() ; i++) {
if(profiles.get(i).getName().equalsIgnoreCase(mProfile.getName()) {
adapterListener.receive(profiles.get(i), i);
break;
}
}
break;
}
}
public interface AdapterListener {
void receive(Profile profile, Integer position);
}
}
I have a RecyclerView with 5 items and drag & drop feature. I want to save items's positions into SharedPreferences in order to restore them when the App is launched. Currently I'm able to save their positions into SharedPreferences using Json but the App crashes when I restore positions. Any solution? Thanks.
public static AppCompatActivity mActivity;
public static SharedPreferences SP;
public static SharedPreferences.Editor mEditor;
public static RecyclerView mRecyclerView;
public static RecyclerViewDragDropManager mRecyclerViewDragDropManager;
public static List<Long> mSaveItemsOrder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mEditor = SP.edit();
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerViewDragDropManager = new RecyclerViewDragDropManager();
mRecyclerViewDragDropManager.setInitiateOnMove(false);
mRecyclerViewDragDropManager.setInitiateOnLongPress(true);
mRecyclerViewDragDropManager.setLongPressTimeout(300);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mRecyclerViewDragDropManager.createWrappedAdapter(new Adapter()));
mRecyclerViewDragDropManager.attachRecyclerView(mRecyclerView);
}
public static class Item {
public final long id;
public final String text;
public Item(long id, String text) {
this.id = id;
this.text = text;
}
}
public static class ViewHolder extends AbstractDraggableItemViewHolder {
Button mButton;
public ViewHolder(View itemView) {
super(itemView);
mButton = (Button) itemView.findViewById(R.id.button);
}
}
public static class Adapter extends RecyclerView.Adapter<ViewHolder> implements DraggableItemAdapter<ViewHolder> {
List<Item> mItems;
public Adapter() {
setHasStableIds(true);
mItems = new ArrayList<>();
if (SP.getString("items_order", null) == null) {
for (int mInt = 0; mInt < 5; mInt++) {
mItems.add(new Item(mInt, "Item " + mInt));
}
} else {
mItems = new Gson().fromJson(SP.getString("items_order", null), new TypeToken<Item>(){}.getType());
}
}
#Override
public long getItemId(int position) {
return mItems.get(position).id;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view, parent, false);
return new ViewHolder(mView);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Item mItem = mItems.get(position);
holder.mButton.setText(mItem.text);
}
#Override
public int getItemCount() {
return mItems.size();
}
#Override
public void onMoveItem(int fromPosition, int toPosition) {
Item mMovedItem = mItems.remove(fromPosition);
mItems.add(toPosition, mMovedItem);
notifyItemMoved(fromPosition, toPosition);
}
#Override
public boolean onCheckCanStartDrag(ViewHolder holder, int position, int x, int y) {
return true;
}
#Override
public ItemDraggableRange onGetItemDraggableRange(ViewHolder holder, int position) {
return null;
}
#Override
public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}
}
#Override
protected void onPause() {
super.onPause();
mSaveItemsOrder = new ArrayList<>();
for (int mInt = 0; mInt < mRecyclerView.getAdapter().getItemCount(); mInt++) {
mSaveItemsOrder.add(mRecyclerView.getAdapter().getItemId(mInt));
}
mEditor.putString("items_order", new Gson().toJson(mSaveItemsOrder)).commit();
}
This is the error:
https://www.dropbox.com/s/j2tf4n08depyynt/Error.txt?dl=0
The problem is with your JSON.
You the object you are serializing to JSON is mSaveItemsOrder which is an ArrayList. But on deserializing you give GSON the Class new TypeToken<Item>(){}.getType().
So you store an ArrayList in your JSON but trying to deserialzie a different Class, that won't work.
This should work:
mItems = new Gson().fromJson(SP.getString("items_order", null), new TypeToken<ArrayList>(){}.getType(););
i have a recyclerView with two sections regular and Favourites and for populating my RecyclerView i have List<Object> my object class :
public class Object {
String id,channelName;
boolean isFavorite;
}
now i want to put the values which have isfavorite == true to top of my recyclerView inside my Favourite section but i don't know where to start or exactly what to do ? do i have to sort the List<Object> with booleans ? if anybody can give me a little hint or guidance then it'll be so helpful for me,
please this image for better understanding ,
this is what i'm trying to get
i added the Section in my RecyclerView by SimpleSectionedRecyclerViewAdapter
class DataModal
{
public String title;
public boolean isFavourite;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isFavourite() {
return isFavourite;
}
public void setIsFavourite(boolean isFavourite) {
this.isFavourite = isFavourite;
}
}
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> {
private final Context mContext;
private List<DataModal> mData;
public void add(DataModal s,int position) {
position = position == -1 ? getItemCount() : position;
mData.add(position,s);
notifyItemInserted(position);
}
public void remove(int position){
if (position < getItemCount() ) {
mData.remove(position);
notifyItemRemoved(position);
}
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
public final TextView title;
public SimpleViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.simple_text);
}
}
public SimpleAdapter(Context context, ArrayList<DataModal> data) {
mContext = context;
if (data != null)
mData = data;
else mData = new ArrayList<DataModal>();
}
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(mContext).inflate(R.layout.simple_item, parent, false);
return new SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {
DataModal data = mData.get(position);
holder.title.setText(data.getTitle());
holder.title.setOnClickListener(new Listener(position,mData.get(position)));
}
class Listener implements View.OnClickListener
{
DataModal Data;
int position;
Listener(int position,DataModal Data)
{
this.Data = Data;
this.position = position;
}
#Override
public void onClick(View view) {
if(Data.isFavourite())
{
//mark the view as unfavorite
}
else{
//mark the view as favorite
}
Toast.makeText(mContext,"Position ="+position,Toast.LENGTH_SHORT).show();
}
}
#Override
public int getItemCount() {
return mData.size();
}
}
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> {
private final Context mContext;
private List<String> mData;
public void add(String s,int position) {
position = position == -1 ? getItemCount() : position;
mData.add(position,s);
notifyItemInserted(position);
}
public void remove(int position){
if (position < getItemCount() ) {
mData.remove(position);
notifyItemRemoved(position);
}
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
public final TextView title;
public SimpleViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.simple_text);
}
}
public SimpleAdapter(Context context, String[] data) {
mContext = context;
if (data != null)
mData = new ArrayList<String>(Arrays.asList(data));
else mData = new ArrayList<String>();
}
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(mContext).inflate(R.layout.simple_item, parent, false);
return new SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {
holder.title.setText(mData.get(position));
holder.title.setOnClickListener(new Listener(position,mData.get(position),false,favview));
}
class Listener implements View.OnClickListener
{
ImageView favoriteView;
int position;
Listener(int position,String Data,boolean isFavourite,ImageView favoriteView)
{
this.favoriteView = favoriteView;
this.position = position;
}
#Override
public void onClick(View view) {
if(isFavourite)
{
//mark the view as unfavorite
}
else{
//mark the view as favorite
}
Toast.makeText(mContext,"Position ="+position,Toast.LENGTH_SHORT).show();
}
}
#Override
public int getItemCount() {
return mData.size();
}
}