Advertisement
Guest User

cds

a guest
Jun 25th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.71 KB | None | 0 0
  1.  
  2. class MainAdapter(val bookList: MutableList<Book>) : RecyclerView.Adapter<CustomViewHolder>() {
  3.  
  4.     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
  5.         val layoutInflater = LayoutInflater.from(parent.context).inflate(R.layout.recycler_view_row, parent, false)
  6.         return CustomViewHolder(layoutInflater)
  7.     }
  8.  
  9.  
  10.     override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
  11.  
  12.         holder.bindItems(bookList[position])
  13.       /*  holder.itemView.text_isbn.text = bookList[position].barcode.toString()*/
  14.         notifyDataSetChanged()
  15.  
  16.     }
  17.  
  18.  
  19.  
  20.     override fun getItemCount(): Int {
  21.         return bookList.size
  22.     }
  23.  
  24. }
  25.  
  26.  
  27. class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  28.  
  29.     fun bindItems(book: Book) {
  30.  
  31.         itemView.text_isbn.text = book.barcode.toString()
  32.     }
  33.  
  34. }
  35.  
  36.  
  37. //////////////
  38.  
  39. override fun onCreate(savedInstanceState: Bundle?) {
  40.         super.onCreate(savedInstanceState)
  41.         setContentView(R.layout.activity_main)
  42.         validatePermission()
  43.  
  44.         var scanActivity = ScanActivity()
  45.  
  46.         my_recycler_view.layoutManager = LinearLayoutManager(this)
  47.         my_recycler_view.adapter = MainAdapter(scanActivity.booksMutable)
  48.  
  49.         button_scan.setOnClickListener {
  50.             val intent = Intent(this, ScanActivity::class.java)
  51.             startActivity(intent)
  52.  
  53.         }
  54.  
  55.     }
  56. /////////////////////
  57. lass ScanActivity : AppCompatActivity(), ZXingScannerView.ResultHandler {
  58.  
  59.     private var mScannerView: ZXingScannerView? = null
  60.     var barcode: Long = 0
  61.     //var books = ArrayList<Book>()
  62.     var booksMutable = mutableListOf<Book>()
  63.  
  64.     public override fun onCreate(state: Bundle?) {
  65.         super.onCreate(state)
  66.         mScannerView = ZXingScannerView(this)   // Programmatically initialize the scanner view
  67.         setContentView(mScannerView)                // Set the scanner view as the content view
  68.     }
  69.  
  70.     public override fun onResume() {
  71.         super.onResume()
  72.         mScannerView!!.setResultHandler(this) // Register ourselves as a handler for scan results.
  73.         mScannerView!!.startCamera()          // Start camera on resume
  74.     }
  75.  
  76.     public override fun onPause() {
  77.         super.onPause()
  78.         mScannerView!!.stopCamera()           // Stop camera on pause
  79.     }
  80.  
  81.     override fun handleResult(rawResult: Result) {
  82.         Toast.makeText(this,rawResult.text,Toast.LENGTH_SHORT).show()
  83.         booksMutable.add(Book(rawResult.text.toLong()))
  84.         val my_recycler_view = findViewById(R.id.my_recycler_view) as RecyclerView?
  85.         my_recycler_view?.layoutManager = LinearLayoutManager(this)
  86.         my_recycler_view?.adapter = MainAdapter(booksMutable)
  87.         println("books size " + booksMutable.size)
  88.  
  89.         onBackPressed()
  90.  
  91.         fetchJson()
  92.  
  93.         // If you would like to resume scanning, call this method below:
  94.         //mScannerView.resumeCameraPreview(this);
  95.  
  96.  
  97.     }
  98.  
  99.     fun fetchJson(){
  100.         val url = "http://xisbn.worldcat.org/webservices/xid/isbn/9780571276646?method=getMetadata&format=json&fl=*&callback=mymethod"
  101.  
  102.         val request =Request.Builder().url(url).build()
  103.  
  104.         val client = OkHttpClient()
  105.  
  106.          client.newCall(request).enqueue(object: Callback{
  107.              override fun onFailure(call: Call?, e: IOException?) {
  108.                  println("Error in executing call")
  109.              }
  110.  
  111.              override fun onResponse(call: Call?, response: Response?) {
  112.                  val body = response?.body()?.toString()
  113.                //  val Jobject = JSONObject(body)
  114.                  println("BODY" + body.toString())
  115.  
  116.              }
  117.  
  118.          })
  119.  
  120.         println()
  121.  
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement