Android setVisibility View.INVISIBLE和View.GONE无效

时间:2014-10-27 19:10:37

标签: java android xml

我试图找出为什么我的Android应用中的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>

2 个答案:

答案 0 :(得分:1)

我认为您正在尝试根据相关的json数据是否可用来显示/隐藏文本视图。 至于你目前的代码:

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

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

您声明的if语句只是检查textview是否为null,因为它在开头分配了一个textview,所以它永远不会为null。在决定是否显示/隐藏文本视图之前,应首先确定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();
}

确保你结束if / else语句以正确分配infoHolder,以便在其余的代码中使用textviews。

答案 1 :(得分: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;
}