Reputation: 6657
I am trying to retrieve a certain id when an item in list is clicked on.
How can i retrieve the value +id/name? I want to use it in an alert dialog ash shown below.
I'm currently working with a simple toast which just shows "name" at the moment when a list item is clicked on
Here is the xml code for my list item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/image" android:textSize="20dip"
android:layout_marginLeft="10dip"/>
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_centerHorizontal="true"
android:src="@drawable/stub" android:scaleType="centerCrop"/>
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:textSize="20dip" android:layout_marginLeft="10dip"/>
</RelativeLayout>
Alert dialog code:
list.setOnItemClickListener(new OnItemClickListener() {
String artistname = "get +id/name here";
String items[] = {"Youtube", "Soundcloud"};
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog.Builder builder = new
AlertDialog.Builder(JsonActivity.this);
builder.setTitle(artistname);
builder.setItems(items, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
}});
}
Upvotes: 1
Views: 1103
Reputation: 20155
Try this
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//name of the artist...
String name=((TextView)view.findViewById(R.id.name)).getText();
}
I think this is what you want...I hope this will help you
Upvotes: 2
Reputation: 13815
When we display a list using Adapter. In the getView method we fetch a value from a collection on the basis of position of item in the list. The data source of the adapter i am talking about. like this.
public View getView(){
holder.name.setText(data.get(position));
}
When a list item get clicked you will get the position that get clicked using listItemOnClickListener. In that callback call the get method of the data source again to get the element you paste over the list item. Hope you got the point. As i didn't posted much code to support it.
Upvotes: 0