联系 Android 中的应用程序
Contact Application in Android
我想构建一个联系人应用程序,它从 android 设备读取联系人并将其显示在列表视图中。单击列表视图的一个项目时,将打开一个新的 activity,其中将显示联系人的详细信息。我已经显示了列表视图,但在从 simplecursoradapter 获取数据时遇到了问题。有人可以帮助我使用 setOnItemClickListener() 方法吗?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
read()
}
fun read()
{
var cursor : Cursor? =
contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,null,null,null)
startManagingCursor(cursor)
var selected_columns = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID)
var to = intArrayOf(android.R.id.text1)
var simple : SimpleCursorAdapter = SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,selected_columns,to,0)
my_listview.adapter = simple
my_listview.setOnItemClickListener { parent, view, position, id ->
val intent = Intent(this, Contactdetails::class.java)
startActivity(intent)
}
}
}
您不需要从 SimpleCursorAdapter
获取数据。就这样吧:
my_listview.setOnItemClickListener { parent, view, position, id ->
val intent = Intent(this, Contactdetails::class.java)
intent.putExtra("CONTACT_DATA" , cursor.getString(position))
startActivity(intent)
}
我想构建一个联系人应用程序,它从 android 设备读取联系人并将其显示在列表视图中。单击列表视图的一个项目时,将打开一个新的 activity,其中将显示联系人的详细信息。我已经显示了列表视图,但在从 simplecursoradapter 获取数据时遇到了问题。有人可以帮助我使用 setOnItemClickListener() 方法吗?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
read()
}
fun read()
{
var cursor : Cursor? =
contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,null,null,null)
startManagingCursor(cursor)
var selected_columns = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID)
var to = intArrayOf(android.R.id.text1)
var simple : SimpleCursorAdapter = SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,selected_columns,to,0)
my_listview.adapter = simple
my_listview.setOnItemClickListener { parent, view, position, id ->
val intent = Intent(this, Contactdetails::class.java)
startActivity(intent)
}
}
}
您不需要从 SimpleCursorAdapter
获取数据。就这样吧:
my_listview.setOnItemClickListener { parent, view, position, id ->
val intent = Intent(this, Contactdetails::class.java)
intent.putExtra("CONTACT_DATA" , cursor.getString(position))
startActivity(intent)
}