简体   繁体   中英

Passing view inside arrayadapter for snackbar

I am trying to pass a View object in an arrayadapter, to open a snackbar. I need to pass a view into it, but taking a final view before the ClickListeners does not work.

How can I get the view variable past the PopupMenu.OnMenuItemClickListener and DialogInterface.OnClickListener

I want to avoid creating a separate OnMenuItemClickListener to pass parameters.

@Override
public View getView(final int position,  View convertView, final ViewGroup parent) {
case R.id.exercise_options_area:

try {//setting texts for custom listView


            deleteArea.setOnClickListener(new View.OnClickListener() {//set to the linear layout

                @Override
                public void onClick(View v) {
final PopupMenu popup = new PopupMenu(parent.getContext(), v);
popup.getMenu().add(0, 0, 0, menuIconWithText(parent.getResources().getDrawable(R.drawable.ic_rename), "Rename"));
//if i try using v in snackbar in this line it works, but I don't want it here, I want it down there. using final view = v  does not work

popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        exerciseName = getItem(position);//no spaces, the exerciseName before rename
        switch (item.getOrder()) {
            case 0:///////////////////////RENAME

                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(parent.getContext());
                 View viewInput = (LayoutInflater.from(parent.getContext())).inflate(R.layout.user_input, null);
                final EditText inputExerciseName = viewInput.findViewById(R.id.userInput);

                alertBuilder.setView(viewInput);//show viewInput which is the editText prompt (user_input.xml)

                alertBuilder.setCancelable(true)
                        .setTitle("Enter New Exercise Name")
                        .setPositiveButton("Save", new DialogInterface.OnClickListener() {


                            @Override
                            public void onClick(DialogInterface dialog, int which) {//on clicking save
                                if (!mainList.contains(inputExerciseName.getText().toString())) {
                                    String renamed = inputExerciseName.getText().toString();
                                    renameItem(position, renamed);//rename in the list



                                    if (sp.contains(mostRecent +  workoutName + " " + exerciseName + " sets")){//only copy if data exists
                                        copySharedPreferencesRename(renamed, workoutName, exerciseName,sp,editor);//rename by copying data to new name
                                    }

                                    checkSharedPreferencesDeleteExercise(workoutName, exerciseName,sp,editor);//delete the data in old name
                                    editor.apply();//apply must be done here since this action is separate
                                    //refreshListView();//refresh view



                                    ((ExerciseList)mContext).recreate();//refresh activity. for past date it refreshes to current
                                    if (visitState.equals("pastDate")){


                                    }
                                    else
                                    *{
                                        simpleSnackBar("Renamed", "#424242", "#FFFFFF", **NEED VIEW HERE**, Snackbar.LENGTH_LONG );//800 grey, white
                                       //debug passes this point, but snackbar does not show with final view v
                                    }*
                                }
                                else{

                                    return;//make user enter again
                                }
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {//on clicking cancel
                            public void onClick(DialogInterface dialog, int which) {
                                InputMethodManager imm = (InputMethodManager) parent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);//remove keyboard
                                imm.hideSoftInputFromWindow(inputExerciseName.getWindowToken(), 0);
                                dialog.dismiss();
                            }
                        });

                AlertDialog dialog = alertBuilder.create();
                dialog.show();
                dialog.setCanceledOnTouchOutside(false);
                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

                break;

You need to pass the rootView of your Fragment or the root ViewGroup of your Activity to your ArrayAdapter when it's created in its constructor.

Hold this view as a field of the ArrayAdapter and use it for showing SnackBar s.

SnackBar wants the view that it's going to show at the bottom of, I assume that is not the result for your Adapter's getView() method.

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