gpt4 book ai didi

java - Android setVisibility View.INVISIBLE 和 View.GONE 不工作

转载 作者:行者123 更新时间:2023-11-30 02:35:45 24 4
gpt4 key购买 nike

我想弄清楚为什么我的 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>

最佳答案

我认为您正试图根据相关的 json 数据是否可用来显示/隐藏 TextView 。至于您当前的代码:

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

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

您声明的 if 语句只是检查 textview 是否为 null,它永远不会为 null,因为它一开始就分配了一个 textview。先判断json数据是否存在,再决定是否显示/隐藏textview。

例如:

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();
}

确保结束用于正确分配 infoHolder 的 if/else 语句,以便在其余代码中按预期使用 TextView 。

关于java - Android setVisibility View.INVISIBLE 和 View.GONE 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595051/

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