Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. package elegion.com.secondappforcontentprovidertest
  2.  
  3. import android.content.ContentValues
  4. import android.database.Cursor
  5. import android.net.Uri
  6. import android.os.Bundle
  7. import android.support.v4.app.LoaderManager
  8. import android.support.v4.content.CursorLoader
  9. import android.support.v4.content.Loader
  10. import android.support.v7.app.AppCompatActivity
  11. import android.util.Log
  12. import android.widget.TextView
  13. import android.widget.Toast
  14.  
  15. class MainActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<Cursor> {
  16.  
  17. lateinit var textView: TextView
  18.  
  19. companion object {
  20. val urlAddress = "content://com.example.roomproject.musicprovider/album/1"
  21. }
  22.  
  23. override fun onCreate(savedInstanceState: Bundle?) {
  24. super.onCreate(savedInstanceState)
  25. setContentView(R.layout.activity_main)
  26.  
  27. textView = findViewById(R.id.tv_hello)
  28. textView.setOnClickListener {
  29. var contentValues = ContentValues()
  30. contentValues.put("id", 0)
  31. contentValues.put("name", "new Name")
  32. contentValues.put("release", "tomorrow")
  33. contentResolver.update(Uri.parse(urlAddress), contentValues, null, null)
  34. }
  35. supportLoaderManager.initLoader(12, null, this)
  36. }
  37.  
  38. override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
  39. return CursorLoader(this,
  40. Uri.parse("content://com.example.roomproject.musicprovider/album"),
  41. null,
  42. null,
  43. null,
  44. null)
  45. }
  46.  
  47. override fun onLoadFinished(loader: Loader<Cursor>?, data: Cursor?) {
  48. if (data != null && data.moveToFirst()) {
  49. val builder = StringBuilder()
  50. do {
  51. builder.append(data.getString(data.getColumnIndex("name"))).append("\n")
  52. } while (data.moveToNext())
  53. Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show()
  54. }
  55. }
  56.  
  57. override fun onLoaderReset(loader: Loader<Cursor>?) {
  58.  
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement