3

我需要获取位于 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 ->。

布局列表视图:

 <?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();
        }
    }
}
4

5 回答 5

6

onclick 已经将父视图传递为“v”

只需使用

TextView txtTechCharacteristic = (TextView) v.findViewById(R.id.techCharacteristic);
String txt = txtTechCharacteristic.getText();
于 2013-09-04T04:16:22.070 回答
1
onListItemClick()

position获取列表(服务器响应)对象引用,然后在从索引中单击项目时获取数据。

于 2013-09-04T04:55:52.030 回答
0

试试下面

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
View view = (View)v.getParent();  
TextView txtTechCharacteristic = (TextView) view.findViewById(R.id.techCharacteristic);
TextView txtTechCharacteristicName = (TextView) view.findViewById(R.id.techCharacteristicName);
}

用于getText()从 textview 中获取文本。

于 2013-09-03T18:45:53.127 回答
0

您将必须通过 ListViews 选定项 parentGroup 视图找到您的文本视图,该视图是参数View v

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

    super.onListItemClick(l, v, position, id);
    if(v!=null)
    {
    TextView txtTechCharacteristic = (TextView) v.findViewById(R.id.techCharacteristic);
    TextView txtTechCharacteristicName = (TextView) v.findViewById(R.id.techCharacteristicName);
    }
}
于 2013-09-03T18:47:58.790 回答
0

当您向下滚动并重新使用其视图时,列表视图会回收其视图。我不会将先前视图的详细信息存储在内存中。更好的是,您可以将数据保存在数组列表或数组中,然后在onListItemClick方法中获取位置并从数组列表/数组的该位置获取数据

于 2013-09-04T05:04:45.717 回答