最后一个按钮在 android 中添加了独裁

Last Button added Dictatorship in android

我知道这个标题很花哨,但这在我的 android 应用程序中正在发生。

我将用户想要打开的网站名称存储在字符串数组列表中。然后制作一个按钮数组,其大小取决于arraylist的大小。

然后我使用数组列表设置按钮的文本。这非常有效,所有按钮都分配了正确的文本。

然后我设置每个按钮的点击监听器并创建一个打开网站的隐式意图。

发生的问题是,尽管按钮的文本设置完美,但侦听器存在一些问题,因为无论我按哪个按钮,总是打开最后一个按钮的站点,因此最后一个按钮独裁统治。

这是我的代码。

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));
        }
    }
   }
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://"+s));
startActivity(i); 

也许你不写i.setclass()

你遇到的问题是

 s=sites.get(i);

S 由最后一个元素设置。 每当单击按钮时,它都会从最后一个元素中获取 S,因为 S 在您拥有的 onClickListener 实例之外。

我建议将 url 保存在 Button 标签中,然后在单击按钮时使用它。

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);
                }
            });
        }