Android- Open new activity on listview clicks

让人想犯罪 __ 提交于 2019-12-10 13:09:51

问题


Im trying to make it so that when i click specific items in my listview, it will take me to specific screens. Does anyone know how to do this? Im using the code below for this

Furthermore. Im trying to make a single back button appear at the bottom of the listview. So far i can only make it appear on every entry in the listview, help would be greatly appreciated!

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:


Start Activity this way.

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

You don't need back button in the ListView, your hardware 'Back' button will do the same.



来源:https://stackoverflow.com/questions/4663092/android-open-new-activity-on-listview-clicks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!