I have a custom ListView
with 3 TextView
s and a custom BaseAdapter
which implements View.OnClickListener
.
I tried some ways of getting the text from the specific TextView
that has been clicked in the onClick(View)
method but came out short.
If the View
i get as the parameter of the onClick(View)
method is the specific TextView
that has been clicked , Why I can't get It's text properly?
MyBaseAdapter.java:
public class MyListViewAdapter extends BaseAdapter implements View.OnClickListener
{
private Context context;
private ArrayList<MyListViewRow> rowsList = new ArrayList<MyListViewRow>(); // All one row items
private TextView songYouTubeLink;
private TextView b;
private TextView c;
public MyListViewAdapter(Context context,ArrayList<MyListViewRow> rowsList)
{
this.context = context;
this.rowsList = rowsList;
}
@Override
public int getCount()
{
return this.rowsList.size();
}
@Override
public Object getItem(int position)
{
return this.rowsList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
/**
* Returns a new row to display in the list view.
*
* Position - position of the current row.
*
*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/**
* Inflating the root view and all his children and set them to a View Object.
*
*/
View row = layoutInflater.inflate(R.layout.list_view_row,null);
// Get all the views in my row
this.songYouTubeLink= (TextView) row.findViewById(R.id.songYouTubeLink_id);
this.b = (TextView) row.findViewById(R.id.b_id);
this.c = (TextView) row.findViewById(R.id.c_id);
MyListViewRow myListViewRow = rowsList.get(position);
// Set values to all the views in my row
this.songYouTubeLink.setText(myListViewRow.getSongYouTubeLinkText());
this.b.setText(myListViewRow.getB());
this.c.setText(myListViewRow.getC());
// Setting on click listeners
this.songYouTubeLink.setOnClickListener(this);
//this.b.setOnClickListener(this);
return row;
}
@Override
public void onClick(View v)
{
Toast.makeText(context, "onClick LV Adapter called", Toast.LENGTH_SHORT).show();
TextView tv = (TextView) v;
Toast.makeText(context, tv.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(context, this.a.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
The first Toast
in onClick(View)
gives me "YouTube Link" String
, and the second gives me a text from the 1 of the rows of the ListView
(Note that the second Toast
give me every time the same text until I browse my LIsView
. After the browsing the first Toast
stays the same and the second gives me a text of some other row TextView
).
EDIT:
list_view_row_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Artist name and song name -->
<TextView
android:id="@+id/artist_song_tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_marginBottom="3dp"
/>
<!-- Song youtube link -->
<TextView
android:id="@+id/youtube_link_tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_below="@id/artist_song_tv_id"
android:layout_marginBottom="3dp"
/>
<!-- Song shazam link -->
<TextView
android:id="@+id/shazam_link_tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_below="@id/artist_song_tv_id"
android:layout_toRightOf="@id/youtube_link_tv_id"
android:layout_marginBottom="5dp"
/>
</RelativeLayout>
Implementing onClick in your BaseAdapter is actually consuming the click of your row which is parent layout RelativeLayout in this case.
To get the actual textview, you should use:
TextView mView = (TextView) v.findViewById(R.id. artist_song_tv_id);
Then you can get the correct text.