如何在cutom ListView和自定义BaseAdapter onClick(View)方法中获取TextView文本

时间:2016-04-24 10:41:33

标签: android listview textview baseadapter

我有一个自定义ListView,其中包含3个TextView和一个自定义BaseAdapter,用于View.OnClickListener

我尝试了一些方法来获取TextView方法中点击的特定onClick(View)的文字,但结果很短。

如果View我得到onClick(View)方法的参数是已点击的特定TextView,为什么我无法正确获取文字?

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();

    }
} 

Toast中的第一个onClick(View)为我提供了“YouTube链接”String,第二个给了我ListView行中的1个文本(注意在浏览我的Toast之前,第二个LIsView每次都会给我相同的文字。浏览后,第一个Toast保持不变,第二个给我一个其他行的文字{ {1}})。

修改

list_view_row_item.xml

TextView

2 个答案:

答案 0 :(得分:1)

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

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

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

然后你就可以得到正确的文字了。

答案 1 :(得分:0)

这是最简单的方法

在您的活动或片段中

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);

}