如何在列表视图中获取textview的值?

时间:2013-09-03 18:43:38

标签: 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();
        }
    }
}

5 个答案:

答案 0 :(得分:6)

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

只需使用

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

答案 1 :(得分:1)

onListItemClick()

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

答案 2 :(得分: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&#39获取文字。

答案 3 :(得分:0)

您必须通过ListView选定的项目查找您的文本视图父组视图查看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);
    }
}

答案 4 :(得分:0)

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