1

I would like to send the file variable from the bookListView LongClickListener to the switch statements from the QuickAction code. How would I do this?

BookListView code:

bookListView.setOnItemLongClickListener(
    new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            String book = String.valueOf(parent.getItemAtPosition(position));
            File file = new File (path + "/" + book);
            mQuickAction.show(view);
            return true;
        }
    }
);

QuickAction code:

ActionItem editItem = new ActionItem(ID_EDIT, "Edit", getResources().getDrawable(R.drawable.edit_icon));
ActionItem deleteItem = new ActionItem(ID_DELETE, "Delete", getResources().getDrawable(R.drawable.delete_icon));

final QuickAction mQuickAction  = new QuickAction(this);
mQuickAction.addActionItem(editItem);
mQuickAction.addActionItem(deleteItem);

mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
    @Override
    public void onItemClick(QuickAction quickAction, int pos, int actionId) {
        ActionItem actionItem = quickAction.getActionItem(pos);

        switch (actionId) {
            case ID_EDIT:
                Toast.makeText(MainActivity.this, "go", Toast.LENGTH_SHORT).show();
                break;
            case ID_DELETE:
                 Toast.makeText(MainActivity.this, "Delete book", Toast.LENGTH_SHORT).show();
                 break;
            }
        }
    });

    mQuickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
        @Override
        public void onDismiss() {
            //close quickaction
        }
    });

   ...
});

1 Answer 1

1

You could use a object, which is responsible for managing the file, like:

public class FileManager {

    private File file;

    public void setFile(File file) {
        this.file = file;
    }

    public File getFile() {
        return this.file;
    }
}

The FileManager could be shared between the methods:

FileManager fileManager = new FileManager();

bookListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String book = String.valueOf(parent.getItemAtPosition(position));
        File file = new File (path + "/" + book);
        fileManager.setFile(file);
        mQuickAction.show(view);
        return true;
    }
});

and than you could call the get method:

mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
    @Override
    public void onItemClick(QuickAction quickAction, int pos, int actionId) {
        ActionItem actionItem = quickAction.getActionItem(pos);
        File file = fileManager.getFile();

        switch (actionId) {
            case ID_EDIT:
                Toast.makeText(MainActivity.this, "go", Toast.LENGTH_SHORT).show();
                break;
            case ID_DELETE:
                Toast.makeText(MainActivity.this, "Delete book", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    });

    ...
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.