【问题标题】:Remove JSON Object from JSON Array on onClick in Android在 Android 的 onClick 上从 JSON 数组中删除 JSON 对象
【发布时间】:2017-08-31 07:18:41
【问题描述】:

我有一个 RecyclerView,当用户点击一个项目时,该项目会被选中,并且它的 id 保存在 JSON 数组中。类似地,如果用户选择了一个项目,后来又决定取消选择它,则应从 JSON 数组中删除该项目并创建一个更新的数组。我可以在 onClick 上创建 JSON 数组,但在 !onClick 我无法从 JSON 数组中删除 JSON 对象。

这是我的适配器类:

public class ClientListAdapter extends RecyclerView.Adapter<ClientListAdapter.ViewHolder> {

private Context context;
private List<ClientListData> clientListData;
public JSONArray clientArray = new JSONArray();

public ClientListAdapter(List<ClientListData> clientListData, Context context) {
    super();

    this.clientListData = clientListData;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.client_list_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(final ClientListAdapter.ViewHolder holder, final int position) {

    final ClientListData clientListDataModel = clientListData.get(position);
    holder.clientList.setText(clientListDataModel.getClientName());

    holder.itemView.setBackgroundColor(clientListDataModel.isSelected() ? Color.GRAY : Color.WHITE);
    holder.clientList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clientListDataModel.setSelected(!clientListDataModel.isSelected());

            try {
                JSONObject clientObject = new JSONObject();
                if(clientListDataModel.isSelected()) {
                    holder.itemView.setBackgroundColor(Color.GRAY);
                    clientObject.put("id", clientListData.get(position).getClientId());
                    clientArray.put(clientObject);
                }

                if(!clientListDataModel.isSelected()) {

                    holder.itemView.setBackgroundColor(Color.WHITE);

                    for(int i=0; i<clientArray.length(); i++) {
                        clientObject = clientArray.getJSONObject(i);
                        clientObject.remove(clientListData.get(position).getClientId());
                        //clientArray.put(clientObject);
                    }
                }
                //clientArray.put(clientObject);

            } catch (JSONException e) {
                e.printStackTrace();
            }

            Log.e("client id array", ""+clientArray);

        }
    });
}

@Override
public int getItemCount() {
    return clientListData == null ? 0:clientListData.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    public TextView clientList;

    public ViewHolder(View itemView) {
        super(itemView);

        clientList = (TextView) itemView.findViewById(R.id.tv_client_list);

    }
}

}

这是我的模型类:

public class ClientListData {

private String clientId;
private String clientName;
private boolean isSelected = false;

public String getClientId() {
    return clientId;
}

public void setClientId(String clientId) {
    this.clientId = clientId;
}

public String getClientName() {
    return clientName;
}

public void setClientName(String clientName) {
    this.clientName = clientName;
}

public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean selected) {
    isSelected = selected;
}

}

请注意,我在模型类中创建了一个布尔变量来监听 onClicks。

注意:- 因为我使用的是 org.json,所以我无法在 JSON Array 上使用 remove 方法,经过大量搜索后,我遇到了this 问题。

【问题讨论】:

  • JSONArray#remove(int index) 不起作用吗?
  • 使用 ArrayList 代替 JSONArray 并在发送时将其转换为 JSONArray
  • @pskink 我没有尝试过,给我一点时间,但我的要求是从我正在通过 clientObject.remove(clientListData.get(position).getClientId 执行的数组中删除一个特定的 id ());
  • @JRamesh 你能给我举个例子吗?

标签: android json android-recyclerview


【解决方案1】:

试试这个:[更新]

if (!clientListDataModel.isSelected()) {

            holder.itemView.setBackgroundColor(Color.WHITE);

            for (int i = 0; i < clientArray.length(); i++) {
                clientObject = clientArray.getJSONObject(i);
                if (clientObject.getString("id").equals(clientListDataModel.getClientId())) {
                    clientArray=removeFromJsonArray(clientArray,i);
                    break;
                }
            }
        }

removeFromJsonArray 函数:

private JSONArray removeFromJsonArray(JSONArray array,int position){
 if(array==null)return null;
 JSONArray newArray = new JSONArray();
            for (int i=0;i<array.length();i++)
            {
                //Excluding the item at position
                if (i != position)
                {
                    newArray.put(jsonArray.get(i));
                }
            }
       return newArray;
    }

【讨论】:

  • 我尝试了你的方法,但在 clientArray.remove(i) 出现错误,我猜 org.json 不支持 remove 方法
  • 让我试试 removeFromJsonArray 函数,我也更新了问题。
  • 位置是在clientArray中找到的那个clientObject的索引。 id与对象在数组中的位置无关
  • 非常感谢您的帮助@Antonis Lat,您的解决方案奏效了。
【解决方案2】:
    Add This code in activity


    private ArrayList<Integer> selectedIdList = new ArrayList<>();


    ClientListAdapter  adapter=new ClientListAdapter (list,this,new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=(Integer)v.getTag();
                if(list.size()>position){
                 ClientListDataModel clientListDataModel=list.get(position);
    if clientListDataModel!= null) {
                clientListDataModel.setSelected(!clientListDataModel.isSelected());

                        boolean isSelected = clientListDataModel.isSelected();
                        if (!isSelected) {
                            if (selectedIdList.contains(clientListDataModel.getId())) {
                                int index = selectedIdList.indexOf(clientListDataModel.getId());
                                selectedIdList.remove(index);
                            }
                        } else {
                            if (!selectedIdList.contains(clientListDataModel.getId())) {
                                selectedIdList.add(clientListDataModel.getId());
                            }
                        }

                    }
                adapter.notifyItemChanged(pos);
    }
        });


    And at sending time 



        public JSONArray clientArray = new JSONArray();
        if(!selectedIdList.empty()){
            for(int i=0; i<selectedIdList.size(); i++) {
                JSONObject clientObject = new JSONObject();
                clientObject.put("id", selectedIdList.get(i));
                clientArray.put(clientObject);

            }
        }



    public class ClientListAdapter extends RecyclerView.Adapter<ClientListAdapter.ViewHolder> {

    private Context context;
    private List<ClientListData> clientListData;
    public JSONArray clientArray = new JSONArray();
    OnClickListener onClickListener;
    public ClientListAdapter(List<ClientListData> clientListData, Context context,OnClickListener onclick) {
        super();

        this.clientListData = clientListData;
        this.context = context;
onClickListener=onclick;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.client_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ClientListAdapter.ViewHolder holder, final int position) {

        final ClientListData clientListDataModel = clientListData.get(position);
        holder.clientList.setText(clientListDataModel.getClientName());

        holder.itemView.setBackgroundColor(clientListDataModel.isSelected() ? Color.GRAY : Color.WHITE);
 holder.clientList.setTag(position);
        holder.clientList.setOnClickListener(onClickListener);
    }

    @Override
    public int getItemCount() {
        return clientListData == null ? 0:clientListData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView clientList;

        public ViewHolder(View itemView) {
            super(itemView);

            clientList = (TextView) itemView.findViewById(R.id.tv_client_list);

        }
    }

【讨论】:

  • 谢谢你的解决方案,我也去看看
  • 您提供的解决方案令人印象深刻,但发送值的按钮位于我的 Activity 类中,位于布局文件中 RecyclerView 元素的正下方,因此我创建了一个接口来传输 clientArray 并实现了我的活动类中的接口。现在,我不知道如何在 Activity Class 中获取实际数组
  • 在不在适配器中的活动/片段上编写你的点击监听器,只需将点击监听器引用传递给适配器并在活动中回调
  • 我的点击监听器在 Activity 本身中,我在实现接口后覆盖了 Activity 中的方法。要不要我更新代码
  • 我根据这个问题发布了一个新问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
  • 2019-11-25
  • 2021-12-23
相关资源
最近更新 更多