android - 在 Listview 中更改 TextView 文本未得到反射(reflect)

标签 android listview

shelf_repots_item_row.xml

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


 <TextView`enter code here`
       android:id="@+id/textView"
       android:layout_width="150dp"
       android:layout_height="80dp"
       android:layout_marginBottom="5dp"
       android:background="@drawable/back"
       android:textColor="@color/color_black"
       android:gravity="center_vertical"
       android:padding="10dip" />

</LinearLayout>

有 10 个 TextView 元素添加了文本设置 “设置 TextView 1”、“设置 TextView 2”...“设置 TextView 10”正确 您可以在下面看到适配器 getView()

private class MyAdapter extends BaseAdapter {
   ...

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.shelf_repots_item_row, null);
            TextView title = (TextView) retval.findViewById(R.id.textView);
            title.setBackground(null);
            title.setText("Seting text view :"+position);
            return retval;
        }
    }
}

在第 4 个元素的项目点击我正在阅读她的阅读 str2 = (String) textView.getText();并获取在 getView 中设置的正确字符串。 str2 现在是“设置 TextView 4”

现在我正在更改 textView 字符串 textView.setText("改变 TextView ");

再次阅读 String like str = (String) textView.getText(); str 现在是“更改 TextView ”

但是在屏幕上没有得到反射(reflect)。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        LinearLayout v = (LinearLayout)parent.getAdapter().getView(position, view, parent);

        TextView textView = (TextView)v.getChildAt(0);
        String str2 = (String) textView.getText();

        textView.setText("Changing Text View");
        textView.setTextColor(R.drawable.back);
        String str = (String) textView.getText();
        ((MyAdapter) parent.getAdapter()).notifyDataSetChanged();
}

任何人都可以帮助我并指出我哪里做错了吗? 提前致谢。

最佳答案

它不会像这样工作,原因如下。

当调用 notifyDataSetChanged() 时调用列表的 getView 方法。

这里你正在做的是改变文本

    LinearLayout v = (LinearLayout)parent.getAdapter().getView(position, view, parent);
    TextView textView = (TextView)v.getChildAt(0);
    String str2 = (String) textView.getText();
    textView.setText("Changing Text View");

但是当你调用 ((MyAdapter) parent.getAdapter()).notifyDataSetChanged();

由于 title.setText( "设置 TextView :"+position);

希望这能帮你把事情搞清楚。

如果对您有帮助,请标记为正确。:)

关于android - 在 Listview 中更改 TextView 文本未得到反射(reflect),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27223992/

相关文章:

Android Gradle lintVital OutofMemoryError 导致构建崩溃

javascript - 如何禁用 ListView 中特定行的编辑按钮

android - Geoquery 填充 ListView/RecyclerView

java - Android - 使用 getSelectedItem 时出现 NullPointer 异常

android - 启用即时运行从 2.2.3 开始给出运行时错误

android - 在断开连接的设备上通过 XMPP 丢失消息

c# - WPF 绑定(bind) ListView ItemContainerStyle 的背景

android - 如何在android中绘制水平 ListView 并使用水平 ScrollView 访问它

android - 后退按钮在 CastCompanionLibrary 的 VideoCastControllerAcitivity 中不起作用

java - 如何捕获ConnectionException、UnknownHostException?