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
Update
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)