Aina Syazwani
Aina Syazwani

Reputation: 5

How to call the whole class into Main Activity?

I'm making an android mobile app. I attempted to save data in MainActivity. But there is so much data, I found its not suitable to keep them in MainActivity. So, I decided to create a java class, called Data to keep the code. But somehow, I cannot call a Data class.

Data.java

package com.example.lenovo.pointofinterest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class Data extends AppCompatActivity {

    //a list to store all the places
    List<Places> placesList;

    //the recyclerview
    RecyclerView recyclerView;

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

        //getting the recyclerview from xml
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //initializing the placesList
        placesList= new ArrayList<>();


        //adding some items to our list
        placesList.add(
                new Places(
                        1,
                        "Adventure Zone Theme Park",
                        "13.3 inch, Silver, 1.35 kg",
                        "Amusement Park",
                        "10.00am - 7.00pm (Daily)",
                        R.drawable.adventure_zone_theme_park));

        placesList.add(
                new Places(
                        1,
                        "Escape Penang Theme Park",
                        "14 inch, Gray, 1.659 kg",
                        "Amusement Park",
                        "10.00am - 6.00pm (Tue - Sun)",
                        R.drawable.escape_penang_theme_park));

        placesList.add(
                new Places(
                        1,
                        "Countryside Stables Penang",
                        "13.3 inch, Silver, 1.35 kg",
                        "Animal Lover",
                        "2.00pm - 7.00pm (Daily)",
                        R.drawable.countryside_stables_penang));

        //creating recyclerview adapter
        PlacesAdapter adapter = new PlacesAdapter (this, placesList);

        //setting adapter to recyclerview
        recyclerView.setAdapter(adapter);
    }
}

MainActivity.java

package com.example.lenovo.pointofinterest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity{


}

I tried to edit the code by following a tip found here where it might be due to onCreate, but I still cannot fix it. Please help. Thanks.

Upvotes: 0

Views: 73

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191711

I feel like you meant to do this

public class Data {

    //a list to store all the places
    public static List<Places> placesList = Arrays.asList(new Places(), new Places());

}

And that's it. Then in MainActivity (which still needs to extend some Activity class), you can just reference Data.placesList to put into an adapter

PlacesAdapter adapter = new PlacesAdapter(MainActivity.this, Data.placesList);

You can put the adapter into the data class if you want, but the RecyclerView should be in used and found from the MainActivity.

Upvotes: 1

user12346352
user12346352

Reputation: 196

First of all your MainActivity Needs to extend AppComapActivity

Second you Need a on create in your MainActivity.

@Override
protected void onCreate(Bundle savedInstanceState) {
   Intent intent = new Intent(getApplicationContext(), YourClass.class);
   startActivity(intent);
}

Then you can call your class in the main Activity.

Upvotes: 0

Related Questions