如何从ListView控件跳转到下一个活动(How to jump from ListView to

2019-11-04 10:28发布

我使用这个代码跳转ListView到选定的类别。

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();
                }
            }
        });
    }
}

问题出在哪儿 ?

Answer 1:

这里是工作的参考代码,你可以使用..

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


Answer 2:

代替的位置,我必须得到标签

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

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


Answer 3:

你需要获得意图应用程序上下文

这样的事情,

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


Answer 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);
    }
}

请注意outOfindex错误的。



Answer 5:

您可以在一个切换Activity利用的IntentIntent s为Android中是非常有用的。 这也是“转发”从一个有些内容有帮助Activity到另一个。



文章来源: How to jump from ListView to next Activity