努力用从数据库中填充的 BUTTONS 替换 SPINNER

Struggling to replace SPINNER with BUTTONS populated from database

正在尝试用从数据库动态填充的按钮替换微调器。 通常微调器使用数组适配器和内置列表项布局 "android.R.layout.simple_spinner_item" 等。

如果要填充按钮而不是微调器,应该如何修改?

我是否应该将 loadDifficulties() 方法中的微调器布局替换为按钮布局?

这里是如何使用 SPINNER

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_screen);
spinnerDifficulty = findViewById(R.id.spinner_quizlist);
loadDifficulties();
Button startTest = findViewById(R.id.start_test);
startTest.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        startQuiz();
    }
});
}
private void startQuiz() {
ListQuiz selectedLevel = (ListQuiz) spinnerDifficulty.getSelectedItem();
int LevelListID = selectedLevel.getId();
String quizListName = selectedLevel.getName();
Intent intent = new Intent(StartingScreenActivity.this, MainActivity.class);
intent.putExtra(EXTRA_DIFFICULTY_ID, LevelListID);
intent.putExtra(EXTRA_DIFFICULTY_NAME, quizListName);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
private void loadDifficulties(){
QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
List<ListQuiz> LevelList = dbHelper.getAllListQuiz();
ArrayAdapter<ListQuiz> adapterLevelList = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, LevelList);    adapterLevelList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDifficulty.setAdapter(adapterLevelList);
}
onCreate(){    
LinearLayout listOrders = findViewById(R.id.listOrders);
    for (int i = 0; i < listForShowing.size(); i++){
        String text = listForShowing.get(i);
        listOrders.addView(createButton(text));
    }
listOrders.requestLayout();
}

public Button createButton(String text){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Button button = inflater.inflate(R.layout.button, null);
button.setText(text);
}

简要

  • 为按钮创建了 LinearLayout。
  • 我不再需要在 loadDifficulties 中使用 ArrayAdapter。
  • 将所有需要的参数添加到 startQuiz()(在我的例子中是 int 和 String),将方法简化为仅 Intents。

    private ArrayAdapter <ListQuiz> adapter;
    private  Button autobutton;
    public int categorySize;
    private List<ListQuiz> categoryName;
    private LinearLayout QuizListLayout;
    private  Button levelButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starting_screen);
    autobutton = findViewById(R.id.autobutton);
    loadDifficulties();
    
    QuizListLayout = findViewById(R.id.layoutForButtons);
    for(int i=0; i<categorySize;i++){
    
        levelButton =new Button(this);
        levelButton.setText("" + categoryName.get(i));
        levelButton.setId(i);
        final int index = i;
        levelButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        levelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startQuiz(v.getId(), categoryName.get(index).toString());
            }
        });
        QuizListLayout.addView(levelButton);
    }
    }
    
    
    private void startQuiz(int LevelListID, String quizListName) {
    Intent intent = new Intent(StartingScreenActivity.this, MainActivity.class);
    intent.putExtra(EXTRA_DIFFICULTY_ID, LevelListID);
    intent.putExtra(EXTRA_DIFFICULTY_NAME, quizListName);
    startActivityForResult(intent, REQUEST_CODE_QUIZ);
    }
    
    private void loadDifficulties(){
    QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
    List<ListQuiz> LevelList = dbHelper.getAllListQuiz();
    categorySize = dbHelper.getAllListQuiz().size(); 
    categoryName = dbHelper.getAllListQuiz();  
    

buttonlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layoutForButtons">
</LinearLayout>