【问题标题】:How to get the value of a textview in a listview?如何在列表视图中获取文本视图的值?
【发布时间】:2013-09-03 18:43:37
【问题描述】:

我需要获取位于我的 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();
        }
    }
}

【问题讨论】:

    标签: java android listview textview


    【解决方案1】:

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

    随便用

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

    【讨论】:

      【解决方案2】:
      onListItemClick()
      

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

      【讨论】:

        【解决方案3】:

        试试下面的

        @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 中获取文本。

        【讨论】:

        • 它没有用。他继续取第一个列表项的值。
        • @user1792343 发布您的列表视图布局 xml,它不可能给您一个正确的答案 继续获取第一个列表项的值是什么意思?
        • @user1792343 在我看来它应该可以工作。我想不出任何其他原因。祝你好运,如果其他人能为你找到正确的答案
        • @user1792343 由于您的列表创建将适配器代码放在这里,它不起作用
        【解决方案4】:

        您必须通过 ListViews selected items 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);
            }
        }
        

        【讨论】:

        • 我的项目列表是选择数据库的结果。根据结果​​,我有几个项目,使我的 textView (techCharacteristic techCharacteristicName ae) 成倍增加。当我实例化我的控件文本视图时,我无法获得点击的确切值。
        • 是的,您必须更改您的列表创建,将您的适配器代码放在这里
        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2023-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-11
          • 1970-01-01
          • 1970-01-01
          • 2011-06-25
          • 1970-01-01
          相关资源
          最近更新 更多