【发布时间】: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