androidandroid-fragmentsandroid-tabbed-activity

Tabbed Activity Android


I'm developing an android app where I have 3 tabs: order status, order details, order Info. my problem is that I get an error message when I try to add Fragment to this app. here is my code:

orderTabbedFraagment.java

package com.example.kamran.bluewhite;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

public class OrderInfoTabbed extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_info_tabbed);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_order_info_tabbed, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_order_info_tabbed, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    orderInfoFragment a = new orderInfoFragment();
                    return a;
            }
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Order Details";
                case 1:
                    return "Order Info";
                case 2:
                    return "Order Status";

            }
            return null;
        }
    }
}

orderInfoFragment .java

package com.example.kamran.bluewhite;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

/**
 * Created by rahafo on 12/5/2017.
 */


public class orderInfoFragment extends Fragment{
    double sum=0;
    DatabaseReference mDatabase;
    ListView shoppingList;
    ArrayList<Shopping_cart> Shopping_cart;
    DatabaseReference Shopping_cartRef;
    DatabaseReference ShoppingcartProduct;
    ProgressDialog mProgressDialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.activity_order_info,container,false);

        mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setMessage("Loading Products..");
        mProgressDialog.show();
        Intent i = getActivity().getIntent();

        String uid = i.getExtras().getString("name","");
        Shopping_cart = new ArrayList<Shopping_cart>();
        LinearLayout productList = (LinearLayout) view.findViewById(R.id.shoppingList);

        mDatabase = FirebaseDatabase.getInstance().getReference();


        Shopping_cartRef = mDatabase.child("Order");
        ShoppingcartProduct = Shopping_cartRef.child(uid).child("products");

        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {

                    Shopping_cart product = ds.getValue(Shopping_cart.class);

                    Shopping_cart.add(product);
                    adapterShopping itemsAdapter = new adapterShopping(getActivity(), Shopping_cart);
                    shoppingList = (ListView) view.findViewById(R.id.listViewshopping);
                    shoppingList.setAdapter(itemsAdapter);



                }

                mProgressDialog.dismiss();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        };
        ShoppingcartProduct.addListenerForSingleValueEvent(eventListener);



        return view;
    }
}

The error I got is: Error:(134, 28) error: incompatible types: orderInfoFragment cannot be converted to Fragment but orderInfoFragment is extending Fragment so I don't know what is the problem! Did I do something wrong?


Solution

  • You are mixing imports which is quite common issue. Either you need to stick to support library provided components like Fragments and stuff (so you should have import android.support.... entries) - this is what you have in OrderInfoTabbed class code. Or use plaftorm provided Fragments and stuff - as you did in orderInfoFragment class. But while you can mix them, and technically it is possible to use both in your code on purpose, these are NOT equivalent and as you can see cannot be cast from one to the other.

    As solution, remove import android.app.... entries and re-add it (see Android Studio can help you) bit this time pay attention to which one you choose as AS will show you list of options.

    If unsure which one you want stick to support lib's one which is what I believe you wanted anyway.