从 ListAdapter 编辑 TextView?

发布于 2024-12-09 16:39:16 字数 656 浏览 0 评论 0原文

我有一个 ListView,它由 ArrayAdapter 支持。它们的定义如下:

ListView listView = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_item);
listView.setAdapter(listAdapter);

list_item.xml 包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:textSize="16sp">
</TextView>

将项目添加到列表时我想要做的是编辑与该项目关联的 TextView,例如更改其颜色。我该怎么做呢?

I have a ListView, which is backed by an ArrayAdapter. They are defined as such:

ListView listView = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_item);
listView.setAdapter(listAdapter);

list_item.xml contains the following:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:textSize="16sp">
</TextView>

What I would like to do when adding an item to the list, is edit the TextView associated with the item, like changing it's colour for example. How would I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不如归去 2024-12-16 16:39:16

您可以重写 ArrayAdapter 的方法 getView() (参考链接)例如:

ListView listView = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_item){
   public View getView (int position, View convertView, ViewGroup parent){
      View v = super.getView(position,convertView,parent);
      TextView tx = (TextView) v;
      //do something with tx
      return v;
   }
};
listView.setAdapter(listAdapter);

You could override the method getView() of your ArrayAdapter (link to the reference) for example:

ListView listView = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_item){
   public View getView (int position, View convertView, ViewGroup parent){
      View v = super.getView(position,convertView,parent);
      TextView tx = (TextView) v;
      //do something with tx
      return v;
   }
};
listView.setAdapter(listAdapter);
红颜悴 2024-12-16 16:39:16

您应该去看这个 Google I/O 演讲: http://www.google.com/events/io/2010/sessions/world-of-listview-android.html 它解释了 ListView 的基础知识以及如何去做这些事情。

You should go watch this Google I/O talk: http://www.google.com/events/io/2010/sessions/world-of-listview-android.html it explains the basics of ListView and how you would go about doing these sorts of things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文