I have a json in this format:
[{
"name": "Selection",
"link": ["https://d1u5p3l4wpay3k.cloudfront.net/testSelect.ogg"]
},
{
"name": "Introduction",
"link": ["https://d1u5p3l4wpay3k.cloudfront.net/introSelect.ogg",
"https://d1u5p3l4wpay3k.cloudfront.net/introSelect2.ogg".ogg"
]
},
{
"name": "Abilities",
"link": ["https://d1u5p3l4wpay3k.cloudfront.net/testatk1.ogg",
"https://d1u5p3l4wpay3k.cloudfront.net/testatk1.ogg",
"https://d1u5p3l4wpay3k.cloudfront.net/testatk1.ogg",
"https://d1u5p3l4wpay3k.cloudfront.net/testatk1.ogg"
]
}]
i would like to create activity where the button are created depending on link given in that json for example first key is 1 button second key is 2 while for 3rd one 4 button this is my get_data()
private void getData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
Image image = new Image(); // this class has getter and setter
image.setVoicline(jsonObject.getString("name"));
image.setLarge(jsonObject.getString("link"));
imageList.add(image);
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
my output for now:
im trying to create soundboard for some game characters each link is sound it make clicking on a button would play that sound
this is full code for my soundboard_activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soundboard);
mList = findViewById(R.id.main_list);
imageList = new ArrayList<>();
adapter = new SoundAdapter(getApplicationContext(),imageList);
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
dividerItemDecoration = new DividerItemDecoration(mList.getContext(), linearLayoutManager.getOrientation());
mList.setHasFixedSize(true);
mList.setLayoutManager(linearLayoutManager);
mList.addItemDecoration(dividerItemDecoration);
mList.setAdapter(adapter);
getData();
}
private void getData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
JSONArray jsonArrayLink = jsonObject.getJSONArray("link");
Image image = new Image();
image.setVoicline(jsonObject.getString("name"));
for (int j = 0; j < jsonArrayLink.length(); j++) {
String link = jsonArrayLink.getJSONObject(j).toString();
image.setLarge(link);
imageList.add(image);
}
imageList.add(image);
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}}
this is exception error
and this is my Recycleview adapter
public class SoundAdapter extends RecyclerView.Adapter { private Context context; private List list;
public SoundAdapter(Context context, List<Image> list) {
this.context = context;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.single_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Image image = list.get(position);
holder.vname.setText(image.getVoicline());
//holder.link.setText(image.getLarge());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView vname,link;
public ViewHolder(View itemView) {
super(itemView);
vname = itemView.findViewById(R.id.vname);
link = itemView.findViewById(R.id.play);
}
}}
my image model getter setter
public Image(String name, String small, String medium, String large, String timestamp, String voicline) {
this.name = name;
this.small = small;
this.medium = medium;
this.large = large;
this.timestamp = timestamp;
this.voicline = voicline;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSmall() {
return small;
}
public void setSmall(String small) {
this.small = small;
}
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
public String getLarge() {
return large;
}
public void setLarge(String large) {
this.large = large;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getVoicline() {
return voicline;
}
public void setVoicline(String voicline) {
this.voicline = voicline;
}}