I have a RecyclerView
with data from local JSON
in CardView
. I need to implement on selected items when one or some item clicked(change background the item selected or highlight) (like edit in Line App ) but without Button or longpress
. But I don't want to use StateListDrawable
or (using XML) because I have some JSON
data which need to process later.
I need a state in my Activity like a boolean value or something to save every item which I clicked but I don't have any solution again. I have read and try some tutorial but it's not working. This is below my Activity now :
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
@Override
public void OnRecyclerViewItemClicked(int position) {
boolean selectedItem = false;
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
@Override
public void OnRecyclerViewItemClicked(int position) {
/* ------ boolean variabel for adapter but still not work ----- */
JSONObject filteredtableList= null;
try {
filteredtableList= new JSONObject("response").getJSONObject("tTisch");
}
catch (JSONException e) {
e.printStackTrace();
}
if (recyclerView == null) {
try {
filteredtableList.has("true");
filteredtableList.put(status, true);
} catch (JSONException e) {
e.printStackTrace();
}
}
else {
try {
filteredtableList.put(status, true);
}
catch (JSONException e) {
e.printStackTrace();
}
}
adapter.updateData(filteredTableList);
//----------------------------------------------------
try {
Toast.makeText(TableActivity.this, filteredTableList.getJSONObject(position).getString("tischnr"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Adapter.java
public int lastCheckedPosition = -1;
.........
.........
.........
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position == lastCheckedPosition) {
holder.itemView.setBackgroundResource(R.color.colorRedTableOcc);
} else {
holder.itemView.setBackgroundResource(R.color.colorTableGreen);
}
try {
currItem = list.getJSONObject(position);
holder.txt_no_table.setText(currItem.getString("tischnr"));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return list.length();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
}
@Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
lastCheckedPosition = getAdapterPosition();
notifyItemRangeChanged(0,list.length());
}
}
JSON.json
..............
"t-tisch": [
{
"tischnr": 1,
"departement": 1,
"normalbeleg": 0,
"kellner-nr": 0,
"bezeich": "TABLE 01",
"roomcharge": false,
"betriebsnr": 0
},
{
"tischnr": 2,
"departement": 1,
"normalbeleg": 0,
"kellner-nr": 0,
"bezeich": "TABLE 02",
"roomcharge": false,
"betriebsnr": 0
},
............
This is my Activity looks like
Output now:
It's now highlight when I click the item (Thanks to @Burhanuddin Rashid) but its still half for my case. I only can select one item (I can't selected multiple/more items). I need to unselect again when I click the highlight item.
EDITED : I try to write new object and key in my JSON (JSON code above). In my logic, it will make a flag for every item is selected or not, but it still not work.
EDITED : this is my log error :
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.development_laptop.vhp_restotemp, PID: 3590
java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.put(java.lang.String, boolean)' on a null object reference
at com.example.development_laptop.vhp_restotemp.TableActivity$1.OnRecyclerViewItemClicked(TableActivity.java:74)
at com.example.development_laptop.vhp_restotemp.com.example.development_laptop.vhp_restotemp.recyclerview.source.Adapter$ViewHolder.onClick(Adapter.java:97)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .
For multi selection, we need to add one variable in our POJO (Employee. java) named isChecked as boolean. Now the logic is when user clicks any item of the recyclerview, we just set to true this field. And in bind() method, first we check this flag (true or false) then we show check image depends on that.
You can apply setOnClickListener like this:-
View itemView;
Context context;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
this.context=parent.getContext();
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
holder.linearLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent,null));
}
else {
holder.linearLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With