2013-03-08 84 views
3

目前我正在構建這個android應用程序,有一件事情讓我難住的是句柄ListView項目都顯示在我需要他們的順序,但如果一個標籤有一個空值,它顯示作爲一個單線對象,除了空間和空間佈局之外。如何檢查解析的XML標籤空值價值

Error

都在這個圖像的ListView項目的空間被送到一個空字符串,我解析它。 我只是想知道處理此錯誤的最佳方法?

我爲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。這當然不起作用,我想知道如何檢查那些nullTextViews並隱藏它們。

+0

告訴我您的適配器,請 – 2013-03-08 20:56:43

+0

我用簡單的適配器......的String [] hashMapObjects = {KEY_CONTACTTITLE,KEY_NAME, \t \t \t \t \t \t \t KEY_MAINPHONE,KEY_CELLPHONE,KEY_FAX,KEY_EMAIL}; \t \t \t \t \t INT [] listItemObjects = {R.id.ContactTitle,R.id.Name, \t \t \t \t \t \t \t R.id.MainPhone,R.id.Cell,R.id.Fax, R.id.Email}; \t \t \t \t \t ListAdapter適配器=新SimpleAdapter( \t \t \t \t \t \t \t MoveContactsActivity.this,的菜單項, \t \t \t \t \t \t \t R.layout.listed_contacts,hashMapObjects, \t \t \t \t \t \t \t listItemObjects);然後我設置適配器。 – Keeano 2013-03-10 00:33:47

+0

如果您希望某些列表項與其他列表項不同,則無法使用簡單的適配器。你必須使用創建你的適配器,然後在'getView'函數中,你可以檢查空值,並且不會將它們設置到任何小部件到列表項中。 – 2013-03-10 07:33:42

回答

2

如果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); 
1

我不知道您的列表視圖項目結構。

我的建議是:

檢查,如果標籤爲空,做一些事情來解決這個問題,比如集合TextView的知名度=不見了。

1

正如威廉說,你可以擴展簡單的適配器的可見性增加,如果文本視圖爲null或空:

public class MyAdapter extends SimpleAdapter { 

    public MyAdapter(Context context, List<? extends Map<String, ?>> data, 
      int resource, String[] from, int[] to) { 
     super(context, data, resource, from, to); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      if(convertView!=null) //reset the visibility if a convert view is used 
       convertView.setVisibility(View.VISIBLE); 

     View v = super.getView(position, convertView, parent); 

     //text recovering from data source. here I check the name field 
     //you can add any test and hide any views using views id and setVisibility method 
     String displayedText = ((HashMap<String, String>) this.getItem(position)).get("Name"); 
     if(displayedText == null || displayedText.isEmpty()) 
      v.setVisibility(View.GONE); 


     return v; 
    } 
} 

使用這個類而不是SimpleAdapter,那麼你可以恰克的getView方法對你的ListView的任何視圖中的任何字段添加條件。

有關信息:getView返回將顯示在ListView中給定位置的視圖,您可以通過恢復給定位置處的項來根據數據源更改這些視圖中的任何內容。

1

只要避免添加到您的收藏被傳遞到適配器用於填充列表視圖

boolean elementFound = false; 
for (int i = 0; i < nl.getLength(); i++) { 
    HashMap<String, String> map = new HashMap<String, String>(); 
    Element e = (Element) nl.item(i); 
    elementFound = false; 
    if (getValue(e, KEY_CONTACTTITLE) != null) { 
     map.put(KEY_CONTACTTITLE, 
       getValue(e, KEY_CONTACTTITLE)); 
     elementFound = true; 
    } else { 
     TextView contactTitle = (TextView) findViewById(R.id.ContactTitle); 
    } 
    if (getValue(e, KEY_NAME) != null) { 
     map.put(KEY_NAME, getValue(e, KEY_NAME)); 
     elementFound = true; 
    } 
    if (getValue(e, KEY_MAINPHONE) != null) { 
     map.put(KEY_MAINPHONE, getValue(e, KEY_MAINPHONE)); 
     elementFound = true; 
    } 

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

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

    if (getValue(e, KEY_EMAIL) != null) { 
     map.put(KEY_EMAIL, getValue(e, KEY_EMAIL)); 
     elementFound = true; 
    } 
    if(elementFound){ 
     menuItems.add(map); 
    } 
    //I don't understand whay are you doing this 
    //menuItems = simple; 
} 
1

與檢查值空值非空String也喜歡:

if (getValue(e, KEY_NAME) != null && !getValue(e,KEY_NAME).toString().equalsIgnoreCase("")) { 
map.put(KEY_NAME, getValue(e, KEY_NAME)); 
} 
1

你正在使用SimpleAdapter。這很好,但我認爲變更管理是軟件的關鍵。您應該延長BaseAdapter,並且應該處理getView方法。

我知道SimpleAdapter實現起來相當快,但是當變化出現時,它會騷擾你。如果您現在爲custom adapter再寫一個文件,您將在變化到來時放鬆。

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 
    if (convertView == null) { 

     convertView = infalter.inflate(R.layout.list_row, null); 
     holder = new ViewHolder(); 
     holder.tvRawName = (TextView) convertView 
       .findViewById(R.id.tvRawName); 
     holder.tvRawEmail= (TextView) convertView 
       .findViewById(R.id.tvRawEmail); 
     convertView.setTag(holder); 

    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    try { 
     if(data.get(position).get("Email")!=null && data.get(position).get("Email").length()>0) 
     { 
       holder.tvRawEmail.setVisibility(View.VISIBLE); 
       holder.tvRawEmail.setText(data.get(position).get("Email")); 
     }else 
     { 
       holder.tvRawEmail.setVisibility(View.GONE); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

//do like for all 

    return convertView; 
} 

public class ViewHolder { 
    TextView tvRawName; 
    TextView tvRawEmail; 
}