如何将list.java添加到android中的main.java中

时间:2012-11-26 04:28:40

标签: java android xml android-intent

我正在开发Android媒体播放器。在main.java中,它包含main.xml,list.java包含
list.xml,我用它来调用list.java(当我按下imagebutton时)到main.java中,但是当我按下imagebutton时,list.xml出现了新窗口,我想在main.xml的底部显示

在main.java图像按钮中调用list.java到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();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您的main.java类是活动吗?你的列表甚至是一个活动,所以你开了一个新的,它将在一个新窗口中显示。我想你需要另一个方法才能做到这一点。首先阅读本教程:

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

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

执行此类操作的可能性是将简单的listView集成到main.xml布局中并隐藏此视图,直到按下按钮。

修改

例如:

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

将它放入您的main.xml,也可以在您的按钮下方。然后您可以在main.java中引用listview:

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

但是还有很多工作要做,所以我建议您阅读教程以获得有关如何构建listView的清晰概念。