如果值在 Android 中为空或 null,则显示默认文本
Posted
技术标签:
【中文标题】如果值在 Android 中为空或 null,则显示默认文本【英文标题】:Displaying Default Text if Value is empty or null in Android 【发布时间】:2017-10-07 16:37:12 【问题描述】:我有一个应用程序,其中包含我 (MyProfileAboutFragment
) 和我的朋友 (AboutFragment
) 的个人资料活动。
我从服务器获取我的朋友列表,当我点击它时,我通过意图将用户名、照片、关于、电话等发送到我的AboutFragment
。 如果缺少某个字段,则会显示默认文本。这可以正常工作。
现在我想这样做:当我打开 MyProfileAboutFragment
时,它应该从我的 sqlite 数据库中获取我的个人数据,并在缺少字段时返回默认文本。 我得到的不是默认文本,而是“null”。
我在MyProfileAboutFragment
中做了所有事情,就像在我的AboutFragment
中一样,除了不是从服务器返回我的数据是从 sqlite 获取的。
AboutFragment.java
:
public class AboutFragment extends Fragment
private TextView userName, contactsStatus, contactUserStars, contactUserAbout;
private String contactId, contactName;
private ImageView contactStatusImage;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.profile_fragment_about, container,
false);
userName = (TextView) view.findViewById(R.id.contactUserName);
contactsStatus = (TextView) view.findViewById(R.id.contactsStatus);
contactUserStars = (TextView) view.findViewById(R.id.userStars);
contactUserAbout = (TextView) view.findViewById(R.id.contactUserAbout);
contactStatusImage = (ImageView) view.findViewById(R.id.contactStatusImage);
ContactProfileActivity activity = (ContactProfileActivity) getActivity();
Intent fromAvailableActivityIntent = activity.getIntent();
String userId = fromAvailableActivityIntent.getStringExtra("userId");
String contactUserName = fromAvailableActivityIntent.getStringExtra("userName");
String contactAbout = fromAvailableActivityIntent.getStringExtra("about");
userName.setText(contactUserName);
if (contactAbout != null && !contactAbout.isEmpty())
contactUserAbout.setText(contactAbout);
return view;
MyProfileAboutFragment.java
:
public class MyProfileAboutFragment extends Fragment
private TextView userName, eMail, myPhone, stars, aboutMe;
private SQLiteHandler db;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.profile_fragment_about_me, container,
false);
db = new SQLiteHandler(getActivity());
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
String id = user.get("uid");
String name = user.get("name");
String email = user.get("email");
String phone = user.get("phone");
String about = user.get("about");
userName = (TextView) view.findViewById(R.id.userName);
eMail = (TextView) view.findViewById(R.id.userEmail);
myPhone = (TextView) view.findViewById(R.id.userPhone);
stars = (TextView) view.findViewById(R.id.userStars);
aboutMe = (TextView) view.findViewById(R.id.userAboutMe);
userName.setText(name);
eMail.setText(email);
myPhone.setText(phone);
// I did the same exact thing in AboutFragment and it worked
if (about != null && !about.isEmpty())
aboutMe.setText(about);
return view;
profile_fragment_about.xml
:
<RelativeLayout
android:layout_
android:layout_
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/contactUserAboutTitle"
android:layout_
android:layout_
android:maxLines="1"
android:text="@string/about"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/mycolorred"
android:textStyle="bold" />
<TextView
android:id="@+id/contactUserAbout"
android:layout_
android:layout_
android:layout_below="@id/contactUserAboutTitle"
android:layout_marginTop="8dp"
android:text="@string/long_text"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
</RelativeLayout>
profile_fragment_about_me.xml
:
<RelativeLayout
android:layout_
android:layout_
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/userAbout"
android:layout_
android:layout_
android:maxLines="1"
android:text="@string/about"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/mycolorred"
android:textStyle="bold" />
<TextView
android:id="@+id/userAboutMe"
android:layout_
android:layout_
android:layout_below="@id/userAbout"
android:layout_marginTop="8dp"
android:text="@string/long_text"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
</RelativeLayout>
SQLITE:
/**
* Storing user details in database
*/
public void addUser(String name, String email, String uid, String created_at, String about, String image, String phone)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_UID, uid); // Email
values.put(KEY_CREATED_AT, created_at); // Created At
values.put(KEY_ABOUT, about);
values.put(IMAGE, image);
values.put(PHONE, phone);
// Inserting Row
long id = db.insert(TABLE_USER, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
/**
* Getting user data from database
*/
public HashMap<String, String> getUserDetails()
HashMap<String, String> user = new HashMap<>();
String selectQuery = "SELECT * FROM " + TABLE_USER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0)
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("phone", cursor.getString(3));
user.put("uid", cursor.getString(4));
user.put("about", cursor.getString(5));
user.put("image", cursor.getString(6));
user.put("created_at", cursor.getString(7));
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
编辑:
在我的个人资料活动中,我在 Bharat singh 的回答之后添加了这个,以显示是否没有电话号码:
if (about != null && !about.isEmpty() && !about.equals("null"))
myPhone.setText(phone);
XML:
<RelativeLayout
android:layout_
android:layout_
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_horizontal_margin">
<!--android:clickable="true"-->
<ImageView
android:id="@+id/userPhoneImage"
android:layout_
android:layout_
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:contentDescription="@string/phone"
app:srcCompat="@drawable/ic_icon_phone" />
<TextView
android:id="@+id/userPhone"
android:layout_
android:layout_
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_toEndOf="@id/userPhoneImage"
android:text="@string/phone_default"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</RelativeLayout>
编辑 2:
Bharat singh 也回答了这个问题:
if (!about.isEmpty() && !about.equals("null"))
myPhone.setText(phone);
【问题讨论】:
请展示您的 sqlite 实现... 【参考方案1】:替换这个
if (about != null && !about.isEmpty())
aboutMe.setText(about);
有了这个
if (about != null && !about.isEmpty() && !about.equals("null"))
aboutMe.setText(about);
作为数据库返回“null”作为字符串
【讨论】:
它仍然显示 null 而不是默认文本。 检查是否 (!about.equals("null")) 是的,我这样做了,用这个(复制粘贴)替换了我的旧代码,但我仍然得到默认文本的空值。 还有一个问题 - 当我对我的电话号码执行相同操作时,我总是得到默认文本,即使电话号码存在。 然后只检查电话号码的这些条件 !about.isEmpty() && !about.equals("null")以上是关于如果值在 Android 中为空或 null,则显示默认文本的主要内容,如果未能解决你的问题,请参考以下文章