1

I have a custom Adapter and within its getView() method, I'm trying to launch an alert dialog when the user clicks a Button but I get an error. I hope someone can help me here

Here is my Custom Adapter:

package com.example.android.testappsfeatures;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


/**
 * Created by korchix on 05.02.17.
 */

public class UserAdapter extends ArrayAdapter<UserCard> {

    Context context = getContext().getApplicationContext();    

    public UserAdapter(Context context, ArrayList<UserCard> userCards) {
        super(context, 0, userCards);
    }


    // override getView because where are using other view than just TV alone.
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        // get the data item for this position
        final UserCard newUser = getItem(position);
        final ViewHolder holder = new ViewHolder();
        // check if an existing view is being reused, otherwise inflate the view
        if(convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.user_layout, parent, false);
        }

        View userCardbackgroundColor = convertView.findViewById(R.id.userCard);
        userCardbackgroundColor.setBackgroundColor(Color.WHITE);


        // Lookup view for data population
        holder.userNameTV = (TextView) convertView.findViewById(R.id.name_textView);
        holder.userNameTV.setText(newUser.getUserName());

        holder.gutHabenTV = (TextView) convertView.findViewById(R.id.guthaben_textView);
        holder.gutHabenTV.setText(NumberFormat.getCurrencyInstance().format(Double.parseDouble(newUser.getGuthaben())));

        holder.timeStampTV = (TextView) convertView.findViewById(R.id.datumUhrzeit_textVie);
        holder.timeStampTV.setText(newUser.getTimeStamp());

        holder.plusButton = (Button) convertView.findViewById(R.id.plusButton_Layout);
        holder.plusButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

         **// HERE IS THE PROBLEM**
                FragmentManager fm = ((AppCompatActivity) context).getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
                AddBalanceDialog addBalanceDialog = new AddBalanceDialog();
                fragmentTransaction.replace(R.id.containerLayoutID, addBalanceDialog);
                fragmentTransaction.commit();

            }
        });
        return convertView;
    }
}



class ViewHolder {
    TextView userNameTV;
    TextView gutHabenTV;
    TextView timeStampTV;
    Button plusButton;
    Button minusButton;
}

And this is the code for the AlertDialog (DialogFragment)

package com.example.android.testappsfeatures;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by korchix on 04.02.17.
 */

public class AddBalanceDialog extends DialogFragment {
    Context context;
    InputUserDataDialog inputUserDataDialog = new InputUserDataDialog();




    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        context = getActivity().getApplicationContext();
        //user Dialog class for convenient dialog construction
        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        // get the layoutInflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        // inflate our custom Layout for the dialog to a View
        View view = inflater.inflate(R.layout.add_guthaben, null);

        final TextView guthabenTV = (TextView) view.findViewById(R.id.guthaben_textView);

        final EditText addGuthabenET = (EditText) view.findViewById(R.id.add_guthaben_LayoutET);

        // inform the dialog it has a custom View
        dialog.setView(view);

        // when positiv button clicked
        dialog.setPositiveButton(R.string.dialog_ok_buttom, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String input = addGuthabenET.getText().toString();

               guthabenTV.setText(input);


                // hier muss noch was
                // gemacht werden
            }
        })
                .setNegativeButton(R.string.dialog_cancel_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // discard the input -> don't make any change
                        dialog.cancel();

                    }
                });

        return dialog.create();
    }

}

I got this error when I clicked on plusButton.setOnClickListener(new View.OnClickListener()

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.android.testappsfeatures, PID: 30548
java.lang.ClassCastException: android.app.Application cannot be cast to android.support.v7.app.AppCompatActivity
at com.example.android.testappsfeatures.UserAdapter$1.onClick(UserAdapter.java:83)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

As i read here it seems that the problem is related with the context, I'm giving Application Context, but I should give Activity context.I don't know how to do this.

Here is my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.testappsfeatures">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".InputGuthabenActivity"
            android:label="@string/addGuthabenTitle"
            android:theme="@style/Theme.AppCompat.Light.Dialog.Alert" />
    </application>

</manifest>
Onur A.
  • 3,007
  • 3
  • 22
  • 37
korchix
  • 11
  • 4

3 Answers3

0

You are passing an Application context and you are trying to cast it back to a FragmentActivity which is wrong. You can also pass the Activity reference directly.

Change

context = getActivity().getApplicationContext();

to

context = getActivity();
Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
  • no i can't, when do Context context = getActivity(); i get an error : Cannot resolve Methode 'getActivity()' – korchix Feb 12 '17 at 16:02
  • okok :) i removed getApplicationContext() but i get the same erro: java.lang.ClassCastException: android.app.Application cannot be cast to android.support.v7.app.AppCompatActivity at FragmentManager fm = ((AppCompatActivity) context).getSupportFragmentManager(); – korchix Feb 12 '17 at 16:13
0

In the constructor of your adapter, there is a context parameter. Declare this as a field with this.context = context;. Then use this context whenever you need context in your adapter.

Mehmet K
  • 2,805
  • 1
  • 23
  • 35
  • i did this: `public class UserAdapter extends ArrayAdapter { Context mContext; public UserAdapter(Context context, ArrayList userCards) { super(context, 0, userCards); this.mContext = context; }` but i got the same error : java.lang.ClassCastException: android.app.Application cannot be cast to android.support.v7.app.AppCompatActivity – korchix Feb 12 '17 at 17:42
  • Did you change the rest of your code? " Then use this context whenever you need context in your adapter." I am also implying that you shouldn't cast it anything – Mehmet K Feb 12 '17 at 17:43
  • You should probably read http://stackoverflow.com/questions/3572463/what-is-context-on-android, http://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext, and http://stackoverflow.com/questions/6229052/difference-in-context-this-and-getcontext – Mehmet K Feb 12 '17 at 17:46
  • yes i used mContext wherever a context is needed... ok do you have another way or code to run the AddBalanceDialog (dialogFragment) when the holder.plusButton.setOnClickListener() in UserAdapter is clicked ?? – korchix Feb 12 '17 at 17:49
  • Did you get rid of the cast here: `((AppCompatActivity) context)`. This is causing your problem. Youre trying to cast an `Application` as an `AppCompatActivity`, and it is not one – Mehmet K Feb 12 '17 at 17:50
  • yes i did it also, but in ths case i can't user getSupportFragmentManager() or getFragmentManager() those methods are then not resolved – korchix Feb 12 '17 at 17:53
  • Ok now cast is as the type of the Activity youre using – Mehmet K Feb 12 '17 at 17:54
  • But seriously, if youre having this much trouble with this, you should really go read about what context is – Mehmet K Feb 12 '17 at 17:55
  • I'm not using any activity in this case.... ok thank you, i thin i should do it, thank you for your help – korchix Feb 12 '17 at 18:10
0

ok finaly i resolved it !!! for the people that are interested in the answer, here how i did it:

in the MainActivity.class where i instantiate the ArrayAdapter, i changed the Context from getApplicationContext to this like this

UserAdapter userAdapter = new UserAdapter(this, arrayOfUsersNames, fm);

and i added the FragmentManager in the Constructor of my UserAdapter like this

public class UserAdapter extends ArrayAdapter<UserCard>{
    Context mContext;
    AddBalanceDialog addBalanceDialog = new AddBalanceDialog();
    FragmentManager mFragmentManager;

    public UserAdapter(Context context, ArrayList<UserCard> userCards, FragmentManager fragmentManager) {

        super(context, 0, userCards);
        mContext = context;
        mFragmentManager = fragmentManager;
    }

then in the onClick() where the button is clicked, i added this line of code, to show the Alert Dialog 'addBalanceDialog.show(mFragmentManager, "test");' and it worked :)

korchix
  • 11
  • 4