I Am using Room database and I want to retrieve data from Category table and display that in 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:
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. So it looks like You should generate String[] catNames that will contain names of the categories, and use it with setItems.
After user presses ok, You should look for a category with category_name as one that was selected in a dialog.
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() {....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.