Karam Abdullah
Karam Abdullah

Reputation: 45

Get data from Json Array to buttons in Java using (for) statment

I'm trying to get two Strings (title and link) from Json Array by (for) statement and create new buttons so that :

The title string will be the text of button And every button will take the user to its own link.

I succeeded in creating the java code and it runs But all the buttons take the user to the same link (first one link).

Here is my code:

public void fetchar() {

    String zi = "";

    try{

        jarry=new JSONArray(jsonstring);

        for (int k=0;k<jas.length();k++) {

            jsn = jas.getJSONObject(k);

            Button nb = new Button(this);

            nb.setText(jsn.getString("title"));

            nb.setId(k);

            nb.setBackgroundResource(R.drawable.feld); 

            nb.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    try{
                        loadthis(jsn.getString("link")); 
                    } catch(Exception e){}
                }

            });

        space.addView(nb);
    }

    dial.dismiss();

    }
    catch(Exception e) {}
}
public void loadthis(String urk) {
    try{
        if (newString.equals("facebook")) {
            urk="fb://facewebmodal/f?href="+urk;
        } else if (newString.equals("youtube")) {
            urk="vnd.youtube:"+urk;
        }

        Intent goi=new Intent(Intent.ACTION_VIEW,Uri.parse(urk)); 
        startActivity(goi); 

    } catch(Exception e){}
}

I defined all the variables at the first of java file

How can I make like changes among my new buttons?

Upvotes: 0

Views: 136

Answers (2)

NiklasLehnfeld
NiklasLehnfeld

Reputation: 1081

The problem is that you are saving the JsonObject as a class variable. Changing it to a local variable would solve your problem. I modified your code a little bit:

public void fetchar() {

    String zi = "";

    try{

        JSONArray jarry = new JSONArray(jsonstring);

        for (int k=0; k < jarry.length(); k++) {

            JSONObject jsonObject = jarry.getJSONObject(k);

            Button button = new Button(this);

            button.setText(jsonObject.getString("title"));

            button.setId(k);

            button.setBackgroundResource(R.drawable.feld); 

            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    try{
                        loadthis(jsonObject.getString("link")); 
                    } catch(Exception e){}
                }

            });

            space.addView(button);
        }

        dial.dismiss();

    } catch(Exception e) {}
}

BTW: Try to use more clear variable names in your code. This really helps to understand the code later again.

Upvotes: 1

Ashwani Garg
Ashwani Garg

Reputation: 62

Use setTag() method on button to set your link as a tag to every button. After setId() call setTag(jsn.getString("link"))

And within event listener modify your code as below.

public void onClick(View v) {
    try{
       String link = String.valueOf(v.getTag); 
       loadthis(link);
    }catch(Exception e){}
}

Upvotes: 0

Related Questions