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:
<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.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");
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:
<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:
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