How to jump from ListView to next Activity

☆樱花仙子☆ 提交于 2020-01-16 18:58:39

问题


I am using this code to jump from ListView to selected class.

public class mainmenu extends Activity {
    private ListView lv1;
    private ArrayAdapter<String> listAdapter;

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

        lv1 = (ListView) findViewById(R.id.options);
        String[] lv_arr = new String[]{"Book a Classified Ad", "Book a Classified display Ad", "Book a display Ad", "Page Position Availability", "MIS", "Market Share", "Approval", "Upload Material", "Exit"};
        ArrayList<String> Options = new ArrayList<String>();
        Options.addAll(Arrays.asList(lv_arr));
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, Options);
        lv1.setAdapter(listAdapter);
        lv1.setTextFilterEnabled(true);
        lv1.setClickable(true);

        lv1.setOnItemClickListener(new ListView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                position = a.getSelectedItemPosition();
                a.setSelection(position);
                int pos1 = position;
                if (pos1 == 0) {
                    Intent intent = getIntent();
                    intent = new Intent(mainmenu.this, classifiedAd.class);
                    startActivity(intent);
                } else if (pos1 == 1 || pos1 == 2) {
                    Intent intent1 = new Intent(mainmenu.this, displayAd.class);
                    startActivity(intent1);
                } else if (pos1 == 3) {
                    Intent intent3 = new Intent(mainmenu.this, spaceAvail.class);
                    startActivity(intent3);
                } else if (pos1 == 4) {
                    Intent intent4 = new Intent(mainmenu.this, mis.class);
                    startActivity(intent4);
                } else if (pos1 == 5) {
                    Intent intent5 = new Intent(mainmenu.this, mark.class);
                    startActivity(intent5);
                } else if (pos1 == 6) {
                    Intent intent6 = new Intent(mainmenu.this, approval.class);
                    startActivity(intent6);
                } else if (pos1 == 7) {
                    Intent intent7 = new Intent(mainmenu.this, uploadMat.class);
                    startActivity(intent7);
                } else if (pos1 == 8) {
                    finish();
                }
            }
        });
    }
}

where is the problem ?


回答1:


Here is the working reference code you can use..

public class MainMenu extends ListActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            try {
                String[] opt = getResources().getStringArray(R.array.MainMenu);
                super.onCreate(savedInstanceState);
                setContentView(R.layout.mainmenu);

                ListView lv = getListView();
                ListAdapter la = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, opt);
                lv.setAdapter(la);
                lv.setTextFilterEnabled(true);
                lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

                lv.setOnItemClickListener(new OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {

                        switch (position) {
                        case 0:
                            Intent firstIntent = new Intent(MainMenu.this,
                                    After1.class);
                            startActivity(firstIntent);
                            break;
                        case 1:
                            Intent secondIntent = new Intent(MainMenu.this,
                                    After2.class);
                            startActivity(secondIntent);
                            break;

                        default:
                            break;
                        }

                    }

                    @SuppressWarnings("unused")
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                    }
                });

            } catch (Exception e) {
            }

        } // END onCreate()
    }// END CLASS



回答2:


instead of position i have to get the Label

Object GetLabel = lv1.getItemAtPosition(position);
if (GetLabel.toString().equals("Book a Classified Ad")) {

                     Intent intent = new Intent(mainmenu.this, classifiedAd.class);
                     startActivity(intent);                  
                    }



回答3:


you need to get application context for intent

something like this,

          Intent intent = new Intent();
          intent.setClass(view.getContext(), ClassToNavigate.class);
          startActivity(intent);



回答4:


String[] lv_arr= new String [] { "Book a Classified Ad" , "Book a Classified display Ad" , "Book a display Ad" , "Page Position Availability" , "MIS" , "Market Share" , "Approval" , "Upload Material", "Exit" }; 
Class[] desClass= new Class[] { classifiedAd.class , Other2.class , Other3.class , Other4.class , Other5.class , Other6.class , Other7.class , Other8.class, Other9.class }; 
ArrayList Options = new ArrayList();
Options.addAll(Arrays.asList(lv_arr)); 
listAdapter = new ArrayAdapter(this, R.layout.simplerow, Options);
lv1.setAdapter( listAdapter );
lv1.setOnItemClickListener(new  ListView.OnItemClickListener() {
@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          Intent intent = new Intent();
          Class activity = desClass[position];
          intent.setClass(mainmenu.this, activity);
          startActivity(intent);
    }
}

Please be careful of outOfindex error。




回答5:


You can switch on an Activity by using an Intent. Intents are very useful in Android. It is also helpful for 'forwarding' some content from one Activity to another.



来源:https://stackoverflow.com/questions/4582757/how-to-jump-from-listview-to-next-activity

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