在Android上的onClick上从JSON数组中删除JSON对象

时间:2017-08-31 07:18:41

标签: android json android-recyclerview

我有一个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;
}

}

请注意,我在Model Class中创建了一个boolean变量来监听onClicks。

注意: - 因为我正在使用org.json,所以在我遇到this问题的大量搜索之后,我无法在JSON数组上使用remove方法。< / p>

2 个答案:

答案 0 :(得分:2)

试试这个:[更新]

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

答案 1 :(得分:0)

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

        }
    }