Android-在listview点击上打开新活动

时间:2011-01-11 21:57:51

标签: android android-widget

我试图这样做,以便当我点击列表视图中的特定项目时,它将带我到特定的屏幕。有谁知道如何做到这一点?我正在使用下面的代码

此外。我试图让单个后退按钮出现在列表视图的底部。到目前为止,我只能让它出现在列表视图中的每个条目上,非常感谢帮助!

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Advertise extends ListActivity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
                "Ubuntu", "Solaris", "Android", "iPhone" };
        // Use your own layout and point the adapter to the UI elements which contains the label
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.advertise,
                R.id.label, names));


    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                .show();


        {

        } 
    }
}

1 个答案:

答案 0 :(得分:6)

以这种方式开始活动。

Intent intent = new Intent("com.mysite.myapp.SOME_NEW_ACTIVITY");   
startActivity(intent); 

您不需要ListView中的后退按钮,您的硬件“后退”按钮也会这样做。