在是/否对话框中从自定义ArrayAdapter访问变量

时间:2017-12-07 16:35:31

标签: android android-arrayadapter android-alertdialog

您好我希望我的代码有分离的方法来创建是/否对话框并创建与 ArrayAdapter 分开的 onClickListener (我不确定是否< em> dialogCreator()表现不错,但问题并非如此)

以下是我想要实现的目标:

public class FlashCardsAdapter extends ArrayAdapter<FileManager> {
interface Callback {
    void onFileDeleted(String fileName);
}

private final Callback mCallback;

public FlashCardsAdapter(Context context, ArrayList<FileManager> users, final Callback callback) {
    super(context, 0, users);
    mCallback = callback;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    final FileManager manager = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item, parent, false);
    }

    TextView mainTextView = convertView.findViewById(R.id.deck_name_id);

    Button addCardButton = convertView.findViewById(R.id.action_add_card);

    Button deleteDeckButton = convertView.findViewById(R.id.action_delete);

    mainTextView.setText(manager.getFileName());

    mainTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getContext(), FlashCardActivity.class);
            intent.putExtra("fileName", manager.getFileName());
            getContext().startActivity(intent);
        }
    });

    addCardButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getContext(), AddCardActivity.class);
            intent.putExtra("fileName", manager.getFileName());
            getContext().startActivity(intent);
        }
    });

    deleteDeckButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


        }
    });
    return convertView;
}

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                manager.deleteFile(); // I want to access manager in here
                mCallback.onFileDeleted(manager.getFileName()); // and here
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked
                break;
        }
    }
};

private void dialogCreator(){
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();
    builder.create();
}

}

经理是我想要在我的方法中访问的变量

1 个答案:

答案 0 :(得分:0)

如果您要访问同一variablemethod之外的class,则必须将variable的焦点设置为(目前你的变量是方法私有的)。

您只需将manager变量移出getView方法即可实现:

public class FlashCardsAdapter extends ArrayAdapter<FileManager> {
interface Callback {
    void onFileDeleted(String fileName);
}

private final Callback mCallback;
private FileManager manager; //in this way this variable is accessible to all method inside the class

public FlashCardsAdapter(Context context, ArrayList<FileManager> users, final Callback callback) {
    super(context, 0, users);
    mCallback = callback;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    manager = getItem(position); //here you initialize it

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item, parent, false);
    }

    TextView mainTextView = convertView.findViewById(R.id.deck_name_id);

    Button addCardButton = convertView.findViewById(R.id.action_add_card);

    Button deleteDeckButton = convertView.findViewById(R.id.action_delete);

    mainTextView.setText(manager.getFileName());

    mainTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getContext(), FlashCardActivity.class);
            intent.putExtra("fileName", manager.getFileName());
            getContext().startActivity(intent);
        }
    });

    addCardButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getContext(), AddCardActivity.class);
            intent.putExtra("fileName", manager.getFileName());
            getContext().startActivity(intent);
        }
    });

    deleteDeckButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


        }
    });
    return convertView;
}

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                manager.deleteFile(); // and here you can access it
                mCallback.onFileDeleted(manager.getFileName()); // and here too
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked
                break;
        }
    }
};

private void dialogCreator(){
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();
    builder.create();
}

}

希望这有帮助,如果我错过了解,请告诉我

注意:

可能更好的方法是在适配器中保存列表的引用并从位置检索项目(FileManager)(也可以覆盖getItem(position)方法)以避免与回收器相关的问题。