如何从ListView跳转到下一个Activity

时间:2011-01-03 07:38:58

标签: android listview android-intent

我正在使用此代码从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();
                }
            }
        });
    }
}

问题出在哪里?

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

而不是位置我必须得到标签

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

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

答案 2 :(得分:0)

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

这样的事,

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

答案 3 :(得分:0)

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错误。

答案 4 :(得分:-2)

您可以使用Activity打开IntentIntent在Android中非常有用。将一些内容从一个Activity“转发”到另一个内容也很有帮助。

相关问题