Get value of item on OnItemClick Listview
Asked Answered
B

4

13

I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:

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

                    View curr = parent.getChildAt((int) id);
                    TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
                    String playerChanged = c.getText().toString();

                    Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();

                }

At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?

Bonita answered 15/11, 2012 at 20:29 Comment(0)
L
50

The parameter v is the current row. so use:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
    String playerChanged = c.getText().toString();

    Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}

(Or you could use getChildAt(position) but this would be slower.)

Understand you might be able to simplify this more depending on your layout.

Leninist answered 15/11, 2012 at 20:32 Comment(2)
Thanks, this works! Do you have any idea why the getChildAt position didn't work? I also tried this, but this gave me errors as well..Bonita
Depending on your adapter id might refer to a completely different index. For example CursorAdapters return the table's primary key in id. position on the other hand should always refer to the row count.Leninist
I
0

Just Small Change is Required

TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);

Since you are using custom view you need to pass the View argument in your OnItemClickListener then you need to use that value to get the Details of TextViews present in that

Incendiarism answered 3/1, 2015 at 9:8 Comment(1)
Next time make a comment, do not post an additional answer.Ministry
B
0
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i=new Intent(ListOfAstrologers.this,AstroProForUser.class);
                i.putExtra("hello",adapter.getItem(position).getUsername() );
                startActivity(i);
            }
        });

Here username is a string field declared in a class and you must over ride getItem() method in adapter class to get the details of inflated row when you click.

Birdsall answered 24/5, 2017 at 11:25 Comment(0)
N
0

I dont have much experience but I think if you want to pass a variable from an OnClick (which is an anonimous class) to the outside of the onClick and to the other activity, you want to pass it via Intent intent.putExtra... (it's quite easy) Otherwise you might end up using "public static" variable, which might be a memory leak...

Northing answered 17/9, 2022 at 4:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.