简体   繁体   中英

How to get the value of a textview in a listview?

I need to get the value of a TextView which is in a certain position of my ListView. How to do it?

MyJava Code:

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

Here Are my TextView ->.

Layout 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>

Layout Rows:

<?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>

The code to fill the listView is done within the asynchronous method.Here it is:

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 already passes the parent view as "v"

Just use

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

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

Try the below

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

Use getText() to get the text from textview's.

You will have to find for your text view through the ListViews selected items parentGroup view that is the argument 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);
    }
}

A listview recycles its view when you scroll down and re uses its views. I will not store the details of previous views in memory. Better you can keep you data in an arraylist or array and then in onListItemClick method get the position and get the data from that position of arraylist/array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM