Java 如何将房间数据库中的列表数据添加到android中的警报对话框

Java 如何将房间数据库中的列表数据添加到android中的警报对话框,java,android,list,android-alertdialog,android-room,Java,Android,List,Android Alertdialog,Android Room,我正在使用房间数据库,我想从类别表中检索数据并在警报对话框中显示 final List<Category> categories = database.getQuery().getByCategory("Income"); final List categories=database.getQuery().getByCategories(“收入”); 但这里显示的错误如下: error: no suitable method found for setItems(List<

我正在使用房间数据库,我想从类别表中检索数据并在警报对话框中显示

final List<Category> categories = database.getQuery().getByCategory("Income");
final List categories=database.getQuery().getByCategories(“收入”);
但这里显示的错误如下:

error: no suitable method found for setItems(List<Category>,<anonymous android.content.DialogInterface.OnClickListener>)
method Builder.setItems(int,android.content.DialogInterface.OnClickListener) is not applicable
(argument mismatch; List<Category> cannot be converted to int)
method Builder.setItems(CharSequence[],android.content.DialogInterface.OnClickListener) is not applicable
(argument mismatch; List<Category> cannot be converted to CharSequence[])
错误:找不到适用于setItems(列表)的方法
方法生成器.setItems(int,android.content.DialogInterface.OnClickListener)不适用
(参数不匹配;列表无法转换为int)
方法生成器.setItems(CharSequence[],android.content.DialogInterface.OnClickListener)不适用
(参数不匹配;列表无法转换为CharSequence[])
package com.example.mybugetssimple;
导入androidx.annotation.NonNull;
导入androidx.appcompat.app.appcompat活动;
导入androidx.coordinatorlayout.widget.coordinatorlayout;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.os.Bundle;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.EditText;
导入android.widget.ListAdapter;
导入android.widget.Spinner;
导入android.widget.SpinnerAdapter;
导入android.widget.TextView;
导入com.example.mybugetssimple.database.Category;
导入com.example.mybugetssimple.database.ExpenseDatabase;
导入com.google.android.material.snackbar.snackbar;
导入java.util.List;
公共类AddExpenseActivity扩展了AppCompatActivity{
text查看日期,catType;
编辑文本量,描述;
费用数据库;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u expense);
//添加后退按钮
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
catType=findviewbyd(R.id.addExInputTv);
日期=findViewById(R.id.AddExDateInputV);
金额=findViewById(R.id.addExAmountInputEt);
desc=findViewById(R.id.addExDesInputEt);
database=ExpenseDatabase.getDatabaseInstance(此);
最终列表类别=database.getQuery().getByCategory(“收入”);
系统输出打印项次(类别);
catType.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
final AlertDialog.Builder=新建AlertDialog.Builder(AddExpenseActivity.this);
建筑商。设置标题(“选择收入类别”);
setItems(类别,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
catype.setText(categories.get(which.getCategory_name());
}
} );
setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
builder.setCancelable(true);
}
} );
setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
如果(其中==-1){
}否则{
catype.setText(categories.get(which.getCategory_name());
}
}
} );
AlertDialog AlertDialog=builder.create();
alertDialog.show();
}
} );
}
公共布尔值OnOptionItemSelected(@NonNull MenuItem item){
int id=item.getItemId();
if(id==android.R.id.home){
这个;
}
返回super.onOptionsItemSelected(项目);
}
}
Doa文件:

package com.example.mybugetssimple.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

import java.util.List;

@Dao
public interface QueryDao {

    @Insert
    void category(Category ob);
    @Insert
    void category(Category...ob);

    @Insert
    void expense(Expense ob);
    @Insert
    void expense(Expense...ob);

    @Insert
    void income(Income ob);
    @Insert
    void income(Income...ob);

    @Insert
    void register(Register ob);

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void registerUser(Register ob);

    @Query("SELECT * FROM Category")
    LiveData<List<Category>> getAllCategoryList();
    @Query("SELECT * FROM EXPENSE")
    List<Expense> getAllExpenseList();
    @Query("SELECT * FROM INCOME")
    List<Income> getAllIncomeLIst();

    @Query("SELECT * FROM REGISTER WHERE Email=:emailId and password=:passwordId")
    Register getUser(String emailId, String passwordId);

    @Query("SELECT *FROM Register")
    List<Register> getUser();

    @Query("SELECT Email FROM REGISTER WHERE Email =:emailId")
    Register getEmail(String emailId);

    @Query("SELECT category_id, category_name FROM CATEGORY WHERE category_type =:catType")
     List<Category> getByCategory(String catType);
}

package com.example.mybugetssimple.database;
导入androidx.lifecycle.LiveData;
导入androidx.room.Dao;
导入androidx.room.Insert;
导入androidx.room.OnConflictStrategy;
导入androidx.room.Query;
导入java.util.List;
@刀
公共接口查询道{
@插入
无效类别(ob类别);
@插入
无效类别(类别…ob);
@插入
作废费用(费用ob);
@插入
作废费用(费用…ob);
@插入
无效收入(收入ob);
@插入
无效收入(收入…ob);
@插入
无效登记册(登记册ob);
@插入(onConflict=OnConflictStrategy.IGNORE)
无效登记员(登记员ob);
@查询(“从类别中选择*)
LiveData getAllCategoryList();
@查询(“从费用中选择*)
List getAllExpenseList();
@查询(“从收入中选择*)
List getAllIncomeLIst();
@查询(“从注册表中选择*,其中Email=:emailId和passwordId=:passwordId”)
注册getUser(字符串emailId、字符串passwordId);
@查询(“从注册表中选择*)
List getUser();
@查询(“从注册表中选择电子邮件,其中电子邮件=:emailId”)
注册getEmail(字符串emailId);
@查询(“从category\u type=:catType的category中选择category\u id、category\u name”)
列表getByCategory(字符串类型);
}

//Afaik setItems接收字符串数组。所以看起来您应该生成包含类别名称的字符串[]catNames,并将其用于setItems

用户按“确定”后,您应该查找在对话框中选择的类别名称为category_的类别

final AlertDialog.Builder builder = new AlertDialog.Builder(AddExpenseActivity.this);
        builder.setTitle( "Select Income Category" );

        String[] catNames = new String[categories.size()];
        for (int i=0; i<categories.size(); i++ ){
            catNames[i] = categories.get(i).getName();
        }

        builder.setItems( catNames, new DialogInterface.OnClickListener() {....
final AlertDialog.Builder=new AlertDialog.Builder(AddExpenseActivity.this);
建筑商。设置标题(“选择收入类别”);
String[]catNames=新字符串[categories.size()];

对于(int i=0;我能告诉我如何编辑答案吗?请编辑答案。看看是否应该修复方法或参数名称。如果有效,请接受答案
final AlertDialog.Builder builder = new AlertDialog.Builder(AddExpenseActivity.this);
        builder.setTitle( "Select Income Category" );

        String[] catNames = new String[categories.size()];
        for (int i=0; i<categories.size(); i++ ){
            catNames[i] = categories.get(i).getName();
        }

        builder.setItems( catNames, new DialogInterface.OnClickListener() {....