gpt4 book ai didi

android - 如何检查已解析的 XML 标记值是否为空

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:16 25 4
gpt4 key购买 nike

目前我正在构建这个 android 应用程序,让我难过的一件事是句柄 ListView 项目都按我需要的顺序显示,但如果一个标签有一个空白值,它显示为一个 singleLine 对象只占用空间和间隔布局。

Error

此图像 ListView 项目中的所有空间都是一个正在发送的 Null 字符串,我对其进行了解析。我只想知道处理此错误的最佳方法?

我的 ListView 项目的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/ContactTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/White" />

<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/ContactTitle"
android:layout_below="@+id/ContactTitle"
android:layout_marginRight="17dp"
android:textColor="@color/White" />

<TextView
android:id="@+id/MainPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/Name"
android:layout_below="@+id/Name"
android:textColor="@color/White"/>

<TextView
android:id="@+id/Cell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/MainPhone"
android:layout_below="@+id/MainPhone"
android:textColor="@color/White" />

<TextView
android:id="@+id/Fax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Cell"
android:layout_below="@+id/Cell"
android:textColor="@color/White" />

<TextView
android:id="@+id/Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Fax"
android:layout_below="@+id/Fax"
android:ellipsize="marquee"
android:textColor="@color/EmailColor" />

</RelativeLayout>

我需要检查空的 TextViews 并隐藏它们。我的代码在这里:

protected void onPostExecute(String result) {
String xml = result;
String KEY_ITEM = "Contact";
String KEY_CONTACTTITLE = "ContactTitle";
String KEY_NAME = "Name";
String KEY_MAINPHONE = "Phone";
String KEY_CELLPHONE = "Cell";
String KEY_FAX = "Fax";
String KEY_EMAIL = "Email";
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

Document doc = getDomElement(xml);
if (xml != null) {
{
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
if (getValue(e, KEY_CONTACTTITLE) != null) {
map.put(KEY_CONTACTTITLE,
getValue(e, KEY_CONTACTTITLE));
} else {
TextView contactTitle = (TextView) findViewById(R.id.ContactTitle);
}
if (getValue(e, KEY_NAME) != null) {
map.put(KEY_NAME, getValue(e, KEY_NAME));
}
if (getValue(e, KEY_MAINPHONE) != null) {
map.put(KEY_MAINPHONE, getValue(e, KEY_MAINPHONE));
}

if (getValue(e, KEY_CELLPHONE) != null) {
map.put(KEY_CELLPHONE, getValue(e, KEY_CELLPHONE));
}

if (getValue(e, KEY_FAX) != null) {
map.put(KEY_FAX, getValue(e, KEY_FAX));
}

if (getValue(e, KEY_EMAIL) != null) {
map.put(KEY_EMAIL, getValue(e, KEY_EMAIL));
}

menuItems.add(map);
menuItems = simple;
}

String[] hashMapObjects = { KEY_CONTACTTITLE, KEY_NAME,
KEY_MAINPHONE, KEY_CELLPHONE, KEY_FAX, KEY_EMAIL };
int[] listItemObjects = { R.id.ContactTitle, R.id.Name,
R.id.MainPhone, R.id.Cell, R.id.Fax, R.id.Email };

ListAdapter adapter = new SimpleAdapter(
MoveContactsActivity.this, menuItems,
R.layout.listed_contacts, hashMapObjects,
listItemObjects);
setListAdapter(adapter);

我正在尝试检查该值是否为 null 以将对象添加到 HashMap。这当然行不通,我想知道如何检查那些 null TextViews 并隐藏它们。

最佳答案

如果 TextView 中没有显示内容,您只需调用 View.setVisibility(View.GONE)。由于您正在使用 SimpleAdapter 并且没有自定义 getView 方法,因此最简单的方法是使用 SimpleAdapter.ViewBinder :

String[] hashMapObjects = { 
KEY_CONTACTTITLE, KEY_NAME, KEY_MAINPHONE, KEY_CELLPHONE, KEY_FAX, KEY_EMAIL
};

int[] listItemObjects = {
R.id.ContactTitle, R.id.Name, R.id.MainPhone, R.id.Cell, R.id.Fax, R.id.Email
};

SimpleAdapter adapter = new SimpleAdapter(MoveContactsActivity.this, menuItems, R.layout.listed_contacts, hashMapObjects, listItemObjects);
SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object object, String value) {
if (!(view instanceof TextView)) {
return false;
}

final TextView text = (TextView) view;

text.setVisibility(TextUtils.isEmpty(value) ? View.GONE : View.VISIBLE);
text.setText(value);

return true;
}
};

adapter.setViewBinder(binder);
setListAdapter(adapter);

关于android - 如何检查已解析的 XML 标记值是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15300464/

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