我想弄清楚爲什麼在我的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>
您setVisibility方法應該是,如果外(convertView == NULL)身上。 – 2014-10-27 19:14:25
@ moh.sukhni我改變了一些東西。它不工作。我究竟做錯了什麼? – valhalla 2014-10-27 19:51:18
你在'else'子句中放了很多東西。它應該只包含infoHolder =(allTheDealershipInfo)convertView.getTag();' – njzk2 2014-10-27 20:03:07