Michael Ortiz
Michael Ortiz

Reputation: 777

ListView setOnItemClickListener action for specific item

In my app, I have a ListView with different items. The issue that I am having is that I want to perform listeners or actions to each specific item. Right now, the code I have when you press an item, it applies it to all items instead of an item specifically.

What I am trying to do is stream videos when a specific item is pressed.

Here is my code:

public class MainActivity extends Activity {


private ListView listView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ViedoStream viedoStream_data[] = new ViedoStream[]
    {
        new ViedoStream(R.drawable.image0, "Mission TV"),
        new ViedoStream(R.drawable.image1, "HCBN Philippines"),
        new ViedoStream(R.drawable.image2, "Global Family Network"),
        new ViedoStream(R.drawable.image3, "2CBN"),
        new ViedoStream(R.drawable.image4, "Red ADvenir"),
        new ViedoStream(R.drawable.image5, "Alfa Television"),
        new ViedoStream(R.drawable.image6, "Light Channel Hungary"),
        new ViedoStream(R.drawable.image7, "Light Channel Rumania"),
        new ViedoStream(R.drawable.image8, "Light Channel Germany"),
        new ViedoStream(R.drawable.image9, "Terceiro Anjo"),
        new ViedoStream(R.drawable.image10, "HCBN Indonesia"),
        new ViedoStream(R.drawable.image11, "TV Famille")
    };

    ViedoStreamAdapter adapter = new ViedoStreamAdapter(this, 
            R.layout.listview_item_row, viedoStream_data);


    listView1 = (ListView)findViewById(R.id.listView1);

    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);

    listView1.setAdapter(adapter);

    listView1.setOnItemClickListener(new OnItemClickListener() {

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

    //  Place code here with the action: RIGHT NOW I HAVE A TOAST FOR EXAMPLE ONLY

            Toast.makeText(getApplicationContext(), "TV Selected", Toast.LENGTH_SHORT).show();


        }
   });

}}

As you can see, when an item is selected, the toast applies to every single item on the ListView. Please note the the Toast is just an example I am using. I want to stream http videos instead. Now, how can I do this so that every time I select an item, it only does an action for that specific item?

I do programing for iPhone and I am new to all this... In iPhone, this is how I do it. *This is just an example of how the logic looks like:

if (item == @"TV Item" { 
  //Do Action
}

Thanks

Upvotes: 0

Views: 4571

Answers (1)

Siddharth Lele
Siddharth Lele

Reputation: 27748

The OnItemClickListener() applies to all the ListView's Items by design, If you wish to pass the control to specific Widgets, then you set to them their individual OnClickListener() methods. This needs to be done in the Adapter. In your case, the ViedoStreamAdapter.

Consider this as an example. In an app of mine, I use the OnItemClickListener() for showing the Tweet details in a separate Activity. That works fine. But when a user clicks the ImageView in the same item, I need to show that Twitter Profile. So, in the Activity, I use the OnItemClickListener() and in the adapter, I override the ImageView's OnClickListener(). Like this:

UPDATED CODE:

holder.imgvwProfilePicture.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent profileIntent = new Intent(activity, ProfileContainer.class);
        profileIntent.putExtra("userId", arrTweets.get(position).getAuthorID());
        profileIntent.putExtra("userScreenName", arrTweets.get(position).getScreenName());
        activity.startActivity(profileIntent);

    }
});

In this case, I have an ArrayAdapter<TimeLineData> arrTweets. And TimeLineData is a POJO class that is a setter and getter for individual values.

If you do not need the OnItemClickListener(), feel free to remove it. It does not affect the OnClickListener() that you setup in the Adapter.

When the ImageView is clicked, I pull out the data for the current position using this: arrTweets.get(position).getAuthorID(). And the position is the one in the getView() of the Adapter.

Do ask if you have more questions on this.

NOTE: This should be done before your return the View instance in your getView(). For instance, the code above is called before this in my app:

return vi;

UPDATE 2:

As pointed out by the OP in the comments, this is what worked for him:

if(position == 0){
    ....
}

Upvotes: 1

Related Questions