来自动态生成的按钮的按钮 OnClicklistener
Button OnClicklistener from dynamically genereated Buttons
我在为运行时生成的按钮构建正确的 OnClicklistener 时遇到问题。我在 Whosebug 上发现了一些线程,但经过多次尝试后我无法正常工作。
我有一个方法可以在左侧 "column" 和右侧 "column" x 按钮中构建带有 TextView 的 GUI。每个 Button 都有另一个 link,应该由 onClick 打开。在运行时创建之前,我不知道 link。
这是我实际尝试的代码。但在这种情况下,我每次只得到最后生成的按钮的 link 。如果我点击第一个,第二个....每次都是一样的link。
希望有解决办法!
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);
}
But in this Case I get everytime only the link of the last generated
button. If I click on first one, second one .... it's everytime the
same link
因为 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()));
将按钮的标签设置为 URI,然后从传递给 onclick 方法的视图访问标签。
我在为运行时生成的按钮构建正确的 OnClicklistener 时遇到问题。我在 Whosebug 上发现了一些线程,但经过多次尝试后我无法正常工作。
我有一个方法可以在左侧 "column" 和右侧 "column" x 按钮中构建带有 TextView 的 GUI。每个 Button 都有另一个 link,应该由 onClick 打开。在运行时创建之前,我不知道 link。
这是我实际尝试的代码。但在这种情况下,我每次只得到最后生成的按钮的 link 。如果我点击第一个,第二个....每次都是一样的link。
希望有解决办法!
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);
}
But in this Case I get everytime only the link of the last generated button. If I click on first one, second one .... it's everytime the same link
因为 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()));
将按钮的标签设置为 URI,然后从传递给 onclick 方法的视图访问标签。