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

  • Home
  • SEARCH
  • 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 8269341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:11:34+00:00 2026-06-08T06:11:34+00:00

My OnClickListener seems to pick the wrong item from a ListView . The Listener

  • 0

My OnClickListener seems to pick the wrong item from a ListView. The Listener always picks the first item in the list. This is my Activity where the OnClickListener

public class ShowCharts extends ListActivity{
ProgressDialog pDialog;
static String url_read = "PHP DATA LINK";
static String TAG_ID="id", 
        TAG_CHARTS="charts",
        TAG_SUCCESS = "success",
        TAG_INTERPRET="interpret", 
        TAG_TITEL="titel", 
        TAG_ALBUM="album", 
        TAG_ALBUMCOVER="albumcover",
        TAG_LIKES="likes";

ArrayList<HashMap<String, String>> songslist;

JSONParser jParser = new JSONParser();
JSONArray charts = null;

String content;
ListView list;

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

        songslist = new ArrayList<HashMap<String, String>>();           

        new LoadCharts().execute();
        list = getListView();
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // getting values from selected ListItem
                String cid = ((TextView)findViewById(R.id._id)).getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),EditSong.class);
                // sending id to next activity
                in.putExtra(TAG_ID, cid);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });

Thats the next Activity, where i get the Data by the Acitvity before:

public class EditSong extends Activity {

EditText txtInterpret;
EditText txtTitel;
EditText txtAlbum;
EditText txtAlbumcover;
Button btnSpeichern;
Button btnLoeschen;

String cid;
// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

// single product url
private static final String url_details = "PHP DETAILS LINK";

// url to update product
private static final String url_update = "PHP UPDATE LINK";

// url to delete product
private static final String url_delete = "PHP DELTE LINK";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CHARTS = "charts";
private static final String TAG_ID = "id";
private static final String TAG_INTERPRET = "interpret";
private static final String TAG_TITEL = "titel";
private static final String TAG_ALBUMCOVER = "albumcover";
private static final String TAG_ALBUM = "album";

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

    // save button
    btnSpeichern = (Button) findViewById(R.id._speichernButton);
    btnLoeschen = (Button) findViewById(R.id._loeschen);

    // getting product details from intent
    Intent i = getIntent();

    // getting product id (id) from intent
    cid = i.getStringExtra(TAG_ID);

This is my Adapter

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView id = (TextView)vi.findViewById(R.id._id);      //id
    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView likes = (TextView)vi.findViewById(R.id.likes); // likes
    ImageView thumb_image = (ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    id.setText(song.get(ShowCharts.TAG_ID));
    title.setText(song.get(ShowCharts.TAG_TITEL));
    artist.setText(song.get(ShowCharts.TAG_INTERPRET));
    likes.setText(song.get(ShowCharts.TAG_LIKES));
    imageLoader.DisplayImage(song.get(ShowCharts.TAG_ALBUMCOVER), thumb_image);
    return vi;
}
}
  • 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-08T06:11:35+00:00Added an answer on June 8, 2026 at 6:11 am

    You need to scope findViewById() to the current row, otherwise it will always grab the first occurrence of R.id._id (which should always be the first row):

    String cid = ((TextView) v.findViewById(R.id._id)).getText().toString();
    //               add this ^
    

    Also since you are extending a ListActivity you should use the onListItemClick listener already provided:

    @Override
    protected void onListItemClick(ListView parent, View view, int position, long id) {
        // getting values from selected ListItem
        String cid = ((TextView) view.findViewById(R.id._id)).getText().toString();
    
        // Starting new intent
        Intent in = new Intent(getApplicationContext(),EditSong.class);
        // sending id to next activity
        in.putExtra(TAG_ID, cid);
    
        // starting new activity and expecting some response back
        startActivityForResult(in, 100);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lv.setOnClickListener(new OnClickListener(){ public void onClick(View v){ } }); Does v refer to the ListView
I saw this link (http://stackoverflow.com/questions/6603868/android-onclicklistener-and-table-layout) which seems like it works for the person asking
button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { db.update(); } }); On line
Code: button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // fileReader(); new xyz().execute(); }//
This is the class I want to implement the onClickListener in: private void updateUserListView(DatabaseHandler
I've setup a footer to my list view, next I'm setting up an onclicklistener
This is my first post on stackoverflow, so feel free let me know if
hey there i have this code that should save a file from sql server
this is my first time on this forum, so forgive me if my question
Inside an OnClickListener I cannot access most variables outside of the scope, like this:

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.