0

I have an Adapter which extend ArrayAdapter. With this adapter class I manage a ListView inside MainActivity.
I also have some buttons inside each row of the list and when I click on a button I want an AlertDialog to be shown. In doing so, I got an error about Theme.
I read about changing theme inside my manifest, but it does not work even if I change.

holder.delete=(ImageButton)row.findViewById(R.id.delete_btn_item);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder dialog= new AlertDialog.Builder(getContext());
                dialog.setTitle("Deleting...");
                dialog.setMessage("You are deleting DEFINITELY <" + db.getAllRecipes().get(position).getRec_name() + ">. Are you sure to continue?");
                dialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        db.deleteRecipe(db.getAllRecipes().get(position));
                        dialog.dismiss();
                        Toast.makeText(getContext(), "deleted " + db.getAllRecipes().get(position).getRec_name(), Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.create();
                dialog.show();
            }
        });

I have the holder inside the getView method. My first code lines are:

public class RecipeAdapter extends ArrayAdapter {
DatabaseHelper db;
LayoutInflater layoutInflater;
private Context context;
PopupWindow popUpWndw;


List list= new ArrayList();
public RecipeAdapter(Context context, int resource) {
    super(context, resource);
   this.context = context;
   db=new DatabaseHelper(context.getApplicationContext());
}

The error I get is this:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Linh
  • 57,942
  • 23
  • 262
  • 279
NioAnt
  • 13
  • 2
  • try change `AlertDialog.Builder dialog= new AlertDialog.Builder(getContext());` to `AlertDialog.Builder dialog= new AlertDialog.Builder(context)`; – Linh May 26 '16 at 09:22
  • It seems you're mixing Support Library classes with "standard" one. Check that your Activity and dialogs have a match. – fasteque May 26 '16 at 09:22
  • Can you please show us your `AndroidManifest.xml`, `style.xml` & error log. – Kasim Rangwala May 26 '16 at 09:22
  • 1
    Possible duplicate of [You need to use a Theme.AppCompat theme (or descendant) with this activity](http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity) – fasteque May 26 '16 at 09:23
  • I passed context instead of getContext(), but the error message still remain the same – NioAnt May 26 '16 at 09:49
  • also I added in the manifest android:theme="@style/Theme.AppCompat.Light"...but it's the same... – NioAnt May 26 '16 at 09:52

5 Answers5

0

You need to add android:theme="@style/Theme.AppCompat.Light" to application tag in the AndroidManifest.xml file.

Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
0

pass your mainactivity context to your custom adaptor and use that context to create your alertdialog

 yourAdaptor(this); // this is mainactivity context 

YourAdaptor

private Context mContext;

    public yourAdaptor(Context context){
    mContext = context;
    }

    AlertDialog.Builder dialog= new AlertDialog.Builder(mContext );

Update ::

just pass your global context instance of adaptor.

AlertDialog.Builder dialog= new AlertDialog.Builder(context);
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
0

Use below theme for this activity in your menifest file

android:theme="@style/Theme.AppCompat.Light"
Naveen Shriyan
  • 1,266
  • 1
  • 9
  • 15
0

You are passing the context to the adapter class through its constructor and it is saved in the variable "context".So use context instead of getcontext(). It worked for me.I think this will help you.

    holder.delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            AlertDialog.Builder dialog= new AlertDialog.Builder(context);
            dialog.setTitle("Deleting...");
            dialog.setMessage("You are deleting DEFINITELY <" + db.getAllRecipes().get(position).getRec_name() + ">. Are you sure to continue?");
            dialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    db.deleteRecipe(db.getAllRecipes().get(position));
                    dialog.dismiss();
                    Toast.makeText(context, "deleted " + db.getAllRecipes().get(position).getRec_name(), Toast.LENGTH_SHORT).show();
                }
            });
            dialog.create();
            dialog.show();
        }
    });
Phoenix
  • 238
  • 2
  • 12
0

change your style.xml with below code :-

values

"styles.xml"

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
</resources>

Androidmanifest.xml

 <activity
    android:name="com.package name.your activity"
    android:theme="@style/AppTheme">
 </activity>
Patel Vicky
  • 766
  • 8
  • 17