2014-10-27 76 views
1

我想弄清楚爲什麼在我的android應用程序View.INVISIBLE和View.GONE不工作。Android setVisibility View.INVISIBLE和View.GONE不工作

我相信我的邏輯計算正確!= null值,但也許它不是?

下面是我的語法。我的目標是,如果他們的信息我想向用戶顯示這些信息。如果facebook,twitter,yelp等沒有顯示信息,那麼我不希望將一個大的空白空間呈現給我的用戶(這並不令人滿意)。我的問題是,我做錯了什麼,我該如何解決這個問題?我的語法如下。由於

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

    allTheshipInfo infoHolder; 

    if(convertView == null){ 
     convertView = inflater.inflate(R.layout.show_dealership_information, parent, false); 

     infoHolder = new allTheshipInfo(); 
     infoHolder.facebook = (TextView) convertView.findViewById(R.id.theFacebook); 
     infoHolder.twitter = (TextView) convertView.findViewById(R.id.theTwitter); 
     infoHolder.youtube = (TextView) convertView.findViewById(R.id.theYoutube); 
     infoHolder.googlePlus = (TextView) convertView.findViewById(R.id.theGoogle_plus); 
     infoHolder.yelp = (TextView) convertView.findViewById(R.id.theYelp); 

     convertView.setTag(infoHolder); 

    } else { 
     infoHolder = (allTheDealershipInfo) convertView.getTag(); 

    } 

    try { 
     JSONObject jsonObject = this.dataArray.getJSONObject(position); 

     infoHolder.dealerships.setText(jsonObject.getString("business")); 
     infoHolder.state.setText(jsonObject.getString("state")); 
     infoHolder.city.setText(jsonObject.getString("city")); 
     infoHolder.phone.setText(jsonObject.getString("phone")); 
     infoHolder.address.setText(jsonObject.getString("address")); 
     infoHolder.zip.setText(jsonObject.getString("zip")); 
     infoHolder.email.setText(jsonObject.getString("email")); 
     infoHolder.website.setText(jsonObject.getString("website")); 
     infoHolder.facebook.setText(jsonObject.getString("facebook")); 
     infoHolder.twitter.setText(jsonObject.getString("twitter")); 

     infoHolder.youtube.setText(jsonObject.getString("youtube")); 
     infoHolder.googlePlus.setText(jsonObject.getString("google_plus")); 
     infoHolder.yelp.setText(jsonObject.getString("yelp")); 


        if(facebook.isEmpty()){ 
      infoHolder.facebook.setVisibility(View.GONE); 
      Toast.makeText(activity, "Facebook Gone", Toast.LENGTH_SHORT).show(); 
     } else { 
      infoHolder.facebook.setText(facebook); 
      infoHolder.facebook.setVisibility(View.VISIBLE); 
      Toast.makeText(activity, "Facebook Here", Toast.LENGTH_SHORT).show(); 
     } 

     if(twitter.isEmpty()){ 
      infoHolder.twitter.setVisibility(View.GONE); 
      Toast.makeText(activity, "Twitter Gone", Toast.LENGTH_SHORT).show(); 
     } else { 
      infoHolder.twitter.setText(twitter); 
      infoHolder.twitter.setVisibility(View.VISIBLE); 
      Toast.makeText(activity, "Twitter Here", Toast.LENGTH_SHORT).show(); 
     } 

     if(youtube.isEmpty()){ 
      infoHolder.youtube.setVisibility(View.GONE); 
      Toast.makeText(activity, "YouTube Gone", Toast.LENGTH_SHORT).show(); 
     } else { 
      infoHolder.youtube.setText(youtube); 
      infoHolder.youtube.setVisibility(View.VISIBLE); 
      Toast.makeText(activity, "YouTube Here", Toast.LENGTH_SHORT).show(); 
     } 

     if(googlePlus.isEmpty()){ 
      infoHolder.googlePlus.setVisibility(View.GONE); 
      Toast.makeText(activity, "GooglePlus Gone", Toast.LENGTH_SHORT).show(); 
     } else { 
      infoHolder.googlePlus.setText(googlePlus); 
      infoHolder.googlePlus.setVisibility(View.VISIBLE); 
      Toast.makeText(activity, "GooglePlus Here", Toast.LENGTH_SHORT).show(); 
     } 

     if(yelp.isEmpty()){ 
      infoHolder.yelp.setVisibility(View.GONE); 
      Toast.makeText(activity, "Yelp Gone", Toast.LENGTH_SHORT).show(); 
     } else { 
      infoHolder.yelp.setText(yelp); 
      infoHolder.yelp.setVisibility(View.VISIBLE); 
      Toast.makeText(activity, "GooglePlus Here", Toast.LENGTH_SHORT).show(); 
     } 

     Toast.makeText(activity, "Call " + jsonObject.getString("business"), Toast.LENGTH_SHORT).show(); 
     Toast.makeText(activity, "# " + jsonObject.getString("phone"), Toast.LENGTH_SHORT).show(); 

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

    return convertView; 
} 

我的XML

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="40dp" 
    android:orientation="horizontal"> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="invisible" 
     android:text="@string/facebook" 
     android:autoLink="web" 
     android:id="@+id/theFacebook" 
     android:layout_column="0" 
     android:layout_weight="1" /> 

</TableRow> 
<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="invisible" 
     android:text="@string/twitter" 
     android:autoLink="web" 
     android:id="@+id/theTwitter" 
     android:layout_column="0" 
     android:layout_weight="1" /> 

</TableRow> 
<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="invisible" 
     android:text="@string/google_plus" 
     android:autoLink="web" 
     android:id="@+id/theGoogle_plus" 
     android:layout_column="0" 
     android:layout_weight="1" /> 

</TableRow> 
<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="invisible" 
     android:text="@string/yelp" 
     android:autoLink="web" 
     android:id="@+id/theYelp" 
     android:layout_column="0" 
     android:layout_weight="1" /> 

</TableRow> 
    <TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="invisible" 
     android:text="@string/youtube" 
     android:autoLink="web" 
     android:id="@+id/theYoutube" 
     android:layout_column="0" 
     android:layout_weight="1" /> 

</TableRow>  

</TableLayout> 
+1

您setVisibility方法應該是,如果外(convertView == NULL)身上。 – 2014-10-27 19:14:25

+0

@ moh.sukhni我改變了一些東西。它不工作。我究竟做錯了什麼? – valhalla 2014-10-27 19:51:18

+0

你在'else'子句中放了很多東西。它應該只包含infoHolder =(allTheDealershipInfo)convertView.getTag();' – njzk2 2014-10-27 20:03:07

回答

1

我想你想顯示/隱藏基於是否可用相關JSON數據的textviews。 至於你當前的代碼:

infoHolder.yelp = (TextView) convertView.findViewById(R.id.theYelp); 

if(infoHolder.yelp != null){ 
    ... 
} 

的,如果你聲明只是檢查的TextView是否是空語句,它永遠不會因爲它的分配在開始一個TextView爲空。在決定是否顯示/隱藏文本視圖之前,您應該首先確定json數據是否存在。

例如:

JSONObject jsonObject = this.dataArray.getJSONObject(position); 
String yelpStr = jsonObject.optString("yelp"); 
if(yelpStr != null){ 
    infoHolder.yelp.setText(yelpStr); 
    infoHolder.yelp.setVisibility(View.VISIBLE); 
    Toast.makeText(activity, "Yelp Here", Toast.LENGTH_LONG).show(); 
}else{ 
    infoHolder.yelp.setVisibility(View.GONE); 
    Toast.makeText(activity, "Yelp Gone", Toast.LENGTH_LONG).show(); 
} 

而且,我相信你放錯地方的右大括號,它應該是正確使用標籤檢索infoHolder後:

if(convertView == null){ 
    ... 
}else{ 
    infoHolder = (allTheDealershipInfo) convertView.getTag(); 
} 

確保ü結束,如果/ else語句,用於正確分配infoHolder,以便按預期方式在代碼的其餘部分使用文本視圖。

+0

感謝您的幫助。我很感激。我已經更新了上面的語法以適應工作。我用綠色檢查標記了這個答案,我相信這表明這是正確的答案。我微調了一下語法,並將'!= null'改爲'isEmpty()'方法。我現在唯一的問題是Toast消息出現兩次了嗎? – valhalla 2014-10-27 21:52:51

+0

爲列表視圖上的每個項目調用getView方法,它將返回每個項目的視圖。這就是說,每當您的列表加載或滾動時,每個可見的項目可能會返回一個Toast。如果您確實想要調試消息,則可以嘗試使用Log.d(標籤,消息),並遵循LogCat中發生的情況。 – Eric 2014-10-27 22:43:27

0

我認爲你必須做這樣的事情(僅適用於Facebook的爲例):

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

    allTheshipInfo infoHolder; 

    if(convertView == null){ 
     convertView = inflater.inflate(R.layout.show_dealership_information, parent, false); 

     infoHolder = new allTheshipInfo(); 
     infoHolder.facebook = (TextView) convertView.findViewById(R.id.theFacebook); 
     convertView.setTag(infoHolder); 

    } else infoHolder = (allTheDealershipInfo) convertView.getTag(); 

    try { 

     JSONObject jsonObject = this.dataArray.getJSONObject(position); 
     if (jsonObject.getString("facebook") == null) 
      infoHolder.facebook.setVisibility(View.GONE); 

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

    return convertView; 
} 
+0

感謝您的幫助。我已經改變了我的語法。看看,謝謝你的幫助 – valhalla 2014-10-27 22:17:15

相關問題