我有一个RecyclerView
来自本地的数据JSON
in CardView
。当单击一个或某个项目时(更改所选项目的背景或突出显示)(例如在 Line App 中编辑)但没有 Button 或,我需要在选定的项目上实现longpress
。但我不想使用StateListDrawable
或者(使用 XML)因为我有一些JSON
后面需要处理的数据。
我的活动中需要一个状态,例如布尔值或其他东西来保存我单击的每个项目,但我再次没有任何解决方案。我已经阅读并尝试了一些教程,但它不起作用。这是我现在的活动下面:
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();
}
}
});
适配器.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
},
............
这是我的活动看起来像
Update
现在输出:
现在,当我单击该项目时,它会突出显示(感谢@Burhanuddin Rashid),但对于我的情况来说,它仍然是一半。我只能选择一项(无法选择多个/更多项目)。当我单击突出显示的项目时,我需要再次取消选择。
编辑:我尝试在 JSON 中编写新对象和密钥(上面的 JSON 代码)。按照我的逻辑,它会为每个项目是否被选择创建一个标志,但它仍然不起作用。
编辑:这是我的日志错误:
--------- 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)