2017-08-31 44 views
0

我有一個RecyclerView,當一個項目一個用戶點擊刪除JSON對象,該項目被選中其ID保存在一個JSON陣列。同樣,如果用戶選擇一個項目,然後決定取消選擇它,則應從JSON數組中刪除該項目,並創建一個更新的數組。我可以在onClick上創建JSON數組,但是在!onClick中,我無法從JSON數組中刪除JSON對象。從JSON陣列上的onClick在Android的

這裏是我的適配器類別:

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問題。

+0

不'JSONArray#刪除(INT指數)'工作? – pskink

+0

而是JSONArray的使用ArrayList並將其轉換成JSONArray在發送時間 –

+0

@pskink我並沒有說,給我一個時刻,但我的要求是從我通過clientObject.remove做數組中刪除一個特定ID(clientListData獲得(位置).getClientId()); – localhost

回答

2

試試這個:UPDATE]

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

我想你的方法,但我在clientArray.remove(I)得到的錯誤,我想remove方法不org.json支持 – localhost

+0

讓我試試removeFromJsonArray功能,我已經更新的問題也是如此。 – localhost

+0

該位置是在clientArray中找到的clientObject的索引。 Id與陣列中物體的位置無關 –

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

     } 
    } 
+0

謝謝您的解決方案,我會看看它,以及 – localhost

+0

您提供的解決方案是令人印象深刻,但發送值按鈕在我的活動課,就在佈局文件中RecyclerView元以下,所以我創建一個接口來傳遞clientArray並在我的Activity類中實現接口。現在,我不知道我如何才能在活動課 – localhost

+0

實際陣列寫上活動/片段clicklistener不是適配器只是通過clicklistener參考適配器和參加活動的回調 –