【问题标题】:Button OnClicklistener from dynamically genereated Buttons来自动态生成的按钮的按钮 OnClicklistener
【发布时间】:2016-01-07 17:19:27
【问题描述】:

我在为运行时生成的按钮构建正确的 OnClicklistener 时遇到问题。我在 stackoverflow 上找到了一些线程,但经过多次尝试后,我无法正常工作。

我有一个方法可以在左侧“列”和右侧“列”x 按钮中构建一个带有 TextView 的 GUI。每个按钮都有一个其他链接,应该由 onClick 打开。在运行时创建之前我不知道该链接。

这是我的代码和我的实际尝试。但在这种情况下,我每次只得到最后生成的按钮的链接。如果我点击第一个,第二个....每次都是同一个链接。

希望有解决办法!

  private void createNewView (String JsonInputBeacon, String JsonInputConfig){

    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
    tableLayout.setStretchAllColumns(true);

    try {
        final JSONArray jsonArrayBeacon = new JSONArray(JsonInputBeacon);
        final JSONArray jsonArrayConfig = new JSONArray(JsonInputConfig);


        int Patientencounter = 1;

        for(int JsonObjectCounterBeacon = 0; JsonObjectCounterBeacon < jsonArrayBeacon.length(); JsonObjectCounterBeacon ++ ){
            final JSONObject objectBeacon = jsonArrayBeacon.getJSONObject(JsonObjectCounterBeacon);

            TableRow row = new TableRow(this);
            TextView outputLeft = new TextView(this);
            outputLeft.setText("Patient " + Patientencounter + ":\n" + "Name: " + objectBeacon.getString("surname") + ", " + objectBeacon.getString("firstName") + "\n" + "Geb-Datum: " + objectBeacon.getString("birthdate"));

            row.addView(outputLeft);

            for (int JsonObjectCounterConfig = 0; JsonObjectCounterConfig < jsonArrayConfig.length(); JsonObjectCounterConfig++){
                final JSONObject objectConfig = jsonArrayConfig.getJSONObject(JsonObjectCounterConfig);

                TableRow rowRight = new TableRow(this);
                Button buttonRight = new Button(getApplicationContext());
                buttonRight.setText(objectConfig.getString("name"));
                final String myURL = objectConfig.getString("link");

                buttonRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(myURL));
                        startActivity(browserIntent);
                    }
                });

                rowRight.addView(buttonRight);
                row.addView(rowRight);
            }

            tableLayout.addView(row);

            Patientencounter +=1;
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    setContentView(tableLayout);
}

【问题讨论】:

    标签: android json android-layout button onclicklistener


    【解决方案1】:

    但在这种情况下,我每次只得到最后生成的链接 按钮。如果我点击第一个,第二个....每次都是 同一个链接

    因为myURL包含由for-loop的最后一次迭代分配的值。

    使用buttonRight的setTag/getTag方法根据Button点击获取url。喜欢:

    使用setTag 设置值:

    final String myURL = objectConfig.getString("link");
    buttonRight.setTag(myURL);
    

    并使用onClick方法的v参数获取myURL值:

    Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                                     Uri.parse(v.getTag().toString()));
    

    【讨论】:

    • 这就是解决方案。它工作正常!感谢您的快速回复!我从几个小时就试过了......
    【解决方案2】:

    将按钮的标签设置为 URI,然后从传递给 onclick 方法的视图中访问标签。

    【讨论】:

      猜你喜欢
      • 2018-12-06
      • 1970-01-01
      • 2013-01-02
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多