0

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:

enter image description here

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

screenshot

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;
}}
AliceB
  • 3
  • 4
  • I don't understand where the question is it looks like you are doing exactly what you want to do? – Nigel Brown Jul 26 '18 at 12:43
  • sorry if not being clear but im stuck at how im unable to create button depending on the the link array not the name only – AliceB Jul 26 '18 at 15:45
  • when you get the name for the button from the json array cant you just grab the next element in the array and set the button click to that url? do you not know how to use the url? – Nigel Brown Jul 26 '18 at 16:17
  • even tho u said i'm probably in the right track i'm still new to this i followed many tuto on net to do this,if u could help me with this i would really appreciated, i couldn't loop through link array for i tried using another " for " but i get an error. – AliceB Jul 27 '18 at 08:03
  • check [here](https://stackoverflow.com/questions/49538393/recyclerview-dynamic-header). – Abhay Koradiya Jul 27 '18 at 13:02

1 Answers1

0

After you have the initial JSON array you need to query the JSON object:

JSONObject jsonObject = response.getJSONObject(i);

this should be getting the following similar block each time you go though the loop:

{"name": "Introduction",
"link": ["https://d1u5p3l4wpay3k.cloudfront.net/introSelect.ogg",
    "https://d1u5p3l4wpay3k.cloudfront.net/introSelect2.ogg".ogg"
]}

You need to something like this to grab the "link" part of the object and assign it to JSONArray since we know this is an array we want:

JSONArray jsonArrayLink = jsonObject.getJSONArray("link");

And again you can set a for loop to get each item of this array and save it to a string:

for (int i = 0; i < jsonArrayLink.length(); i++) {
    String link = jsonArrayLink.getJSONObject(i).toString();
    // do what you want with the string
}

Extra tip for JSON:

It is good practice to do the following:

String testJSON = attributes.has("test") ? attributes.getString("test") : "";

Rather then:

String testJSON = jsonObject.getString("test");

This will make a check to see if it has that item, if it doesn't then it will set the variable to empty string "" rather than creating an error if nothing is found.

Nigel Brown
  • 440
  • 2
  • 13
  • hey thank you for the code, but i actually did this before and got stuck in this exception error "at 0 of type java.lang.String cannot be converted to JSONObject" – AliceB Jul 28 '18 at 08:20
  • Would you be able to share the code and error where is happend for you? – Nigel Brown Jul 29 '18 at 11:43