简体   繁体   English

如何打开新活动点击列表视图中的项目?

[英]How open new activity clicking an item in listview?

I can't start a new activity clicking over an item in my listview. 我无法启动新活动,点击列表视图中的项目。 I want that onItemClick can open the ApkInfoActivity .. Actually when i click nothing happen. 我希望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);

    }

I can't find the problem..How can i solve? 我找不到问题。我怎么解决?

EDIT with logcat: 用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)

Use This for doing your work 用它来做你的工作

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

 }

 }

You need to use Intent, You can also pass the clicked listview item data to your new activity. 您需要使用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);


        }
    });

}

Output: 输出:

ListView打开另一个活动OnItemClick

You can find the whole tutorial here 你可以在这里找到整个教程

为Listview添加setOnItemclickListener()。

用这个:

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

Try changing the visibility from protected to public for your method header. 尝试将方法标题从protected更改为public

Edit 编辑

Now that I look at it, your method header is actually wrong. 现在我看一下,你的方法标题实际上是错误的。 It should be the following: 它应该是以下内容:

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

The variables must be in the same order as they are in the Interface they implement. 变量必须与它们实现的接口中的变量顺序相同。

for instance if u want to open an activity based on the text u click in listview,ie if "abcd" is the option clicked on the listview and u want to open the activity with the very same name "abcd",then perform this .. 例如,如果你想打开一个基于文本的活动你点击列表视图,即如果“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){

        }


}

Giving an explanation to my answer. 给我的答案解释。 I assume that you have set your listview in order just as in your posted code. 我假设您已按照发布的代码顺序设置列表视图。 I will only review this part of your code: super.onListItemClick(l, v, position, id); 我只会检查你的代码的这一部分:super.onListItemClick(l,v,position,id); I don't this is necessary. 我不这是必要的。 In the case of the example I gave: 在我给出的例子的情况下:

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

I am just setting a lister to my listview which I have called lv, my adapter(which is the holder of my listview items) sets three variables, a View, int for position and long for argument:, I refer to the item selected on listview by its position which as usual starts at 0 (though you can instantiate it to start at any other number as you wish e,g int position=1, starts the item count at 1). 我只是设置一个lister到我的listview,我称之为lv,我的适配器(这是我的listview项目的持有者)设置了三个变量,一个是View,int是位置,long是参数:我参考选择的项目listview的位置通常从0开始(尽管你可以将它实例化为任何其他数字,如你所愿e,g int position = 1,将项目计数开始为1)。 From here you can then use any control struct to start activity as per item clicked, in my case, I used a for loop since I assumed my listview has three items only, for larger listview items, you can use a for-loop. 从这里你可以使用任何控件结构来按照单击的项目启动活动,在我的情况下,我使用了for循环,因为我假设我的listview只有三个项目,对于更大的listview项目,你可以使用for循环。 Please note how I start my new activity by first referencing to current activity as follows (SwahiliService.this) of which can safely be replace by (this keyword only) and then follows the activity I want to start. 请注意我如何通过首先引用当前活动来开始我的新活动,如下所示(SwahiliService.this)可以安全地替换为(仅此关键字),然后遵循我想要开始的活动。 I hope this is now more elaborate. 我希望现在更加精细。

// Add ArrayList and ArrayAdapter: //添加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);

// Add ArrayList of Classes : //添加类的ArrayList

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

// Click on list item to open Class from ArrayList of Classes : //单击列表项以从类的ArrayList打开Class:

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

SEE IMAGE OF CLASS NAMES HERE 在这里看到类别名称的图像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM