简体   繁体   中英

Using the same 'OnClickListener' in different Activities

I was defining and using a custom OnClickListener (ie MyOnClickListener ) inside the file of the Fragment I was using at the moment.

Keeping writing code I realized that I needed the same listener also in another Fragment that happened to be in another Activity as well.

I created therefore the file MyOnClickListener.java copying all the code I was using in the first Fragment for it earlier, but now I get the following errors:

Cannot resolve method 'getActivity()'

Cannot resolve method 'getResources()'

Note: I read on stackoverflow that one solution may be just writing MainActivity.this in place of getActivity() , but in my situation I need to use the same in two different activities . What should I do?

EDIT: Here's the code of MyOnClickListener It just has to display a matrix of icons:

class MyOnClickListener implements View.OnClickListener {

    private LabeledButton labeledButton;

    MyOnClickListener(LabeledButton labeledButton) {
    super();
    this.labeledButton = labeledButton;
}

@Override
public void onClick(View view) {
    Button iconButton = (Button) view;
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // ERROR HERE
    LayoutInflater layoutInflater = getActivity().getLayoutInflater(); // ERROR HERE
    final View viewLayout = layoutInflater.inflate(R.layout.dialog_matrix_icons, null);
    builder.setView(viewLayout);
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Do nothing
        }
    });
    final AlertDialog alertDialog = builder.create();

    // Set-up listeners of icon button
    Button imageButtons[][] = new Button[3][3];
    int i;
    int j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            Resources res = getResources(); // ERROR HERE
            TypedArray icons = res.obtainTypedArray(R.array.listIcon);
            int idDrawable = icons.getResourceId(i + 3 * j, 1); // ERROR HERE
            icons.recycle();
            LinearLayout grid = (LinearLayout) viewLayout;
            LinearLayout row = (LinearLayout) grid.getChildAt(i);
            imageButtons[i][j] = (Button) row.getChildAt(j); // Retrieve the right image in the grid
            imageButtons[i][j].setBackgroundResource(idDrawable);
            String nameIcon = getResources().getResourceEntryName(idDrawable); // ERROR HERE
            ImageOnClickListener imageOnClickListener = new ImageOnClickListener(iconButton, alertDialog, idDrawable, nameIcon, labeledButton);
            imageButtons[i][j].setOnClickListener(imageOnClickListener);
        }
    }
    alertDialog.show();
}

}

Replace this with your constructor

MyOnClickListener(LabeledButton labeledButton,Context context) {
   super();
   this.labeledButton = labeledButton;
   this.context = context;
}

Make sure you create context variable inside your MyOnClickListenerClass if not you will find error at the constructor line.Then you can replace all the getActivity() to context .

When you initiallize your MyOnClickListener make sure to pass the context parameter

For your inflater use this

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

I read on stackoverflow that one solution may be just writing MainActivity.this in place of getActivity(), but in my situation I need to use the same in two different activities. What should I do?

If you put the OnClickListener in its own compilation unity, you can't use whawt you read on Stackoverflow . MainActivity.this , refers to a concrete and current instance of MainActivity (please, look it up what the keyword this means in java). If you need a Context, you can the parameter you get in the onClick callaback, View view , to retrieve it. Read more here

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM