2017-08-31 85 views
0

我有一個RecyclerView,並且正在使用onClick方法中單擊的元素的ID創建一個JSON數組。我能夠生成JSON數組,但我在將此JSON數組傳遞給我的活動時遇到問題。將RecyclerView適配器類中的JSON數組傳遞給活動類

我創建了一個接口:

public interface ClientDataTransfer { 
public void getValues(JSONArray jsonArray); 
} 

和修改適配器的構造是這樣的:

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

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

,並在我的Activity類我設置適配器這樣的:

recyclerViewAdapter = new ClientListAdapter(clientListData, this, this); 
      recyclerView.setAdapter(recyclerViewAdapter); 

我在Activity類中實現了接口並繼承了這個方法,但是我遇到了麻煩Activity類中的JSON數組。

我在onBindViewHolder的setOnClickListener方法內部創建了JSON數組。

這裏是我的適配器類別:

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

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


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

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

@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); 
       } else 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; 
         } 
        } 
       } 
       //clientArray.put(clientObject); 

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

      clientDataTransfer.getValues(clientArray); 
      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); 

    } 
} 

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

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); 
       } else 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; 
         } 
        } 
       } 
       //clientArray.put(clientObject); 

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

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

這裏是我的活動課,我正在執行適配器:

public class ClientListActivity extends AppCompatActivity implements View.OnClickListener, ClientDataTransfer { 

private String groupId; 
private String clientId; 
private String clientName; 
private RecyclerView recyclerView; 
private List<ClientListData> clientListData; 
private RecyclerView.LayoutManager recyclerViewLayoutManager; 
private RecyclerView.Adapter recyclerViewAdapter; 
private JSONArray clientArrayList = new JSONArray(); 
ClientDataTransfer clientDataTransferx; 

private Toolbar toolbar; 
private FloatingActionButton share; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_client_list); 

    recyclerView = (RecyclerView) findViewById(R.id.rv_client_list); 
    toolbar = (Toolbar) findViewById(R.id.client_list_toolbar); 
    share = (FloatingActionButton) findViewById(R.id.floating_action_button); 
    share.setOnClickListener(this); 

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     Window window = getWindow(); 
     window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
     window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
     window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorSecondary)); 
    } 

    setSupportActionBar(toolbar); 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true); 

    clientListData = new ArrayList<>(); 
    recyclerViewLayoutManager = new LinearLayoutManager(this); 
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    recyclerView.setLayoutManager(recyclerViewLayoutManager); 

    if(getIntent() != null) { 

     Bundle bundle = getIntent().getExtras(); 
     try { 
      JSONObject jsonClientListObject = new JSONObject(getIntent().getStringExtra("clientList")); 
      JSONArray clientArray = jsonClientListObject.optJSONArray("activeClients"); 

      for(int i = 0 ; i < clientArray.length(); i++) { 
       ClientListData clientListData = new ClientListData(); 
       clientListData.setClientId(clientArray.getJSONObject(i).getString("id")); 
       clientListData.setClientName(clientArray.getJSONObject(i).getString("name")); 

       clientId = clientListData.getClientId(); 
       clientName = clientListData.getClientName(); 
      } 
      groupId = bundle.getString("groupId"); 

      for(int i=0; i<clientArray.length(); i++) { 
       ClientListData clientListDataModel = new ClientListData(); 
       JSONObject jsonObject = null; 
       jsonObject = clientArray.getJSONObject(i); 
       clientListDataModel.setClientName(jsonObject.getString("name")); 
       clientListDataModel.setClientId(jsonObject.getString("id")); 
       clientListData.add(clientListDataModel); 
      } 

      recyclerViewAdapter = new ClientListAdapter(clientListData, this, this); 
      recyclerView.setAdapter(recyclerViewAdapter); 

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

/*@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.select_all, menu); 

    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 

     case R.id.select_all: 

      break; 
    } 

    return super.onOptionsItemSelected(item); 
}*/ 

@Override 
public void getValues(JSONArray jsonArray) { 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 

     case R.id.floating_action_button: 
      clientDataTransferx = new ClientDataTransfer() { 
       @Override 
       public void getValues(JSONArray jsonArray) { 
        clientDataTransferx.getValues(clientArrayList); 
       } 
      }; 
      break; 
    } 
} 

} 
+0

你面臨什麼樣的錯誤? 您是否在活動中實施了ClientDataTransfer接口? –

+0

是的,我已經在活動中實現了接口。 clientDataTransfer.getValues(clientArray);在onClick的底部是我如何設置接口的方法中的clientArray – localhost

+0

你在活動中接收回調?如果可能,讓我看看錯誤 –

回答

1

通JSON對象爲String 時,一定要仔細閱讀使用: -

JJSONObject obj = new JSONObject(Stringhere); 
JSONArray jsonArray = obj.getJSONArray("results"); 
ArrayList <objectClass> somearray= new ArrayList <objectClass>(); 
for (int i = 0; i < jsonArray.length(); i++) 
{ 
    Movie objectClass = new objectClass(); 
    JSONObject json_data = jsonArray.getJSONObject(i); 
    somearray.setImageUrl(json_data.getString("key1")); 
    somearray.setTitle(json_data.getString("key2")); 
    somearray.setPlot(json_data.getString("key3")); 
} 
相關問題