如果Android中的值为空或为空,则显示默认文本

时间:2017-05-09 11:28:34

标签: java android xml sqlite android-fragments

我有一个应用程序,可以为我(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_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_horizontal_margin">

<TextView
    android:id="@+id/contactUserAboutTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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_width="wrap_content"
    android:layout_height="wrap_content"
    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_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_horizontal_margin">

<TextView
    android:id="@+id/userAbout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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_width="wrap_content"
    android:layout_height="wrap_content"
    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_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:padding="@dimen/activity_horizontal_margin">
        <!--android:clickable="true"-->

        <ImageView
            android:id="@+id/userPhoneImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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:

巴拉特·辛格也回答了这个问题:

    if (!about.isEmpty() && !about.equals("null")) {
        myPhone.setText(phone);
    }

1 个答案:

答案 0 :(得分:3)

替换此

 if (about != null && !about.isEmpty()) {
    aboutMe.setText(about);
}

用这个

 if (about != null && !about.isEmpty() && !about.equals("null")) {
        aboutMe.setText(about);
    }

作为数据库返回“null”作为字符串