Issue
I need to know how to call a ProgressBar (or any other kind of button) into a class, outside of "onCreate" method. I saw in Java I can do it calling findViewById(), however it's been rid of in Kotlin. For example I was trying a thing like this:
var myProgressBar = findViewById(R.id.myProgressBar) as ProgressBar
But it gives me the unresolved reference for the find view method.
Thanks in advance for help
______________UPDATE____________________
Here's my DownloadManager custom class
class DownloadSoundController(context: Context): Activity() {
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
.....
.....
Thread(Runnable{
while (starter) {
var query = DownloadManager.Query()
query.setFilterById(refid)
var c = downloadManager.query(query)
if (c.moveToFirst()) {
var totalSizeIndex = c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
var downloadedIndex = c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
val fileSize = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
var totalSize = c.getInt(totalSizeIndex)
var downloaded = c.getInt(downloadedIndex)
var progress = 0.0
var raffronto: Long = -1
if (fileSize != raffronto) {
progress = downloaded * 100.0 / totalSize
//println("myTag - downloaded % :$progress, downloaded var: $downloaded")
myProgressBar.progress = progress.toInt()
if (progress == 100.0) {
starter = false
}
} else {
//println("myTag - downloaded = 100 progress: $progress, $downloaded")
myProgressBar.progress = progress.toInt()
if (progress == 100.0) {
starter = false
}
}
}
}
}).start()
Here's my manifest:
<application
android:hardwareAccelerated="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
//ATTENTION
<activity android:name="com.example.myapplication.DownloadSoundController"/>
// here it returns ERROR, on default constructor
<receiver android:name=".DownloadBroadcastManager">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
</application>
_________UPDATE_2__________
I polished the code and I found several issues that sound really "weird" to me:
1) to stop the nullpoint exception about myProgressBar.progress = value, I found out I have to initialize myProgressBar.progress = 0
2) With println() my thread stamps into the log exactly, step by step, the downloaded amount of my file, but if I add the myProgressBar.progress = progress.toInt() (progress var is THE SAME I stamp into the println()!) in the Thread... it starts to be async in a way it EVEN STARTS BEFORE the download has begun.. leading to several errors and multiple downloads"
I really don't know what's going on
Solution
findViewById now returns a generic type which extends View (docs).
final <T extends View> T findViewById(...)
You can use
val progressBar = findViewById<ProgressBar>(R.id.myProgressBar)
Answered By - LppEdd
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.