java - Activity.java中的多个选中项RecyclerView

标签 java android json android-recyclerview

我有一个 RecyclerView,其中的数据来自 CardView 中的本地 JSON。当单击一个或某些项目(更改所选项目的背景或突出显示)(如在 Line App 中编辑)但没有 Button 或 longpress 时,我需要在选定项目上实现。但我不想使用 StateListDrawable 或(使用 XML),因为我有一些 JSON 数据需要稍后处理。

我需要在我的 Activity 中有一个状态,例如 boolean 值或其他东西来保存我单击的每个项目,但我又没有任何解决方案。我已经阅读并尝试了一些教程,但它不起作用。这是我现在的 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)  {

    /* ------ 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
            },
............

这是我的 Activity 的样子

enter image description here

更新

现在输出:

enter image description here

现在,当我单击该项目时它会突出显示(感谢@Burhanuddin Rashid),但它仍然是我的一半。我只能选择一个项目(我不能选择多个/更多项目)。单击突出显示项时需要再次取消选择。

已编辑:我尝试在我的 JSON(上面的 JSON 代码)中编写新对象和 key 。按照我的逻辑,它会为每个项目创建一个标志,但它仍然不起作用。

已编辑:这是我的日志错误:

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

最佳答案

您可以像这样应用 setOnClickListener:-

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

                    }
                }
            });
        }

关于java - Activity.java中的多个选中项RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41235278/

相关文章:

java - Seam:以编程方式将 s:decorate 设置为无效状态

java - 如何生成clojure代码在运行时调用java方法?

PHP json_encode 对于大数组很慢

java - 如何从 Spring Boot Restful GET 调用返回对象属性的子集?

android - 如何删除底部导航栏上图标和文本变大的动画?

javascript - 使用 .each 从 jsonresult 填充下拉列表

java - 求含有未知元素的矩阵中某一列的平均值

java - 如何将 CodeModel 表达式强制括在括号中?

Android 4.0 调试问题 - DDMS 错误?

java - 为什么我的 .isEmpty 在此代码中不起作用?