I have a RecyclerView
with data from local JSON
in CardView
. I need to implement on selected elements when one or some element is clicked (change the background to the selected or selected element) (for example, edit the Line application in the application), but without a button or longpress
. But I do not want to use StateListDrawable
or (using XML) because I have some JSON
data that needs to be processed later.
I need a state in my activity as a logical value or something to save every element that I clicked, but I have no solution again. I read and tried the tutorial, but it does not work. Now it is below my activity:
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) { 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);
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 my activity looks like

Update
Output:

Now it stands out when I click on an item (thanks @Burhanuddin Rashid), but its still half for my business. I can select only one item (I cannot select multiple / multiple items). I need to deselect again when I click on the highlighted item.
EDITED: I'm trying to write a new object and key in my JSON (JSON code above). In my logic, it will make the flag for each element selected or not, but it still does 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)