Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project wherein I have to parse a JSON Array and show the values in a RecyclerView. The issue is that I am not able to show the data form JSON in the RecyclerView.

Here is my Activity class:

Java
<blockquote class="quote"><div class="op">Quote:</div>public class ClientListActivity extends AppCompatActivity {

    private String groupId;
    private String clientId;
    private String clientName;
    private RecyclerView recyclerView;
    private List<ClientListData> clientListData = new ArrayList<>();
    private ClientListAdapter mClientListAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_list);
        recyclerView = (RecyclerView) findViewById(R.id.rv_client_list);


        if(getIntent() != null) {

            Bundle bundle = getIntent().getExtras();
            try {

                JSONObject jsonClientListObject = new JSONObject(getIntent().getStringExtra("clientList"));
                //JSONArray clientArray = jsonClientListObject.getJSONArray("activeClients");

                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();
                }
                //String activeClients = jsonClientListObject.getJSONArray("activeClients").toString();

                groupId = bundle.getString("groupId");

                mClientListAdapter = new ClientListAdapter(R.layout.client_list_item, clientListData);
                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
                recyclerView.setLayoutManager(mLayoutManager);
                recyclerView.setAdapter(mClientListAdapter);

                Log.e("Client List ********", jsonClientListObject.toString() + " group Id: " + groupId);
                Log.e("client id: " + clientId, "client name: " + clientName);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}</blockquote>


and here are my Adapter and Model Class:

Java
<blockquote class="quote"><div class="op">Quote:</div>public class ClientListAdapter extends RecyclerView.Adapter<ClientListAdapter.ViewHolder> {

    private int listItemLayout;
    private List<ClientListData> clientArrayList;

    public ClientListAdapter(int layoutId, List<ClientListData> clientArrayList) {
        this.listItemLayout = layoutId;
        this.clientArrayList = clientArrayList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(listItemLayout, parent, false);
        ViewHolder myViewHolder = new ViewHolder(view);

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(ClientListAdapter.ViewHolder holder, int position) {

        TextView clients = holder.clientList;
        clients.setText(clientArrayList.get(position).getClientName());

    }

    @Override
    public int getItemCount() {
        return clientArrayList == null ? 0:clientArrayList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView clientList;
        private final Context context;

        public ViewHolder(View itemView) {
            super(itemView);

            context = itemView.getContext();
            itemView.setOnClickListener(this);

            clientList = (TextView) itemView.findViewById(R.id.tv_client_list);
        }

        @Override
        public void onClick(View v) {

        }
    }
}</blockquote>


Model Class:

Java
public class ClientListData {

    String clientId;
    String clientName;

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


I would like to know what am I doing wrong here.

What I have tried:

I tried a lot of things but I am quite new to Android Development and I don't know where I am doing wrong
Posted
Comments
David Crow 30-Aug-17 10:00am    
I'm seeing multiple instances of clientListData. One belongs to ClientListActivity. The other is local to the for() loop. Is that intentional?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900