java - 如何在 ListView 中获取 TextView 的值?

标签 java android listview textview

我需要获取位于 ListView 特定位置的 TextView 的值。怎么做?

MyJava 代码:

protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);

    TextView txtTechCharacteristic = (TextView) findViewById(R.id.techCharacteristic);
    TextView txtTechCharacteristicName = (TextView) findViewById(R.id.techCharacteristicName);
}

这是我的 TextView ->.

布局 ListView :

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout 
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <include layout="@layout/simple_back_action_bar" />
    </RelativeLayout>
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dp"
        android:textSize="16sp"/>
</LinearLayout>

布局行:

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

<TextView android:id="@+id/techCharacteristic"         
android:textSize="20sp"         
android:textStyle="bold" 
android:layout_width="fill_parent"         
android:layout_height="fill_parent"/>
<TextView android:id="@+id/techCharacteristicName"         
android:textSize="15sp"               
android:layout_width="wrap_content"         
android:layout_height="fill_parent"/>
</LinearLayout>

填充 listView 的代码是在异步方法中完成的。这里是:

public class PopulateTechCharacteristicList extends
        AsyncTask<Integer, String, Integer> {

    ProgressDialog progress;
    Context context;

    public PopulateTechCharacteristicList(Context context) {
        this.context = context;
    }

    protected void onPreExecute() {
        progress = ProgressDialog.show(TechCharacteristicList.this,
                getResources().getString(R.string.Wait), getResources()
                        .getString(R.string.LoadingOperations));
    }

    protected Integer doInBackground(Integer... paramss) {

        ArrayList<TechCharacteristic> arrayTechChar = new ArrayList<TechCharacteristic>();
        TechCharacteristicWSQueries techCharWSQueries = new TechCharacteristicWSQueries();

        try {
            arrayTechChar = techCharWSQueries
                    .selectTechCharacteristicByAsset(asset);

            for (TechCharacteristic strAux : arrayTechChar) {
                HashMap<String, String> temp = new HashMap<String, String>();
                temp.put("cod", strAux.getTechCharacteristic() + " - "
                        + strAux.getTechCharacteristicName());
                temp.put("value",
                        "Valor: " + strAux.getTechCharacteristicValue());
                list.add(temp);
            }

        } catch (QueryException e) {

            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    protected void onPostExecute(Integer result) {

        if (result == 1) {
            SimpleAdapter adapter = new SimpleAdapter(
                    TechCharacteristicList.this, list,
                    R.layout.techcharacteristic_rows, new String[] { "cod",
                            "value" }, new int[] { R.id.techCharacteristic,
                            R.id.techCharacteristicName });

            setListAdapter(adapter);
            progress.dismiss();
        }
    }
}

最佳答案

onclick 已经将父 View 传递为“v”

就用

TextView txtTechCharacteristic = (TextView) v.findViewById(R.id.techCharacteristic);
String txt = txtTechCharacteristic.getText();

关于java - 如何在 ListView 中获取 TextView 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18599355/

相关文章:

java - 为什么类称为对象的抽象?

java - 一起生成随机字母和数字

android - 我的 RecyclerView Activity 显示空白 Activity

android - ArrayAdapter 正在为 ListView 中的每个项目重复 UI Header

android - 更改后使用 Parse.com 数据正确刷新 ListView

java - 如何在 JSP 上转义撇号或引号(由 JavaScript 使用)

java - 显示本地文件依赖

android - 键盘下一个键的 adb Key 事件

android - Dart Flutter "Non-nullable instance field ' _isSigningIn' 必须初始化。”错误

delphi - vsReport模式下的ListView项目和行的颜色