[英]how to add list data from Room database to Alert Dialog in android
I Am using Room database and I want to retrieve data from Category table and display that in Alert Dialog我正在使用 Room 数据库,我想从 Category 表中检索数据并将其显示在 Alert Dialog 中
final List<Category> categories = database.getQuery().getByCategory("Income");
but there it is showing error like this:但它显示这样的错误:
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[])
package com.example.mybugetssimple;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import com.example.mybugetssimple.database.Category;
import com.example.mybugetssimple.database.ExpenseDatabase;
import com.google.android.material.snackbar.Snackbar;
import java.util.List;
public class AddExpenseActivity extends AppCompatActivity{
TextView Date,catType;
EditText amount,desc;
ExpenseDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_expense);
//Add back Button
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
catType = findViewById(R.id.addExInputTv);
Date =findViewById(R.id.addExDateInputTv);
amount = findViewById(R.id.addExAmountInputEt);
desc = findViewById(R.id.addExDesInputEt);
database = ExpenseDatabase.getDatabaseInstance(this);
final List<Category> categories = database.getQuery().getByCategory("Income");
System.out.println(categories);
catType.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(AddExpenseActivity.this);
builder.setTitle( "Select Income Category" );
builder.setItems( categories, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
catType.setText(categories.get(which).getCategory_name());
}
} );
builder.setNegativeButton( "CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
builder.setCancelable( true );
}
} );
builder.setPositiveButton( "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == -1) {
} else {
catType.setText( categories.get(which).getCategory_name());
}
}
} );
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
} );
}
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if(id == android.R.id.home){
this.finish();
}
return super.onOptionsItemSelected(item);
}
}
Doa file: 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);
}
// //
Afaik setItems receives a String array. Afaik setItems 接收一个字符串数组。 So it looks like You should generate String[] catNames that will contain names of the categories, and use it with setItems.
所以看起来您应该生成包含类别名称的 String[] catNames,并将其与 setItems 一起使用。
After user presses ok, You should look for a category with category_name as one that was selected in a dialog.用户按确定后,您应该查找 category_name 为在对话框中选择的类别。
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() {....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.