https://kotlinlang.org logo
#android
Title
# android
h

Hele

06/25/2018, 3:12 PM
I created row
n

neil.armstrong

06/25/2018, 3:12 PM
probably better to use a thread to prevent spam…Could you paste the activity so I can see how you are setting it up?
h

Hele

06/25/2018, 3:12 PM
sure
but is this slack not about also solving problems ?
so it could be an example for others also in case they need
n

neil.armstrong

06/25/2018, 3:13 PM
it is, that’s why I’m helping you 🙂 Just don’t want to send messages to all 15k users at once. It is better to do this kind of thing in a thread if we can
👍 1
h

Hele

06/25/2018, 3:14 PM
oh ok sure
no problem
I mean my code should be fine s I did read and watched examples
n

neil.armstrong

06/25/2018, 3:16 PM
Looks like you are setting the content view to
ZXingScannerView
rather than your activity layout? I haven’t played with scanners in a long time but I think that is a library provided view rather than your own?
h

Hele

06/25/2018, 3:17 PM
yes exactly
that is a provided library
n

neil.armstrong

06/25/2018, 3:17 PM
Copy code
public override fun onCreate(state: Bundle?) {
        super.onCreate(state)
        mScannerView = ZXingScannerView(this)   // Programmatically initialize the scanner view
        setContentView(mScannerView)                // Set the scanner view as the content view
    }
that means that the call to grab the recycler view won’t work, as it isn’t being shown
Ah man I did do this specific problem years ago with a QR code scanner, I think it was the same library too
h

Hele

06/25/2018, 3:18 PM
but I need to set it to scanner layout
as it has the camera and scanner
is not it correct ?
n

neil.armstrong

06/25/2018, 3:18 PM
Yes, and you want to overlay stuff on top of it?
or just get the result from the scanning operation?
h

Hele

06/25/2018, 3:18 PM
it opens that scanning layout, scans and get the result
n

neil.armstrong

06/25/2018, 3:18 PM
if you just need the result, you can do it as part of another activity, and then just return the result via intents
h

Hele

06/25/2018, 3:18 PM
which I do
I fill a list
n

neil.armstrong

06/25/2018, 3:19 PM
I think from memory you need to do the scanning operation (or at least it is easier) from a separate activity
h

Hele

06/25/2018, 3:19 PM
Copy code
booksMutable.add(Book(rawResult.text.toLong()))
that is a mutableList
n

neil.armstrong

06/25/2018, 3:20 PM
yea that bit looks fine, just maybe have another look at the ZX examples as it is where your issue is I believe
the kotlin code looks correctly from glancing at it
h

Hele

06/25/2018, 3:20 PM
imo I dont have problem with the scan but maybe setting up the recycler view
as I am able to get the scan result of scan activity
Copy code
06-25 17:20:00.076 8652-8652/com.okan.pc.mylbrary.app I/System.out: books size 1
n

neil.armstrong

06/25/2018, 3:21 PM
yea, it’s because you aren’t inflating your own layout
you are just inflating the scanner layout
so the recycler view is never actually created unfortunately
h

Hele

06/25/2018, 3:22 PM
but am I not doing it here
Copy code
my_recycler_view?.layoutManager = LinearLayoutManager(this)
        my_recycler_view?.adapter = MainAdapter(booksMutable)
on
Copy code
onHandleResult
n

neil.armstrong

06/25/2018, 3:23 PM
that bit looks correct
it is
Copy code
public override fun onCreate(state: Bundle?) {
        super.onCreate(state)
        mScannerView = ZXingScannerView(this)   // Programmatically initialize the scanner view
        setContentView(mScannerView)                // Set the scanner view as the content view
    }
which is not correct, I think you need to handle the scanning in a separate activity
as your main activity is just showing the ZX layout, not your own xml layout that you have created (with the recycler view)
h

Hele

06/25/2018, 3:24 PM
it is already an activity itself
Copy code
button_scan.setOnClickListener {
            val intent = Intent(this, ScanActivity::class.java)
            startActivity(intent)

        }
this is how I open scanner activity
maybe I need to open my main activity with an intent
lthough I use a
Copy code
onBackPressed()
bcoz when I first open the app my list of mutableList shows up as 0 but after I scan and go back to main activity
it does not show size of that list
n

neil.armstrong

06/25/2018, 3:27 PM
Ah I see what you are doing, you need to move your recycler_view logic to the other activity, it cannot be in the
ScanActivity
as it doesn’t show the recycler view
h

Hele

06/25/2018, 3:28 PM
it is in main activity
is notit
that is where I show it
n

neil.armstrong

06/25/2018, 3:28 PM
Copy code
booksMutable.add(Book(rawResult.text.toLong()))
        val my_recycler_view = findViewById(R.id.my_recycler_view) as RecyclerView?
        my_recycler_view?.layoutManager = LinearLayoutManager(this)
        my_recycler_view?.adapter = MainAdapter(booksMutable)
        println("books size " + booksMutable.size)
this bit I mean, on your
ScanActivity
as it won’t work
h

Hele

06/25/2018, 3:28 PM
I also have that on
Copy code
onCreate
of my main activity
Copy code
setContentView(R.layout.activity_main)
Copy code
<RelativeLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
    xmlns:tools="<http://schemas.android.com/tools>"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

    <Button
        android:layout_marginTop="10dp"
        android:id="@+id/button_scan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/scan_book" />



</RelativeLayout>
n

neil.armstrong

06/25/2018, 3:30 PM
yes, but it doesn’t have the result. The flow will be 1. Start main activity 2. Do some action to start the scan activity via
startActivity
.
var activity = ScanActivity()
creates a different from one Android, so you cannot share data by making a mutable list like you are. 3. On scan successful, receive data in
handleResult
, then send this result data back to the main activity via
startActivity
and an intent. I recommend looking up the Android docs for how to do this
h

Hele

06/25/2018, 3:32 PM
I know how to pass data between activities
I am just new to kotlin
I have already made an app with recycler view in java but in kotlin it just does not work, this app is just for learning purposes 🙂
n

neil.armstrong

06/25/2018, 3:33 PM
from what I can see your approach won’t work in java either though, as it can not share data between the activities
the
my_recycler_view
is not accessible on the
ScanActivity
h

Hele

06/25/2018, 3:35 PM
yeah I get that part
now I am making an intent to share data to main activity
thanks for your help by the way
n

neil.armstrong

06/25/2018, 3:49 PM
no worries, best of luck 👍