List Click上的新活动

时间:2012-05-11 10:27:16

标签: android android-intent android-activity

我创建了一个列表。现在,当我单击列表项时,我想运行一个Java类,这是另一个名称列表。

这是我的代码:

public class SecondAct extends ListActivity {
   private String[] items = { "Pending Retailers", "Ordered Retailers",
         "No Order Retailers", "Today's Plan", "Messages", "Daily Sales",
         "Pending Invoices", "Day Close", "Tools and Updates", "Exit" };

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, items);
      // Get the activity's ListView and set its choice mode as multiple choice
      getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
      setListAdapter(adapter);
   }

   protected void onListItemClick(ListView l, View v, int position, long id) {
      // TODO Auto-generated method stub

      super.onListItemClick(l, v, position, id);
      String selectionValue = "";
      selectionValue = (String) getListView().getItemAtPosition(position);

      Log.i("List Selection Value: ",
            (String) getListView().getItemAtPosition(position));

      if (selectionValue.equalsIgnoreCase("Pending Retailers")) {

         Intent intent = new Intent("my.com.npi.List");
         startActivity(intent);
         AlertDialog alertDialog = new AlertDialog.Builder(SecondAct.this)
               .create();
         alertDialog.setTitle("Pending Retailers Selected");

         alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

               // here we i add functions

            }
         });

         // alertDialog.setIcon(R.drawable.icon);
         alertDialog.show();
      }

      else if (selectionValue.equalsIgnoreCase("Exit")) {
         finish();
      }
   }
}

现在当我点击第一个项目时,应用程序被强制关闭。我曾意图开始新的活动,但它没有用。绝望地卡住了!请帮忙。

感谢。

2 个答案:

答案 0 :(得分:1)

如果您在自己的应用程序中有Activity,则使用:

Intent electIntent = new Intent();
electIntent.setClass(SecondAct.this, List.class);
startActivity(electIntent);

和AndroidManifest

<activity
   android:name = ".List" />

使用此意图从您的应用程序启动另一个应用程序活动:

Intent intent25 = new Intent(Intent.ACTION_MAIN).addCategory(
Intent.CATEGORY_LAUNCHER).setClassName("APP_PACKAGE_NAME",
"APP_PACKAGE_NAME.TestActivity").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("APP_PACKAGE_NAME",
"APP_PACKAGE_NAME.TestActivity"));
getApplicationContext().startActivity(intent25);

答案 1 :(得分:1)

首先注册清单中的新活动

清单声明包中的应用程序标记之前

<package name="my.com.npi">

<activity android:name = ".List" />

然后 在Intent中使用正确的类名称。不要像my.com.npi一样使用 只需导入那些包,将明确的名称命名为List.class

Intent intent = new Intent(this,List.class);
         startActivity(intent);