Android listview - 获取自定义列表视图的文本

2024-02-07

我正在开发一个应用程序,在其中创建了一个自定义列表视图:

列表视图xml代码如下:

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="15dip"
    android:divider="#623b42"
    android:dividerHeight="0.5dip"
    android:cacheColorHint="#00000000"
    android:overScrollMode="never" >
</ListView>

适配器 xml 是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/textview_rupee_mlsaa"
        android:gravity="center_vertical"
        android:padding="15dip"
        android:textColor="@color/color_black"
        android:textSize="15sp"
        tools:ignore="SelectableText" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        android:padding="15dip"
        android:textColor="@color/color_black"
        android:textSize="15sp"
        tools:ignore="SelectableText" />

</RelativeLayout>

适配器的.java 文件是:

public class ListAdapter extends ArrayAdapter<String> {
/** Global declaration of variables. As there scope lies in whole class. */
private Context context;
private String[] dataset1;  
private int[] dataset2;

    /** Constructor Class */
    public ListAdapter(Context context,String[] ListArray1, int[] ListArray2) {
        super(context,R.layout.list_adapter_layout,ListArray);
        this.context = context;
            this.dataset1= ListArray1;
            this.dataset2= ListArray2;
    }

    /** Implement getView method for customizing row of list view. */
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        // Creating a view of row.
        View rowView = inflater.inflate(R.layout.list_adapter_layout, parent, false);   

        TextView tv1 = (TextView)rowView.findViewById(R.id.textview1);      
        TextView tv2 = (TextView)rowView.findViewById(R.id.textview2);

        tv1.setText(data1[position]);       
        tv2.setText(""+data2[position]);

        return rowView;
    }
}

然后我在列表视图上将项目单击侦听器设置为:

listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(this);    

ListAdapter adapter = new ListAdapter(this,list1,list2);
listView.setAdapter(adapter);

然后我在项目单击上使用 onItemClickListener :

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
name = (String) arg0.getItemAtPosition(position);       
}

在这里,我获取了自定义列表视图的第一个文本视图的值。但我还想要第二个文本视图的值。如果我添加更多,那么如何获取它的值。

请建议我这样做的方法。


在你的 OnItemClick 中尝试这样

TextView text = (TextView) arg1.findViewById(R.id.urtextID);
                           ^^^^^^
String tEXT = text.getText().toString();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android listview - 获取自定义列表视图的文本 的相关文章

随机推荐

  • Docker 返回退出代码 3221225781 安装 vc_redist.x64.exe

    我在 docker RUN 中看到了很多有关退出代码 3221225781 的问题 但我仍然找不到答案 考虑这个 dockerfile FROM mcr microsoft com dotnet core runtime 3 1 WORKD
  • 将自然语言描述解析为结构化数据的策略

    我有一系列要求 并且正在寻找最好的基于Java使用的策略 算法 软件 基本上 我想获取一组由真人用自然英语输入的食谱成分 并将元数据解析为结构化格式 请参阅下面的要求以了解我想要做什么 我环顾过这里和其他地方 但没有找到任何关于下一步方向的
  • 在Java中为单链表创建新节点

    我仍在学习 Java 目前正在解决 破解编码面试 中的问题 第 2 章 LinkedList 中的问题之一要求从未排序的链表中删除重复项 我在 GitHub 上找到了一堆答案 解决方案 但我想创建自己的 Node 并编写自己的版本 到目前为
  • C# 退出使用 lambda 的泛型 ForEach

    有谁知道是否可以退出使用 lambda 的通用 ForEach 例如 someList ForEach sl gt if sl ToString foo break continue processing sl here some proc
  • 如何从 CFMutableDictionary 检索特定键的值

    In C EventType find 1 gt second可用于查找键 1 的值 我是 Objective C 的新手 我已经实现了CFDictionary为其增加价值 但是我该怎么办 retreive并查看values特定键的 EDI
  • SQL Server通讯协议问题

    我正在使用 VSTS 2008 C ADO Net SQL Server 2008 我对 SQL Server 2008 将使用哪种通信协议的问题 我的问题的更多详细信息 如果连接字符串看起来像这样 是否使用命名管道或 TCP IP 使用不
  • Vue index.html favicon 问题

    我正在使用 Vue 2 0 和 Vue CLI 3 0 我试图让 favicon ico png 文件正常工作 但一点运气都没有 我已将问题缩小到以下范围 由yarn build生成的index html文件转换了这行html代码 to 如
  • 创建我的第一个 JDBC Realm(Glassfish V3)

    我想为我的 Web 应用程序创建一个 JDBC 领域 由于我的用户彼此之间非常不同 我只需要一个表 其中包含领域的用户名和密码 我决定在我的数据库中创建一个名为 ROLE 的新表 与其他用户表的一对一关系 This is how my us
  • 如何知道应用程序正在关闭

    有一个 WPF 应用程序 我想在应用程序关闭时记录 但我无法修改应用程序 一些限制 只是因为业务 因此 我在现有应用程序中创建了一个不可见的表单组件 添加为 dll 库 因此不需要修改现有应用程序 但问题是 我的隐形组件如何知道应用程序正在
  • 上传前预览多张图片

    我想预览多个图像 但问题是如果我上传 2 个图像 我的代码就可以工作 我想预览与用户上传的一样多的内容 这是我的 JavaScript var abc 0 Declaring and defining global increement v
  • C#:获取域中计算机的信息

    为了获取网络中某台计算机的信息 我应该在 C 中使用哪些类 例如谁登录了该计算机 该计算机上正在运行什么操作系统 打开了哪些端口等 查看系统管理 http msdn microsoft com en us library system ma
  • TableTools 导出在多个 JQuery 选项卡上的 DataTables 中不起作用

    我使用 DataTables 1 8 2 TableTools 2 0 1 和 JQuery 1 6 4 在三个 jQuery 选项卡上分别显示一个表格 TableTools 为表格提供了复制 Excel PDF 打印导出功能 并且仅适用于
  • 描述符的 Python 文档字符串

    我使用描述符来定义接口类的寄存器 class Register object def init self address docstring instance docstring self address address self doc
  • 当你没有在 ruby​​ 中指定异常类时,你会捕获哪些异常?

    当您不指定这样的异常类时 您会捕获哪些异常 begin do something rescue puts Exception end 根据我的副本Ruby 编程 1 9 不带参数的救援子句被视为具有参数StandardError 这是来自的
  • 如何在 Windows 中安装适用于 Python 3.4 的 pandas?

    根据the docs http pandas pydata org pandas docs dev install html 这应该有效 从 PyPI 安装 pandas 可以通过 PyPI 中的 pip 安装 点安装熊猫 这可能需要安装许
  • TypeScript:在事件中使用 jquery $(this)

    HTML div div
  • 对每个请求执行代码

    我希望能够对每个将数据填充到 ViewData 的请求执行一些代码 目前 我有一个基本控制器类 我的所有控制器都继承自该类 并且我重写 OnActionExecuting 并在那里执行它 我对这种方法唯一关心的是 创建新控制器的人必须继承基
  • 在事件处理程序中发送不带 XHR 的 http 请求

    如何使用 javascript 作为事件处理程序通过 post get 方法发送 http 请求 谢谢 保罗 好吧 你不想使用 Ajax 您可以使用事件处理程序来提交表单 a href send a
  • Salesforce PhoneGap iPhone 应用程序中的 oAuth2 问题(远程访问应用程序授权问题)

    我使用 PhoneGap 创建了一个 iPhone 应用程序 我遵循了本教程中的代码 构建由 Database com 提供支持的 PhoneGap 移动应用程序 http wiki developerforce com page Buil
  • Android listview - 获取自定义列表视图的文本

    我正在开发一个应用程序 在其中创建了一个自定义列表视图 列表视图xml代码如下