Last Button added Dictatorship in android

46 views Asked by At

I know that title is fancy but this is going on in my android app.

I am storing the names of websites which user wants to open in an arraylist of strings. Then an array of buttons is made whose size depends on the size of arraylist.

Then I set the text of the buttons using the arraylist. This works perfectly and all the buttons are assigned correct text.

Then I set the on click listeners of each button and create an implicit intent to open the web site.

Problem that is occuring is that despite the fact the text of the buttons are set perfectly , There is some problem with the listener because no matter which button i press, always the site of last button is opened and hence last button dictatorship.

Here is my code.

package com.example.hp.myproject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;    
import java.util.ArrayList;

/**
 * Created @CreativeLabsworks 
 */
    public class Act3 extends Activity 
    {
     LinearLayout ll1; Button[] bt_arr; String s;
     LinearLayout.LayoutParams params1;
     ArrayList<String> sites;TextView t1;
     protected void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        sites= intent.getStringArrayListExtra("arraylist");
        /*Creating a linear layout to hold editTexts*/
        ll1=new LinearLayout(this);
        ll1.setOrientation(LinearLayout.VERTICAL);
        /*set the width and height of the linear layout*/
        params1=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setContentView(ll1, params1);
        /*Initializing the button array*/
        bt_arr = new Button[sites.size()];
        /*Initialize each button*/
        for(int i=0;i<bt_arr.length;++i)
        {
            bt_arr[i] = new Button(this);
            ll1.addView(bt_arr[i]);
        }
        setText();
        attach_listeners();
    }
    public void attach_listeners()
    {
        /*this function attaches listeners with all buttons in the button array*/
        for(int i=0;i<bt_arr.length;++i)
        {
            s=sites.get(i);
            bt_arr[i].setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    Intent i=new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse("http://"+s));
                    startActivity(i);
                }
            });
        }
    }
    public void setText()
    {
        /*This function sets the text of the various buttons*/
        int j;
        for(int i=0;i<bt_arr.length;++i)
        {
            j=sites.get(i).indexOf(".com");
            bt_arr[i].setText(sites.get(i).substring(4,j));
        }
    }
   }
2

There are 2 answers

1
N Jay On BEST ANSWER

The problem that you have is that

 s=sites.get(i);

S is being set by the last element. Whenever a button is clicked it is taking the S from the last element since S is outside the onClickListener instance that you have.

I would suggest to save the url in the Button tag and then use it When the button is clicked.

for (int i = 0; i < bt_arr.length; ++i) {
            s = sites.get(i);
            bt_arr[i].setTag(s);
            bt_arr[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String website = (String)v.getTag();
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse("http://" + website));
                    startActivity(i);
                }
            });
        }
2
爱上猫咪的小鼠 On
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://"+s));
startActivity(i); 

Maybe you do not write i.setclass()?