首页 > 解决方案 > 在 kotlin 中,如何创建个人资料图片并将联系人姓名的首字母显示为离线短信应用中的个人资料图片?

问题描述

我必须创建离线应用程序,我想将个人资料图片附在联系人姓名旁边,并将第一个字母作为个人资料图片。但我不明白如何编码。**在移动设备上运行的应用程序的屏幕截图。**

我已经搜索过但一无所获。

1.

class Inbox : AppCompatActivity(){

    private val requestReceiveSms: Int =1
    private val requestReadSms: Int = 2

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.readsms)

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS) !=
            PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this, arrayOf(android.Manifest.permission.SEND_SMS,android.Manifest.permission.READ_CONTACTS),
                requestReadSms
            )
        } else {
            refreshSmsInbox()
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
                                            grantResults: IntArray) {
        if(requestCode == requestReadSms) refreshSmsInbox()
    }

private fun refreshSmsInbox() {

        val smsList = ArrayList<SmsData>()

        val cursor = contentResolver.query(Uri.parse("content://sms/inbox"),null,null,null,null)

        if(cursor!!.moveToFirst()){

            val nameID = cursor.getColumnIndex("address")

            val messageID = cursor.getColumnIndex("body")

            val dateID = cursor.getColumnIndex("date")

            do{

                val dateString = cursor.getString(dateID)

                smsList.add(SmsData(cursor.getString(nameID),cursor.getString(messageID),Date(dateString.toLong()).toString()))
            }while (cursor.moveToNext())

        }

        cursor.close()

        val  adapter = ListAdapter(this, smsList)

        sms_list_view.adapter = adapter
    }


}

文件

2.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
<ListView

        android:id="@+id/sms_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"
        tools:ignore="Suspicious0dp"></ListView>
</LinearLayout>

预期的

个人资料图片以及联系人姓名。

实际结果

只是联系人姓名。

标签: androidandroid-layoutkotlin

解决方案


您可以TextView在 ListView 的项目布局中放置一个。现在我们为此设置一个圆形彩色背景TextView。首先,我们drawable为 TextView 定义一个背景,

profile_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="oval">
            <solid android:color="@android:color/holo_blue_bright"/>
        </shape>
    </item>

</selector>

TextView我们在 ListView 的项目布局中将此背景设置为 a 。

listview_item_layout.xml

<TextView
    android:id="@+id/profile_text"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:background="@drawable/sample"
    />

现在,我们需要将 the 的第一个字母设置nameId为 this TextView

val firstLetter = nameId.subString( 0 , 1 ).toUpperCase()
profile_text.text = firstLetter 

推荐阅读