0

I have a RecyclerView and am creating a JSON Array with the ids of elements which are clicked in the onClick method. I am able to generate the JSON Array but I am facing trouble passing this JSON Array to my Activity.

I created an interface :

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

and modified the Adapter constructor like this:

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

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

and in my Activity class I am setting the adapter like this:

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

I implemented the Interface in the Activity class and inherited the method but I am facing trouble getting the JSON Array in the Activity class.

I am creating the JSON Array inside the setOnClickListener method in onBindViewHolder.

Here is my Adapter Class:

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

Here is my Activity Class where I am implementing the Adapter:

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

}
7
  • What kind of error are you facing ? Have you implemented the ClientDataTransfer interface in activity ? Commented Aug 31, 2017 at 12:02
  • Yes, I have implemented the interface in the Activity. The clientDataTransfer.getValues(clientArray); at the bottom of onClick is how I am setting the clientArray in the Interface's method Commented Aug 31, 2017 at 12:04
  • are you receiving callback in activity? let me see the error if possible Commented Aug 31, 2017 at 12:06
  • Just a moment, let me update the Activity code in above. In the log I am getting an empty Array but I am not getting any error Commented Aug 31, 2017 at 12:08
  • Override method body is empty . Commented Aug 31, 2017 at 12:18

1 Answer 1

1

pass json object as a String when you have to read use :-

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"));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.