gpt4 book ai didi

android - 如何从 android 中的自定义列表适配器获取控件 ID?

转载 作者:行者123 更新时间:2023-11-29 22:31:59 26 4
gpt4 key购买 nike

我有一个自定义列表适配器,其中包含一个带有控件的布局。我想知道如何获得每个单独的控件 ID?我打算更改一些 TextView 的颜色。

private void UpdateDisplay() {
// define the list which holds the information of the list
List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();

// define the map which will hold the information for each row
Map<String, Object> data;

if (appts.size() > 0) {
for (int i = 0; i < appts.size(); i++) {
data = new HashMap<String, Object>();

data.put("apptListType", appts.get(i).getType().getName());
data.put("apptListTime", DateFormat.getTimeInstance(
DateFormat.SHORT).format(appts.get(i).getStartDate())
+ " - "
+ DateFormat.getTimeInstance(DateFormat.SHORT).format(
appts.get(i).getEndDate()));
data.put("apptListContactName", appts.get(i).getContact()
.getFullName());
data.put("apptListContactCompany", appts.get(i).getContact()
.getCompany());
resourceNames.add(data);
}
} else {

data = new HashMap<String, Object>();

data.put("apptListType", "No appointments found for today.");
data.put("apptListTime", "");
data.put("apptListContactName", "");
data.put("apptListContactCompany", "");
resourceNames.add(data);

}

apptDescription = new SimpleAdapter(this, resourceNames,
R.layout.apptlistitem, new String[] { "apptListType",
"apptListTime", "apptListContactName",
"apptListContactCompany" }, new int[] {
R.id.apptListType, R.id.apptListTime,
R.id.apptListContactName, R.id.apptListContactCompany });
ListView apptList = (ListView) findViewById(R.id.apptsList);
apptList.setAdapter(apptDescription);

apptList.setOnItemClickListener(AppointmentDailyView.this);

apptList.setItemsCanFocus(true);

这是每个列表项的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>

<TextView android:id="@+id/apptListType" android:textSize="20px"
android:textColor="#333" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textStyle="bold" />

<TextView android:layout_below="@+id/apptListType" android:id="@+id/apptListTime"
android:textSize="15px" android:textColor="#666" android:layout_width="wrap_content"
android:layout_height="wrap_content" ></TextView>
<TextView android:layout_below="@+id/apptListTime" android:id="@+id/apptListContactName"
android:textSize="20px" android:textColor="#333" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>

<TextView android:layout_toRightOf="@+id/apptListContactName"
android:layout_below="@+id/apptListTime" android:id="@+id/apptListContactCompany"
android:textSize="15px" android:textColor="#666"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingLeft="5px" android:layout_marginTop="3px"></TextView>

感谢任何帮助,谢谢。

最佳答案

第 1 步:将 SimpleAdapter 扩展到您自己的类中(此处称为 MyAdapter)

第 2 步:覆盖 MyAdapter 中的 getView()

第 3 步:从返回 ViewgetView() 链接到父类(super class)(即调用 super.getView()) > 对于这一行

第 4 步:在 View 行上调用 findViewById() 以获取其中的小部件,并根据需要调整它们

第 5 步:从 MyAdapter 中的 getView() 返回行 View

第 6 步:在您使用 SimpleAdapter 的地方使用 MyAdapter

关于android - 如何从 android 中的自定义列表适配器获取控件 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3688922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com