[英]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.