简体   繁体   English

ListView项目打开新活动

[英]ListView Item Open New Activity

I'm still following a couple of tutorials in order to learn Android Development, I am struggling to open new activities. 为了学习Android开发,我仍在关注一些教程,我正在努力开展新的活动。 I can open only one activity, in fact all items on my ListView open this one activity, which would be great for ListView item in position 0 (the first item) not all of them. 我只能打开一个活动,实际上ListView上的所有项目都打开了一个活动,这对于位置0(第一个项目)中的ListView项(而不是全部)非常ListView

My code below: 我的代码如下:

        // Listen for ListView Item Click
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(mContext, MyActivity1.class);
            mContext.startActivity(intent);
        }        
    });

    return view;
}

As you can see MyActivity1.class opens fine, but what if I had a second class called MyActivity2 which should be opened up by listview item in position 1 (second item), how would I do that? 如您所见,MyActivity1.class可以很好地打开,但是如果我有一个名为MyActivity2的第二个类,应该由位置1(第二个项)中的listview项打开,该怎么办? Also what does (View arg0) mean? 还有(View arg0)是什么意思? I can't find proper explanation on this? 我找不到适当的解释吗?

Use listview OnItemClickListener 使用listview OnItemClickListener

listView.setOnItemClickListener( new OnItemClickListener()
{
       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
       { 
         switch(positon)
         {
             case 0:
                      Intent intent = new Intent(mContext, MyActivity1.class);
                      mContext.startActivity(intent);
             break;
             case 1:
                      Intent intent = new Intent(mContext, MyActivity2.class);
                      mContext.startActivity(intent);
             break;
         }  
       }
});

position is the index of list item. position是列表项的索引。 so based on the index you can start the respective activity. 因此,您可以根据索引开始相应的活动。

The other way without switch case 没有开关盒的另一种方式

String[] classes ={"MyActivity1","MyActivity2"};

Then in onItemClick 然后在onItemClick

String value = classes[position];
Class ourClass =Class.forName("packagename."+value);
Intent intent = new Intent(mContext,ourClass);
mContext.startActivity(intent);

You can use Itemclick listner. 您可以使用Itemclick列表器。

listviewOBJ.setOnItemClickListener( new OnItemClickListener()
{
       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
       { 

       }
});

here you can use int position to decide which item is clicked. 在这里,您可以使用int position来确定单击哪个项目。

If my understanding is correct, then here is your answer: 如果我的理解是正确的,那么这里是您的答案:

Implement OnItemClickListener to your main activity: then set listener to your listview : listView.setOnItemClickListener(this); 对您的主要活动实施OnItemClickListener :然后将侦听器设置为listviewlistView.setOnItemClickListener(this);

finallly : 最后:

    private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){

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


   switch(positon)
         {
             case 0:
                      Intent intent = new Intent(mContext, MyActivity1.class);
                      mContext.startActivity(intent);
             break;
             case 1:
                      Intent intent = new Intent(mContext, MyActivity2.class);
                      mContext.startActivity(intent);
             break;
         }  
        }       
    };
public void onClick(View arg0) {

onClick is called when you click on something, and arg0 (you could call it view , arg0 is so useless as name) is what you clicked. 当您单击某物时,将调用onClick ,并且您单击的是arg0 (您可以将其称为viewarg0是如此无用)。

This callback is often used in Buttons , Views in general but cannot be used with ListView . 通常,在ButtonsViews中通常使用此回调,但不能与ListView一起使用。

If you want to open something when an item in the listview is clicked, you should use setOnItemClickListener ! 如果要在单击列表视图中的项目时打开某些内容,则应使用setOnItemClickListener

On AdapterView.OnItemClickListener you have AdapterView<?> parent, View view, int position, long id arguments. AdapterView.OnItemClickListener您具有AdapterView<?> parent, View view, int position, long id参数。

From android doc: 从android doc:

  • parent : The AdapterView where the click happened. parent :单击发生的AdapterView。
  • view : The view within the AdapterView that was clicked (this will be a view provided by the adapter) view :在AdapterView中被单击的视图(这将是适配器提供的视图)
  • position : The position of the item clicked (0 the first, 1 the second etc.) position :单击的项目的位置(第一个为0,第二个为1,依此类推)
  • id : The row id of the item that was clicked. id :单击的项目的行ID。

When you write your onItemClick you should remember you are in an anonymous class! 当您编写onItemClick您应该记住自己在一个匿名类中! So you cannot use this when you need to pass your context to new Intent 因此,当您需要将上下文传递给new Intent时,就不能使用this

pastesList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Intent intent = null;
        switch (position)
        {
            case 0:
                intent = new Intent(ActivityClass.this, Class1.class);
                break;
            case 1:
                intent = new Intent(ActivityClass.this, Class2.class);
                break;
            default:
                return;
        }

        startActivity(intent);
    }
});

This line 这条线

ActivityClass.this

is used when you need to use the instance of the class ActivityClass (You should replace ActivityClass with your class name) but you cannot access it directly using this which here refer to new AdapterView.OnItemClickListener() 当你需要使用的类的实例用于ActivityClass (此时应更换ActivityClass与类名),但它直接使用您不能访问this其在这里是指new AdapterView.OnItemClickListener()

In your code you use mContext i don't know what is it, but i suppose it's a context. 在您的代码中,您使用mContext我不知道它是什么,但是我想它是一个上下文。

Next time you don't have this variable, follow my advise if you want. 下次您没有此变量时,如果需要,请遵循我的建议。

What you want to use is an AdapterView.OnItemClickListener . 您要使用的是AdapterView.OnItemClickListener

    final ListView list = ...;
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(mContext, MyActivity1.class);
            if (position == 1) {
                intent = new Intent(mContext, MyActivity2.class);
            }
            startActivity(intent);
        }
    });
lv.setOnItemClickListener(n## Heading ##ew OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        // When clicked, show a toast with the TextView text
        if(position == 1) {
            //code specific to first list item    
            Intent myIntent = new Intent(view.getContext(), Html_file1.class);
            startActivityForResult(myIntent, 0);
        }

        if(position == 2) {
            //code specific to 2nd list item    
            Intent myIntent = new Intent(view.getContext(), Html_file2.class);
            startActivityForResult(myIntent, 0);
        }
    }
});

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

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