【问题标题】:How to get TextView text in a cutom ListView and a custom BaseAdapter onClick(View) method如何在自定义 ListView 和自定义 BaseAdapter onClick(View) 方法中获取 TextView 文本
【发布时间】: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


【解决方案1】:

在你的 BaseAdapter 中实现 onClick 实际上是消耗你的行的点击,在这种情况下是父布局 RelativeLayout。

要获得实际的文本视图,您应该使用:

TextView mView = (TextView) v.findViewById(R.id.artist_song_tv_id);

那么你就可以得到正确的文本了。

【讨论】:

  • 我做到了。但它给了我一些我没有点击的行项目TextView
  • 您是否使用任何视图持有者模式?您的适配器可能会重新使用视图的位置并提供另一个值。
  • 无视图持有者模式。我发布的代码与我的代码完全相同。所以我可以让适配器不重用Views的位置?
  • 其实。您在回答中提到的内容给了我NullPointerExcepion
  • 知道为什么NullPointerException 吗?我真的很想接受你的回答。
【解决方案2】:

这是最简单的方法

在您的活动或片段中

listView=(ListView)findViewById(R.id.listview);
    listView.setAdapter(hotelAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Hotel hotel = (Hotel) arg0.getItemAtPosition(position);

}

【讨论】:

  • 我试过 OnItemClickListener ,但有人告诉我它不适用于 TextView 所以..
  • 叹息|:所以他为什么在这里告诉我stackoverflow.com/questions/36814378/…我不能。 (user3419782)。
猜你喜欢
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
相关资源
最近更新 更多