14

我无法通过单击列表视图中的项目开始新活动。我希望它onItemClick可以打开ApkInfoActivity..实际上当我单击时没有任何反应。

protected void onItemClick(ListView l, View v, int position, long id, AdapterView<?> parent) {
        super.onListItemClick(l, v, position, id);

       final ApplicationInfo app = applist.get(position);

       PackageInfo packageInfo = (PackageInfo) parent.getItemAtPosition(position);

       AppDataActivity appData = (AppDataActivity) getApplicationContext();
       appData.setPackageInfo(packageInfo);

       Intent appInfo = new Intent(getApplicationContext(), ApkInfoActivity.class);
       startActivity(appInfo);

    }

我找不到问题..我该如何解决?

用 logcat 编辑:

10-29 17:14:07.710: E/AndroidRuntime(3535): FATAL EXCEPTION: main
10-29 17:14:07.710: E/AndroidRuntime(3535): java.lang.ClassCastException: android.content.pm.ApplicationInfo cannot be cast to android.content.pm.PackageInfo
10-29 17:14:07.710: E/AndroidRuntime(3535):     at com.dd.application.MainActivity.onItemClick(MainActivity.java:369)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at android.widget.AdapterView.performItemClick(AdapterView.java:297)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at android.widget.AbsListView.performItemClick(AbsListView.java:1149)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2939)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at android.widget.AbsListView$2.run(AbsListView.java:3622)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at android.os.Handler.handleCallback(Handler.java:730)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at android.os.Looper.loop(Looper.java:137)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at android.app.ActivityThread.main(ActivityThread.java:5323)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at java.lang.reflect.Method.invokeNative(Native Method)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at java.lang.reflect.Method.invoke(Method.java:525)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:743)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
10-29 17:14:07.710: E/AndroidRuntime(3535):     at dalvik.system.NativeStart.main(Native Method)
4

10 回答 10

19

用它来做你的工作

 list.setOnItemClickListener(new AdapterView.onItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
      Intent appInfo = new Intent(YourActivity.this, ApkInfoActivity.class);
       startActivity(appInfo);
   } 
});
于 2013-10-29T15:30:28.967 回答
11
public class MenuYangu extends ListActivity {

String classes[] = { "Quiz Trivia", "Sign A New User", "Friend List",
"Download A File", "Upload A File", "Select Pdf files", "Memory Game",
"Dzidza Maths", "Write Exam" };

@Override
protected void onCreate(Bundle savedInstanceState) 
{
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setListAdapter(new ArrayAdapter<String>(Menuone.this,
   android.R.layout.simple_list_item_1, classes));

}

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

if (position == 0) {
Intent intent = new Intent(this, QuizActivity.class);
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(this, SignUp.class);
startActivity(intent);
}
 else if (position == 2) {
 Intent intent = new Intent(this, FriendList.class);
 startActivity(intent);
 } 
 }

 }

 }
于 2014-11-10T07:11:08.623 回答
1

您需要使用 Intent,您还可以将点击的 listview 项目数据传递给您的新活动。

String classes[] = { "Quiz Trivia", "Sign A New User", "Friend List",
"Download A File", "Upload A File", "Select Pdf files", "Memory Game",
"Dzidza Maths", "Write Exam" };

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                   
Intent intent = new Intent(getApplicationContext(),ApkInfoActivity.class);
                intent.putExtra("name",classes[i]);
                startActivity(intent);


        }
    });

}

输出:

ListView 打开另一个活动 OnItemClick

你可以在这里找到整个教程

于 2018-04-27T18:19:19.707 回答
0

尝试将您的方法标题的可见性从protected更改为。public

编辑

现在我看一下,您的方法标题实际上是错误的。它应该是以下内容:

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

变量的顺序必须与它们在它们实现的接口中的顺序相同。

于 2013-10-29T15:23:49.623 回答
0

为您的列表视图添加 setOnItemclickListener()。

于 2013-10-29T15:20:47.377 回答
0

用这个:

Intent appInfo = new Intent(CurrentActivity.this, ApkInfoActivity.class); startActivity(appInfo);

于 2013-10-29T15:21:45.047 回答
0

例如,如果您想根据您在列表视图中单击的文本打开一个活动,即如果“abcd”是在列表视图上单击的选项,并且您想打开具有相同名称“abcd”的活动,则执行此操作。 .

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

    String temp=yourarray[position];


    try{
        Class myclass=Class.forName("yourpackagename."+temp);
        Intent in=new Intent(this,myclass);
        startActivity(in);
        }catch(Exception e){

        }


}
于 2015-05-09T17:56:54.213 回答
0

解释我的答案。我假设您已经按照发布的代码中的顺序设置了列表视图。我只会查看您的这部分代码: super.onListItemClick(l, v, position, id); 我不认为这是必要的。在我给出的示例中:

lv.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
      if(position==0){
     Intent appInfo = new Intent(SwahiliService.this, DisplayActivity.class);
      startActivity(appInfo);
  } 
      if(position==1){
          Intent english=new Intent(SwahiliService.this,EnglishService.class);
          startActivity(english);
      }
      if(position==2){
          Toast.makeText(getApplicationContext(),"You have selected pst3", Toast.LENGTH_LONG).show();
      }

我只是为我的列表视图设置了一个列表器,我称之为 lv,我的适配器(它是我的列表视图项目的持有者)设置了三个变量,一个视图,用于位置的 int 和用于参数的 long:,我指的是在列表视图的位置通常从 0 开始(尽管您可以根据需要将其实例化为从任何其他数字开始,例如 int position=1,从 1 开始项目计数)。然后,您可以从这里使用任何控制结构根据单击的项目启动活动,在我的情况下,我使用了 for 循环,因为我假设我的列表视图只有三个项目,对于较大的列表视图项目,您可以使用 for 循环。请注意我如何通过首先引用当前活动(SwahiliService.this)来开始我的新活动,其中可以安全地替换为(仅限 this 关键字),然后跟随我想要开始的活动。我希望现在更详细。

于 2016-05-30T20:04:40.093 回答
0

// 添加 ArrayList 和 ArrayAdapter:

    final ArrayList<String> listItems = new ArrayList<String>();
        listItems.add("image_one");
        listItems.add("image_two");
        listItems.add("image_three");

    ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<>(this, 
        android.R.layout.simple_list_item_1, listItems);
        myListView.setAdapter(myArrayAdapter);

// 添加类的 ArrayList

    final ArrayList<Class> intents = new ArrayList<Class>();
        intents.add(image_one.class);
        intents.add(image_two.class);
        intents.add(image_three.class);

// 单击列表项以从类的ArrayList 中打开类

    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            Intent listIntent = new Intent(getApplicationContext(), 
            intents.get(position)); 
            startActivity(listIntent);
        }
    });

在此处查看班级名称的图像

于 2018-05-13T09:39:35.960 回答
-1
    lv.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
          if(position==0){
         Intent appInfo = new Intent(SwahiliService.this, DisplayActivity.class);
          startActivity(appInfo);
      } 
          if(position==1){
              Intent english=new Intent(SwahiliService.this,EnglishService.class);
              startActivity(english);
          }
          if(position==2){
              Toast.makeText(getApplicationContext(),"You have selected pst3", Toast.LENGTH_LONG).show();
          }
   }});
于 2016-05-26T17:47:03.400 回答