[英]How can I fix the layout views in my android app?
我正在尝试使用我的 api 创建按钮。 我使用Retrofit
调用 api,然后创建按钮。 但是有代码错误! API 响应中返回的数据是正确的,但创建视图是一个问题。
它说我需要removeView()
。 我这样做了,但现在没有数据打印。
我的代码:
package com.example.englishapp;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class LessonsActivity extends AppCompatActivity {
private TextView textViewResult;
String URL = "http://kurchanovenglish.ru/data/";
private String[] name = new String[18];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lessons);
final LinearLayout layout = new LinearLayout(this);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
params.setMargins(250, 10, 250, 50);
layout.setOrientation(LinearLayout.VERTICAL);
int i;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Call<List<Lesson>> call = jsonPlaceHolderApi.getPosts();
final LinearLayout row = new LinearLayout(this);
final Button button = new Button(this);
call.enqueue(new Callback<List<Lesson>>() {
@Override
public void onResponse(Call<List<Lesson>> call, Response<List<Lesson>> response) {
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
List<Lesson> lessons = response.body();
int b = 0;
String[] name = new String[18];
for (Lesson lesson : lessons) {
button.setText(lesson.getName());
button.setId(lesson.getNumber());
button.setLayoutParams(params);
button.setTextSize(20);
/*button.setBackground(this.getResources().getDrawable(R.drawable.buttons));*/
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
Intent intent = new Intent(LessonsActivity.this, WelcomeActivity.class);
startActivity(intent);
}
});
row.removeView(layout);
row.addView(button);
}
layout.addView(row);
}
@Override
public void onFailure(Call<List<Lesson>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
}
错误:
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.englishapp, PID: 19385
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4915)
at android.view.ViewGroup.addView(ViewGroup.java:4746)
at android.view.ViewGroup.addView(ViewGroup.java:4686)
at android.view.ViewGroup.addView(ViewGroup.java:4659)
at com.example.englishapp.LessonsActivity$1.onResponse(LessonsActivity.java:92)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
请帮帮我。 我不知道该怎么办。
这是你做错了什么; 您正在使用相同的Button
实例并将其再次添加到row
( LinearLayout
)。
如果我理解正确,这就是您想要的视图层次结构。
LinearLayout
--LinearLayout
--Button
--Button
.
--LinearLayout
--Button
--Button
.
.
你需要了解的第一件事是,你不能重复使用View
在视图层次的多个地方。 其次, View
不能是多个父项的子项,即同一Button
不能出现在两行中。 让我们想想这种限制的原因; 比方说,您被允许将相同的Button
添加到另一个父级,然后您将如何识别其中的一次点击,您如何知道您点击了哪个。 此外,假设您有一个文本要显示在第二个Button
,如果/当您共享参考时,您将如何更改每个文本。 你看到问题了吗?
但是,由于您是在 Java 代码中实例化和添加Button
。 您需要首先创建一个新的Button
实例以添加到父row
。 因此,如果您要添加多行,您还需要一个新的 row 实例来添加到父layout
。
然后您的代码变为:
for (Lesson lesson : lessons) {
Button button = new Button(LessonsActivity.this);
button.setText(lesson.getName());
button.setId(lesson.getNumber());
button.setLayoutParams(params);
button.setTextSize(20);
/*button.setBackground(this.getResources().getDrawable(R.drawable.buttons));*/
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
Intent intent = new Intent(LessonsActivity.this, WelcomeActivity.class);
startActivity(intent);
}
});
row.addView(button);
}
layout.addView(row);
}
您需要调整layout.addView(row);
因此,遵循相同的原则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.