2017-10-10 115 views
0

所以我知道這是代碼升沉,但我有問題讓我的RecyclerView在一個片段中整理出來。我讀過一大堆教程,每個人似乎都有不同的做事方式。只是沒能敲定下來......因此,這裏是我的代碼:我似乎無法得到我的Kotlin Recycler多視圖代碼

片段XML

<FrameLayout 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="com.example.xxxx.listview.CalendarFragment">  

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/event_recycle" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="horizontal" /> 
</FrameLayout> 

數據類

data class SectionOrRow(var needTicketing:String?,var eventSponsor:String?,var eventED:String?,var eventSD:String?,var eventTitle:String?,var mysectionName:String?,var isRow:Boolean){ 

    companion object { 

     public fun createRow(row: String): Unit { 
      // val ret = SectionOrRow() 
     // return ret 
     } 

     public fun createSection(section: String): Unit { 
      // val ret = SectionOrRow(null,section,false) 
      // return ret 
     } 
    } 
} 

片段CLASS

package com.example.XXX.listview 




class CalendarFragment : Fragment() { 
    lateinit var eventInfo: JsonArray<JsonObject> 
    lateinit var event2Info: JsonArray<JsonObject> 
    val eventList0 = ArrayList<SectionOrRow>()  
    val TAG : String= "CalendarFragment" 

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, 
           savedInstanceState: Bundle?): View? { 

     val view : View = inflater.inflate(R.layout.fragment_calendar,container,false) 
     (activity as MainActivity).getSupportActionBar()?.setTitle("Medfield Calendar") 
     var event_recycle:RecyclerView = view.findViewById(R.id.event_recycle) 


     var myUrl:String = "https://www.nosite.com/functions/API/eventsJSON2.php?zip_code=02052" 
     myUrl.httpGet().responseString { request, response, result -> 
      //do something with response 
      when (result){ 
       is Result.Failure -> { 
        Log.d("Towns",result.toString()) 
        val error = result.getAs<String>() 
        Log.d("Towns error ",error) 
       } 
       is Result.Success -> { 

        val data = result.getAs<String>() 
        val events : Parser = Parser() 
        val eventBuilder: StringBuilder = StringBuilder(data) 
        val eventsjson = events.parse(eventBuilder) as JsonObject 

        Log.d("JOO",eventsjson.toString()) 

        var eventList0 = ArrayList<SectionOrRow>() 


        eventInfo = eventsjson.array("events")!! 

        Log.d("length array",this.eventInfo.size.toString()) 
        if(this.eventInfo.size<2){ 
         Toast.makeText(context,"There are no categories for this town...", Toast.LENGTH_LONG).show() 
        }else{ 

         Log.d("type",this.eventInfo.javaClass.name) // kotlin.Double 
         for (i in eventInfo.indices){ 



          //Log.d("Loops"+i.toString(),eventInfo[i].string("category")) 
          //Log.d("Loops"+i.toString(),eventInfo[i].string("events2")) 
          var theCat = eventInfo[i].string("category") 
          eventList0.add(SectionOrRow(null,null,null,null,null,theCat,false)) 


          var theEvents:JsonArray<JsonObject>? = eventInfo[i].array("events2") 
          Log.d("MSG1","IS category header"+theCat) 
          theEvents?.let{ 
           for(x in theEvents.indices){ 
            val needT: String? = theEvents[x].string("news_needsticketing") 
            val eventSpon: String? = theEvents[x].string("news_sponsor") 
            val startD: String? = theEvents[x].string("news_start_date") 
            val endD: String? = theEvents[x].string("news_expiration") 
            val newsT: String? = theEvents[x].string("news_tile") 

            eventList0.add(SectionOrRow(needT,eventSpon,endD,startD,newsT,theCat,true)) 
           } 
           Log.d("MSG2","Are Events") 
          } 

          //eventList0.add(SectionOrRow.createRow("City")) 
         } 


        } 

        event_recycle.layoutManager = LinearLayoutManager(context) 
        event_recycle.hasFixedSize() 
        event_recycle.adapter = MyEventAdapter(context,eventList0) 
        Log.d("All Size First",eventList0.size.toString()) 



        //Log.d("FN",nextOne.toString()) 
        // myview.textView3.setText(Html.fromHtml(ownerMessage,Html.FROM_HTML_MODE_LEGACY)) 
        //myview.textView3.setText(myCS) 

       } 
      } 
     } 

     // Inflate the layout for this fragment 
     return view 

    } 

    inner class MyEventAdapter(context: Context,val theEvents:ArrayList<SectionOrRow>):RecyclerView.Adapter<RecyclerView.ViewHolder>(){ 

     override fun getItemViewType(position: Int): Int { 
      if(theEvents[position].isRow){ 
       return 0 
      }else{ 
       return 1 
      } 


     } 

     override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { 
      val item = theEvents.get(position) 

      if(item.isRow){ 
       val h = holder as RowViewHolder 
       h.textView.setText(item.mysectionName) 
      }else{ 
       val h = holder as SectionViewHolder 
       h.textView.setText(item.eventSD) 
      } 

     } 



     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder? { 

      if(viewType==0) { 
       val v = LayoutInflater.from(parent.context).inflate(R.layout.calendar_row, parent, false) 
       return RowViewHolder(v) 
      } 

      if(viewType==1){ 
       val v = LayoutInflater.from(parent.context).inflate(R.layout.calendar_section, parent, false) 
       return SectionViewHolder(v) 
      } 
      return null 
     } 

     override fun getItemCount(): Int { 
       Log.d("All Size",theEvents.size.toString()) 
       return theEvents.size 
     } 


     inner class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 
      val textView: TextView 

      init { 
       textView = itemView.findViewById<View>(R.id.sectionHead) as TextView 
      } 
     } 

     inner class SectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 
      val textView: TextView 

      init { 
       textView = itemView.findViewById<View>(R.id.dateOfEvent) as TextView 
      } 
     } 

    } 

} 

錯誤,我得到它:

10-10 09:12:49.239 32612-32612/com.example.xxxx.listview E/RecyclerView:沒有適配器連接;跳過佈局

因此,看來我無法將適配器連接到回收站視圖。肯定還有其他的錯誤,但是這是一個問題;)

SOLUTION

inner class MyEventAdapter(context: Context,val theEvents:ArrayList<SectionOrRow>):RecyclerView.Adapter<RecyclerView.ViewHolder>(){ 

     override fun getItemViewType(position: Int): Int { 
      Log.d("Current Boolean",theEvents[position].isRow.toString()) 

      if(theEvents[position].isRow){ 
       //true is row 
       return 1 
      }else{ 
       return 0 
      } 


     } 

     override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { 
      val item = theEvents.get(position) 
      Log.d("Current Item",item.toString()) 

      if(item.isRow){ 
       val h = holder as RowViewHolder 

       val s = java.text.SimpleDateFormat("EEE\nMM/dd") 
       val format = s.format(Integer.parseInt(item.eventSD) * 1000L) 

       h.textView.setText(format) 
       Log.d("Current Title",item.eventTitle.toString()) 

       h.titleView.setText(item.eventTitle) 
       h.datesView.setText(item.eventED) 
       h.sponsorView.setText(item.eventSponsor) 
       if(item.needTicketing=="Y") { 
        h.ticketView.setText("Ticketing Available") 
       }else{ 
        h.ticketView.setText("") 

       } 
      }else{ 
       val h = holder as SectionViewHolder 
       h.textView.setText(item.mysectionName) 
      } 

     } 



     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder? { 

      if(viewType==1) { 
       val v = LayoutInflater.from(parent.context).inflate(R.layout.calendar_row, parent, false) 

       v.setOnClickListener(object: View.OnClickListener { 
        override fun onClick(p0: View?) { 
         Toast.makeText(
           v.context, 
           "Please Click Set Town to Return to Previous Screen....", 
           Toast.LENGTH_SHORT 
         ).show() 

        } 
       }) 
       return RowViewHolder(v) 
      } 

      if(viewType==0){ 
       val v = LayoutInflater.from(parent.context).inflate(R.layout.calendar_section, parent, false) 
       return SectionViewHolder(v) 
      } 
      return null 
     } 

     override fun getItemCount(): Int { 
       Log.d("All Size",theEvents.size.toString()) 
       return theEvents.size 
     } 


     inner class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 
      var textView: TextView 
      var titleView: TextView 
      var datesView: TextView 
      var sponsorView: TextView 
      var ticketView: TextView 

      init { 
       textView = itemView.findViewById<View>(R.id.dateOfEvent) as TextView 
       titleView = itemView.findViewById<View>(R.id.rowTitle) as TextView 
       datesView = itemView.findViewById<View>(R.id.rowDates) as TextView 
       sponsorView = itemView.findViewById<View>(R.id.rowSponsor) as TextView 
       ticketView = itemView.findViewById<View>(R.id.rowTicketing) as TextView 

      } 
     } 

     inner class SectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 
      var textView: TextView 
      init { 
       textView = itemView.findViewById<View>(R.id.sectionHead) as TextView 
      } 
     } 



    } 

回答

1

撥打override fun onViewCreatedRecyclerViewAdapter類的Fragment

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { 
    super.onViewCreated(view, savedInstanceState) 
    val recyclerView=view?.findViewById(R.id.your_recycler_view) as RecyclerView 
    recyclerView.layoutManager=LinearLayoutManager(context, LinearLayout.VERTICAL,false) 
    var yourData=ArrayList<YourDataClass>() 
    yourData.add(YourDataClass("passYourData")) 


    val yourAdapterClassVariableName=YourRecyclerViewAdapterClass(yourData) 
    recyclerView.adapter=yourAdapterClassVariableName 
} 
+0

是不是什麼 event_recycle.adapter = MyEventAdapter(context,eventList0) 是說.. – BostonMacOSX

0

你需要set your adapter in the UI thread。首先初始化適配器,然後將數據添加到它並調用adapter.notifyDataSetChanged()。

我也會引導你到kotlin extensions(導入綜合屬性部分),所以你不必再調用那個混亂的findViewById調用。

0

請看這個問題的解決方案的頂部...是一個正確的類型進入正確的行問題。

相關問題