0

I have a class that extend RecyclerView.Adapter.

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
.
ArrayList<String> mDataset;
.
.
.

I added a new public member function to this class to return mDataset

public  ArrayList<String> getmDataset() {

    return mDataset;
}

the problem is when instantiate MainAdapter I cannot a reference getmDataset() in this class,

when I type new MainAdapter.getmDataset() it shows cannot resolve method error.

How can I reference getmDataset() ???

Shai
  • 355
  • 2
  • 5
  • 18
  • (new MainAdapter()).getmDataset() ??? – Access Denied Oct 11 '18 at 10:15
  • Post `MainAdapter.java`. I mean full code. – Bek Oct 11 '18 at 10:30
  • This is pretty much super basic: static methods dont need an object of the corresponding class, but they can only access static data. Non static methods can access non static fields, but you can only invoke them on an instance of the class. If such basics aren't in your knowledge yet ... I suggest you focus on java basics for a few weeks. Dont do "android", unless you are somehow proficient with basic java. – GhostCat Oct 11 '18 at 10:31
  • And unrelated: A) read about java naming convents ... using such suffix notation like **m**DataSet is bad practice B) when you keep using these names: do not expose them in the interface - that method should (if at all) be named `getData()` or something alike. And note the confusing naming here: you call it a *data* **set**, but you use a **list** as type? – GhostCat Oct 11 '18 at 10:33
  • To resume: By calling `MainAdapter.getmDataset()` you are trying to access a static method of the class `MainAdapter` and `getmDataset()` is not static. Use as `(new MainAdapter()).getmDatabaset()` and it will be acessible. – czdepski Oct 11 '18 at 11:39

1 Answers1

0

If you are referring getmDataset() from any class then you have to create instance of class like

MainAdapter ma=new MainAdapter();
ma.getmDataset();

Or from Same class you can write this.getmDataset();

Or if your mDataset is static member then you can make getmDataset() as static like

public static ArrayList<String> getmDataset() {
  return mDataset;
}

and call it MainAdapter.getmDataset()

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Tarun
  • 986
  • 6
  • 19