leo
leo

Reputation: 35

How do i excute onCreate method by creating an instance of a class in android

I have a MainActivity.java and SongLoad.java. The code of MainActivity.java is

 public class MainActivity extends Activity {

public static final int LOADREQUEST = 100;
private String callerClass = "DoubleMode";

ImageButton loadPlaylist1,loadPlaylist2;
ImageButton play1,play2;


SongLoad songloader = new SongLoad(callerClass);
MusicPlayerService musicplayer = new MusicPlayerService();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homescreen);
    loadPlaylist1 = (ImageButton)findViewById(R.id.load_button1);
    loadPlaylist2 = (ImageButton)findViewById(R.id.load_button2);
    play1 = (ImageButton)findViewById(R.id.play1);
    play2 = (ImageButton)findViewById(R.id.play2);
}

public void onLoad(View v) {
    int loadButtonPressed = v.getId();
    songloader.playListGenerator();
    musicplayer.songLoader(songloader.songIndex, loadButtonPressed);.. soon       

SongLoad.java

public class SongLoad extends ListActivity{
public int songIndex = 0;
public String selectedSongPath;
public String callerClass;
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath());
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongLoad(String callerClass) {
    this.callerClass = callerClass; 
}
SongLoad() {

}

@Override
public void onCreate(Bundle savedInstanceState)  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    if(callerClass == "DoubleMode")
    {
        setContentView(R.layout.playlist);
    }
}

public void playListGenerator() {

    mediaScanner(); 
    for (int i = 0; i < songsList.size(); i++)  {
        // creating new HashMap
        HashMap<String, String> song = songsList.get(i);

        // adding HashList to ArrayList
        songsListData.add(song);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, songsListData,
            R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
                    R.id.songTitle });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();
    // listening to single listitem click
    lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        // getting listitem index
        songIndex = position;
        selectedSongPath = songsList.get(songIndex).get("songPath");

    }
    });
}

Now my question is that when I create a object of SongLoad.java and call playListGenerator onCreate method is called. However I want it to execute. How do i do that?

Upvotes: 0

Views: 932

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006724

How do i excute onCreate method by creating an instance of a class in android

You don't.

when I create a object of SongLoad.java

Delete this code. Android creates instances of activities; you do not. Also:

  • Delete the SongLoad empty constructor

  • Move the call to playListGenerator() from MainActivity to the onCreate() method of SongLoad

  • Start up a SongLoad activity by calling startActivity() from the onLoad() method of MainActivity

Each activity works in relative isolation, just as one Web page of a Web app does not call JavaScript functions from some other Web page in the same Web app.

You will eventually need other changes, such as the proper use of background threads when doing I/O, but that will get you started.

You should also consider reading the documentation on activities.

Upvotes: 1

Related Questions