Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9298221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:16:08+00:00 2026-06-18T22:16:08+00:00

I have a ListView that is contained in a fragment. When an item is

  • 0

I have a ListView that is contained in a fragment. When an item is clicked on the listview I want it to execute some code but the code that is executed depends on which item on the List was clicked. Here is my code for the fragment that contains the ListView:

public class ListViewer extends Fragment {
private ListView list;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInsanceState) {
    View v = inflater.inflate(R.layout.activity_list_view, container, false);

    Player player_data[] = new Player[] {
        new Player(R.drawable.bschenn1, "Brayden Schenn", "Age: 21, Drafted 5th overall by LA King in 2009",  "Center - #10"),  
        new Player(R.drawable.giroux, "Claude Giroux", "Age: 25, Drafted 22nd overall by Philadelphia Flyers in 2006", "Center - #28"),
        new Player(R.drawable.hartnell, "Scott Hartnell", "Age: 30, Drafted 6th overall by Nashville Predators in 2000", "Left Wing - #19")
    };

    PlayerAdapter adapter = new PlayerAdapter(getActivity(), R.layout.list_row, player_data);

    list = (ListView)v.findViewById(R.id.list);

    View header = (View)inflater.inflate(R.layout.listview_header_row, null);

    list.addHeaderView(header);
    list.setAdapter(adapter);

    return v;
}
}

Here is the code to the Adapter:

public class PlayerAdapter extends ArrayAdapter<Player> {
Context context;
int layoutResourceId;
Player data[] = null;

public PlayerAdapter(Context context, int layoutResourceId, Player[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    PlayerHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new PlayerHolder();
        holder.imageIcon = (ImageView)row.findViewById(R.id.list_image);
        holder.txtplayerName = (TextView)row.findViewById(R.id.title);
        holder.txtageDraft = (TextView)row.findViewById(R.id.artist);
        holder.txtpositionSNum = (TextView)row.findViewById(R.id.duration);

        row.setTag(holder);
    } else {
        holder = (PlayerHolder)row.getTag();
    }
     Player player = data[position];
     holder.imageIcon.setImageResource(player.image);
     holder.txtplayerName.setText(player.playerName);
     holder.txtageDraft.setText(player.ageDraft);
     holder.txtpositionSNum.setText(player.positionSweaterNum);

     return row;
}

static class PlayerHolder {
    ImageView imageIcon;
    TextView txtplayerName;
    TextView txtageDraft;
    TextView txtpositionSNum;
}

}

Any help is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:16:09+00:00Added an answer on June 18, 2026 at 10:16 pm

    You should add a listener to the listview to detect touch events.

    list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
    {
     public void onItemSelected(AdapterView parentView, View childView, int position, long id) 
     {
        //here you have the position so you can do whatever you want with this item
    
    
     }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code I have set up a fragment that shows 2 ListViews.
I have a ListView composed by different fragment which contains text and a link
I have a listview that contain several EditTexts created dynamically according to the number
I have a ListView that contains 1 image+2 texts + 1 image on a
I have a listview that is loaded with a list of objects that contain
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=> <td> <asp:LinkButton ID=UpdateButton
I have ListView that uses a GridView to display several columns of data. Two
I have a ListView that shows around 300 items. When something is changed and
I have a listview that highlights a row when you mouseover it with the
I have a ListView that sits on the left side of a tablet-size screen.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.