【发布时间】:2016-04-24 10:41:33
【问题描述】:
我有一个自定义 ListView 和 3 个 TextViews 和一个自定义 BaseAdapter 实现 View.OnClickListener。
我尝试了一些方法来从特定的 TextView 中获取文本,该文本已在 onClick(View) 方法中单击,但结果很短。
如果我作为onClick(View)方法的参数得到的View是被点击的具体TextView,为什么我不能正确获取It's text?
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();
}
}
onClick(View) 中的第一个 Toast 给了我“YouTube 链接”String,第二个给了我来自 ListView 的第一行的文本(请注意,第二个 Toast 给我每次相同的文本,直到我浏览我的LIsView。浏览后第一个Toast 保持不变,第二个给我一个其他行的文本TextView)。
编辑:
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>
【问题讨论】:
-
为什么不为实际视图设置一个 onclick 监听器,而不是让 Base Adapter 实现 onclick?
-
@AndroidEnthusiast 因为这样的代码更简洁。
-
好的...请上传您的“R.layout.list_view_row”xml 的样子
-
@AndroidEnthusiast 已编辑。
标签: android listview textview baseadapter