Binja
Binja

Reputation: 121

How to add list.java into main.java in android

I am working on android media player. In main.java it includes main.xml and list.java includes
list.xml , I used intent to call list.java (when I press imagebutton) into main.java , But when I press imagebutton list.xml comes up new window I want to show in bottom of the main.xml

In main.java image button calls list.java into main.java

songslist_button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        Intent i = new Intent(main.this, list.class);
        Toast.makeText(main.this, "Song List", Toast.LENGTH_SHORT).show();

        startActivityForResult(i, 100); 
        //Intent i = new Intent(main.this, list.class);
        //startActivity(i);
    }
});


/////////////////////////

public class list extends ListActivity 
{
    // Songs list
    public ArrayList<HashMap<String, String>> songsLists = new ArrayList<HashMap<String, String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);//list.xml

        ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

        SongList plm = new SongList();
        // get all songs from sdcard
        this.songsLists = plm.getPlayList();

        // looping through playlist
        for (int i = 0; i < songsLists.size(); i++)
        {
            // creating new HashMap
            HashMap<String, String> song = songsLists.get(i);

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

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

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();
        //lv = (ListView) findViewById (R.layout.list);

        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            {
                // getting listitem index
                int songIndex = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),main.class);
                // Sending songIndex to PlayerActivity
                in.putExtra("songIndex", songIndex);
                setResult(100, in);
                // Closing PlayListView
                finish();
            }
        });
    }
}

Upvotes: 0

Views: 107

Answers (1)

Opiatefuchs
Opiatefuchs

Reputation: 9870

Is Your main.java class an activity? Your list ist even an activity, so You started a new one, that will be shown in a new window. I think You need another aproach to do this. Read this tutorials first:

http://www.vogella.com/articles/AndroidListView/article.html

http://windrealm.org/tutorials/android/android-listview.php

A possibillity to do such thing is to integrate a simple listView into your main.xml layout and hide this view until button will be pressed.

EDIT

for example:

    <ListView android:layout_width="fill_parent"   
      android:layout_height="fill_parent"   
      android:id="@+id/mainListView">  
    </ListView>

put this into Your main.xml, maybe below Your button. Then You can reference the listview in your main.java:

    mainListView = (ListView) findViewById( R.id.mainListView );   

But there is a lot more to do, so I recommend You to read the tutorials to get a clear conception about how to build a listView.

Upvotes: 1

Related Questions