如何在TextView中显示UUID?

时间:2019-01-09 08:26:21

标签: java android uuid

我想像在unique code中使用uuid生成的mysql一样显示TextView

创建了database表并使用触发器创建uuid之后,我能够成功生成一个json对象并在android studio using retrofit中实现。但是,我可以显示jsonmysql的所有TextView数据,但不能显示uuid代码。 TextView显示为空白。

activity_main.xml

<androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/spacing_medium"
            android:layout_marginTop="@dimen/spacing_medium"
            android:visibility="visible"
            app:cardCornerRadius="0dp"
            app:cardElevation="2dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white"
                    android:gravity="center_vertical"
                    android:minHeight="?attr/actionBarSize"
                    android:orientation="horizontal">

                    <View
                        android:layout_width="@dimen/spacing_smlarge"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Unique Code"
                        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
                        android:textColor="@color/grey_80" />

                    <TextView
                        android:id="@+id/unique_code"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/spacing_large"
                        android:text="CXDT2887A"
                        android:textAppearance="@style/Base.TextAppearance.AppCompat.Title"
                        android:textColor="@color/colorAccent" />

                    <ImageButton
                        android:id="@+id/bt_copy_code"
                        android:layout_width="?attr/actionBarSize"
                        android:layout_height="?attr/actionBarSize"
                        android:background="?attr/selectableItemBackgroundBorderless"
                        android:tint="@color/grey_60"
                        app:srcCompat="@drawable/ic_content_copy" />

                </LinearLayout>

            </LinearLayout>

        </androidx.cardview.widget.CardView>

User.java

public class User {
private int id;
private String uuid;
private String email;
private String firstname;
private String lastname;
private String companyname;
private String postcode;
private String city;
private String state;
private String phonenumber;
private String address;
private String country;
private String status;
private int currency;
private String language;
private int email_verified;
private String photo;



public User(int id, String uuid, String email, String firstname, String lastname, String companyname, String address, String city,
            String state, String postcode, String country, String phonenumber, String status, int currency,
            String language, int email_verified, String photo) {

    this.id = id;
    this.uuid = uuid;
    this.email = email;
    this.firstname = firstname;
    this.lastname = lastname;
    this.companyname = companyname;
    this.address = address;
    this.city = city;
    this.state = state;
    this.postcode = postcode;
    this.country = country;
    this.phonenumber = phonenumber;
    this.status = status;
    this.currency = currency;
    this.language = language;
    this.email_verified = email_verified;
    this.photo = photo;


}

public int getId() {
    return id;
}
public String getUuid() {
    return uuid;
}
public String getEmail() {
    return email;
}
public String getFirstName() {
    return firstname;
}
public String getLastName() {
    return lastname;
}
public String getCompanyName() { return companyname; }
public String getAddress() {
    return address;
}
public String getCity() {
    return city;
}
public String getState() { return state; }
public String getPostcode() {
    return postcode;
}
public String getCountry() {
    return country;
}
public String getPhonenumber() {
    return phonenumber;
}
public String getStatus() { return status; }
public int getCurrency() { return currency; }
public String getLanguage() {
    return language;
}
public int getEmail_verified() { return email_verified; }
public String getPhoto() {
    return photo;
}


}

MainActivity.java

tv_unique_code = (TextView) findViewById(R.id.unique_code);
    bt_copy_code = (ImageButton) findViewById(R.id.bt_copy_code);
    bt_copy_code.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Tools.copyToClipboard(getApplicationContext(), tv_unique_code.getText().toString());
        }
    });

    tv_unique_code.setText(SharedPrefManager.getInstance(MainActivity.this).getUser().getUuid());

    parent_view = findViewById(android.R.id.content);

1 个答案:

答案 0 :(得分:0)

如果要使用以下来源的字符串加载对象:

String myId = UUID.randomUUID().toString();

您可以通过以下方式将textview设置为ID:

Textview displayId= (TextView) findViewById(R.id.display_id);
displayId.setText(User.getId());

希望能回答您的问题,否则请提供更多活动代码:)