在arrayadapter中传递视图为snackbar

时间:2017-12-07 15:39:21

标签: java android view android-arrayadapter snackbar

我正在尝试在arrayadapter中传递View对象,以打开快餐栏。 我需要将视图传递给它,但在ClickListener不起作用之前进行最终视图。

如何通过PopupMenu.OnMenuItemClickListener和DialogInterface.OnClickListener

获取视图变量

我想避免创建一个单独的OnMenuItemClickListener来传递参数。

@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;

1 个答案:

答案 0 :(得分:0)

您需要将rootView的{​​{1}}或Fragment的{​​{1}} ViewGroup传递给您Activity的{​​{1}}它的构造函数。

将此视图保留为ArrayAdapter的字段,并将其用于显示ArrayAdapter

SnackBar想要将其显示在底部的视图,我认为这不是适配器SnackBar方法的结果。