简体   繁体   中英

Android ListView- Opening new activity with each item

Hi I'm trying to get my listview to open a different activity with each part. As of now i can open an activity however they all open the same activity. I need to know to change that so for example:

item1 will open activity1 item2 will open activity2 and so forth.

Any help will be much appreciated, here is my code(Some of the comments are form a tutorial i followed so sorry for my inexperience):

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);

    //get listview from xml
    listView = (ListView)findViewById(R.id.listView);

    //defined array to show in listview
    String[] values = new String[]{"About the App",
                                    "How to Use",
                                    "Build log",
                                    };
    //define a new context
    //First parameter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Forth - the Array of data

    ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

    //Assign adapter to listview
    listView.setAdapter(adapter);

   // ListView Item Click Listener
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            /*//listview clicked item index
            int itemPosition = position;

            //Listview clicked item value
            String itemValue = (String)listView.getItemAtPosition(position);

            //show alert
            Toast.makeText(getApplicationContext(),
                    "Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/

            Intent appInfo = new Intent(About.this, about_app.class);
                startActivity(appInfo);

        }
    });
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            /*//listview clicked item index
            int itemPosition = position;

            //Listview clicked item value
            String itemValue = (String)listView.getItemAtPosition(position);

            //show alert
            Toast.makeText(getApplicationContext(),
                    "Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/
            switch(itemPosition)    

            case 0 :Intent appInfo = new Intent(About.this, about_app.class);
                startActivity(appInfo);
             break;
            case 1 :Intent appInfo = new Intent(About.this, Activity1.class);
                startActivity(appInfo);
             break;
             case 2 :Intent appInfo = new Intent(About.this, Activity2.class);
                startActivity(appInfo);
             break;
        }
    });

For this you'll have to mention like :

if (position == 1) {
Intent appInfo = new Intent(About.this, about_app.class);
startActivity(appInfo);
}
else if (position == 2) {
Intent appInfo1 = new Intent(About.this, Test.class);
startActivity(appInfo1);
}

Check this.

public interface IAction {
    public abstract void doAction();
    public abstract String getCaption();
}

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);

    //get listview from xml
    listView = (ListView)findViewById(R.id.listView);

    //defined array to show in listview
    final ArrayList<String> values = new ArrayList<String>();
    //define a new context
    //First parameter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Forth - the Array of data

    final ArrayList<IAction> actions = new ArrayList<IAction>();
    prepareActions(actions);
    for (int i = 0; i < actions.size(); i++)
        values.add(actions.get(i).getCaption());
    ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);

    //Assign adapter to listview
    listView.setAdapter(adapter);

    // ListView Item Click Listener
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /*//listview clicked item index
        int itemPosition = position;

        //Listview clicked item value
        String itemValue = (String)listView.getItemAtPosition(position);

        //show alert
        Toast.makeText(getApplicationContext(),
                "Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/

            actions.get(position).doAction();

        }
    });
}
    private void prepareActions(final ArrayList<IAction> actions) {


        actions.add(new IAction() {
            @Override
            public void doAction() {
                startActivity(new Intent(About.this, about_app.class));
            }

            @Override
            public String getCaption() {
                return "About the App";
            }
        });

        actions.add(new IAction() {
            @Override
            public void doAction() {
                startActivity(new Intent(About.this, HowtoUse.class));
            }

            @Override
            public String getCaption() {
                return "How to Use";
            }
        });

        actions.add(new IAction() {
            @Override
            public void doAction() {
                if (NeedToBuildLog)
                    startActivity(new Intent(About.this, Buildlog.class));
                /*else
                    Toast.makeText(getApplicationContext(),
                            "Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/
            }

            @Override
            public String getCaption() {
                return "Build log";
            }
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM