简体   繁体   中英

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. 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.

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? Also what does (View arg0) mean? I can't find proper explanation on this?

Use 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. so based on the index you can start the respective activity.

The other way without switch case

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

Then in 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.

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.

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

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.

This callback is often used in Buttons , Views in general but cannot be used with ListView .

If you want to open something when an item in the listview is clicked, you should use setOnItemClickListener !

On AdapterView.OnItemClickListener you have AdapterView<?> parent, View view, int position, long id arguments.

From android doc:

  • parent : The AdapterView where the click happened.
  • view : The view within the AdapterView that was clicked (this will be a view provided by the adapter)
  • position : The position of the item clicked (0 the first, 1 the second etc.)
  • id : The row id of the item that was clicked.

When you write your onItemClick you should remember you are in an anonymous class! So you cannot use this when you need to pass your context to new Intent

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

In your code you use mContext i don't know what is it, but i suppose it's a context.

Next time you don't have this variable, follow my advise if you want.

What you want to use is an 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);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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