ivesingh
ivesingh

Reputation: 888

Easier way to use Switch/Case Statement in Java Android

I have a getItem(int position) method of type Fragment. I have about more than 100 pages meaning every page is different I was trying to use switch and case statement. Every case returns a new class which is extends Fragment as shown below.

public class Pages{
public static class Page1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_main_dummy,
            container, false);
    final Button search = (Button) rootView.findViewById(R.id.clicktosearch);
    final EditText number = (EditText)rootView.findViewById(R.id.number);
    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for(int i = 0; i < MainActivity.Help.arr.length; i++){
                if(number.getText().toString().equals(MainActivity.Help.arr[i])){ 
                    MainActivity.mViewPager.setCurrentItem(i);
                }
            }
        }
    });
    WebView webview = (WebView) rootView
            .findViewById(R.id.webview);
    webview.loadUrl("file:///android_asset/2.html");
    return rootView;
      }

   }
}
public Fragment getItem(int position) {
    switch(position){
    case 1:
        Pages.Page1 page1 = new Pages.Page2();
        return page1; //same with other pages in different cases.

Is there any easier to do this rather than using switch/case statement, which will take really long. Thanks

Upvotes: 0

Views: 1108

Answers (2)

user93353
user93353

Reputation: 14049

Yes. There is an easier way

Page createPage(int index)
{
    // ignore the damn index
    return new Page();
}

Will do the same thing your function is doing. Since you don't seem to be doing anything to save the variable which stores the page being created.

On the other hand if wanted to store it somewhere you could have a array in the class (where this function exists) - say the class is a book.

public class Book
{
    private Page [] p;

    public Book()
    {
        p = new Page[1000];
        for(int i = 0; i < 1000; ++i)
            p[i] = new Page();
    }

    public Page createPage(int index)
    {
        return p[index];
    }
}

Or a lazy initialization

public class Book
{
    private Page [] p;

    public Book()
    {
        p = new Page[1000];
    }

    public Page createPage(int index)
    {
        if (p[index) != null)
            return p[index];

        p[index] = new Page();
        return p[index];
    }
}

Upvotes: 1

lelloman
lelloman

Reputation: 14183

You would like to create objects with a function

Page getPage(){
    return new Page();
}

I assume that each page has got it's own attributes, maybe a string or whatever. So you could store those values in an ArrayList<String> and set them to the page according to the index

Page getPage(int index){
    Page p = new Page();
    String attr = youArrayList.get(index);
    p.setAttrMethod(attr);
    return p;
}

Upvotes: 1

Related Questions