【问题标题】:How to jump from ListView to next Activity如何从 ListView 跳转到下一个 Activity
【发布时间】:2011-01-03 07:38:58
【问题描述】:

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

问题出在哪里?

【问题讨论】:

  • 您能否描述一下您面临的问题,我们应该如何知道您面临的问题
  • 问题是我无法从 Item CLick on list Itmes 开始下一个活动
  • 它会抛出任何异常吗? logcat 说什么
  • logcat 未捕获列表视图的 onclick 事件。得到答案我必须使用 id 而不是 position 去下一个项目

标签: android listview android-intent


【解决方案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

【讨论】:

    【解决方案2】:

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

    Object GetLabel = lv1.getItemAtPosition(position);
    if (GetLabel.toString().equals("Book a Classified Ad")) {
    
                         Intent intent = new Intent(mainmenu.this, classifiedAd.class);
                         startActivity(intent);                  
                        }
    

    【讨论】:

      【解决方案3】:

      您需要为意图获取应用程序上下文

      类似的,

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

        请注意 outOfindex 错误。

        【讨论】:

          【解决方案5】:

          您可以使用Intent 打开Activity。 Intents 在 Android 中非常有用。它还有助于将一些内容从一个Activity“转发”到另一个。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-03-10
            • 1970-01-01
            • 2018-11-15
            • 2019-10-06
            • 1970-01-01
            • 2017-07-14
            • 1970-01-01
            • 2012-06-02
            相关资源
            最近更新 更多