-5

I'm quite new in Android Studio. I am having a problem with setContentView. I know that it doesn't work in Fragment.

Anyone know how to fix this issue?

Error:

error: cannot find symbol method setContentView(int)

Code:

public class Tab1Fragment extends Fragment{
    private static final String TAG = "Tab1Fragment";


    Toolbar mToolbar;
    ListView mListView;

    String[] countryNames = {"Australia", "Brazil", "China", "France", "Germany", "India", "Ireland", "Italy"
            , "Mexico", "Poland", "Russia", "Spain", "US"};
    int[] countryFlags = {R.drawable.flag_australia,
            R.drawable.flag_brazil,
            R.drawable.flag_china,
            R.drawable.flag_france,
            R.drawable.flag_germany,
            R.drawable.flag_india,
            R.drawable.flag_ireland,
            R.drawable.flag_italy,
            R.drawable.flag_maxico,
            R.drawable.flag_poland,
            R.drawable.flag_russia,
            R.drawable.flag_spain,
            R.drawable.flag_us};


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment1_layout);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("My Application");
        mListView = (ListView) findViewById(R.id.listview);
        MyAdapter myAdapter = new MyAdapter(Tab1Fragment.this, countryNames, countryFlags);
        mListView.setAdapter(myAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent mIntent = new Intent(Tab1Fragment.this, DetailActivity.class);
                mIntent.putExtra("countryName", countryNames[i]);
                mIntent.putExtra("countryFlag", countryFlags[i]);
                startActivity(mIntent);
            }
        });
    }

    private Button btnTEST;
}
kasia
  • 1
  • 3
  • https://stackoverflow.com/q/12108370, https://stackoverflow.com/q/36870159, https://stackoverflow.com/q/16424538, https://stackoverflow.com/q/34079194, https://stackoverflow.com/q/6938560, https://stackoverflow.com/q/23034301, https://stackoverflow.com/q/37891837, https://stackoverflow.com/search?q=%5Bandroid%5D+fragment+setContentView – Mike M. Aug 15 '17 at 20:05
  • 3
    In other words I think @MikeM. is saying you should have spent 5 seconds and googled this question before asking. – Error - Syntactical Remorse Aug 15 '17 at 20:08

1 Answers1

1

In fragment you inflate the layout not setContentView the code maybe look like this

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment1_layout, container, false);

      // Put your fragment code here

        return rootView;
    }
Ahmed Abdelmeged
  • 2,299
  • 2
  • 18
  • 29
  • Thank you for your help @Ahmed Abd-Elmeged. Now it's easy to understand. but MyAdapter and Intent don't work. `MyAdapter myAdapter = new MyAdapter(MainActivity.this, countryNames, countryFlags);` `Intent mIntent = new Intent(MainActivity.this, DetailActivity.class);` do you know how should it look in Fragment? – kasia Aug 17 '17 at 09:24
  • Tell what exactly you trying to do – Ahmed Abdelmeged Aug 17 '17 at 09:26
  • I want to show the list of names and flags in this fragment. Here is tutorial: https://www.youtube.com/watch?v=q2XA0Pe2W04&t=334s where is possible to show in main activity. But I want to show it in antoher tab - 'Tab1Fragment'. – kasia Aug 17 '17 at 09:32
  • Of course I changed the MainActivity.this to Tab1Fragment.this in both cases – kasia Aug 17 '17 at 09:35
  • I think you should ask a new question for this with your current code and what you try to achieve and i will try to answer it after an hour – Ahmed Abdelmeged Aug 17 '17 at 09:37
  • Hi I want to add ListView in Fragment. I don't know that it's good idea, but I found this code in some project. But this code with ListView is used in Activity, but I need to create list in Fragment. I have several tabs in my app. – kasia Aug 17 '17 at 10:46
  • OK i will give you an example but it recycle inside a fragment that's OK ? – Ahmed Abdelmeged Aug 17 '17 at 16:07
  • Yes, it's ok. Thank you! – kasia Aug 18 '17 at 08:53
  • https://github.com/Ahmed-Abdelmeged/ADAS/blob/master/app/src/main/java/com/example/mego/adas/accidents/ui/AccidentFragment.java – Ahmed Abdelmeged Aug 18 '17 at 09:37