努力用数据库填充的按钮替换SPINNER

时间:2018-10-18 13:10:25

标签: android spinner android-arrayadapter android-button android-adapterview

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

如果要代替按钮来填充按钮,应该如何修改?

我应该在loadDifficulties()方法中用按钮的布局替换微调器的布局吗?

在这里如何使用旋转器

@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);
}

2 个答案:

答案 0 :(得分:0)

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);
}

答案 1 :(得分:0)

简短

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

    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>