2017-09-02 66 views
1

代碼示例:如何從Kotlin中的ListView中獲取所選項目?

package tech.kapoor.listviewdemo 

import android.content.Context 
import android.graphics.Color 
import android.support.v7.app.AppCompatActivity 
import android.os.Bundle 
import android.view.View 
import android.view.ViewGroup 
import android.widget.BaseAdapter 
import android.widget.ListView 
import android.widget.TextView 
import android.widget.AdapterView 


class MainActivity : AppCompatActivity() { 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     setContentView(R.layout.activity_main) 

     val listView = findViewById<ListView>(R.id.main_listview) 
     var redColor = Color.parseColor("#FF0000") 

     listView.setBackgroundColor(redColor) 
     listView.adapter = CustomAdapter(this) 
    } 

    private class CustomAdapter(context: Context): BaseAdapter() { 

     private val mContext: Context 

     init { 
      mContext = context 
     } 

     override fun getCount(): Int { 
      return 80 
     } 

     override fun getItemId(position: Int): Long { 
      return position.toLong() 
     } 

     override fun getItem(position: Int): Any { 
      return position 
     } 

     override fun getView(position: Int, view: View?, viewGroup: ViewGroup?): View { 
      val textView = TextView(mContext) 
      textView.text = "Here comes the !!" 
      return textView 
     } 
    } 
} 

我試圖瞭解列表視圖,而不是回收器視圖首先要了解基本知識。 任何人都知道我們如何得到選擇或onclick選定的行ID /索引值,以及如何執行一些操作選擇kotlin中的特定行?

回答

1

要填充列表視圖,您必須具有數據集。數據集可以是任何數據類型(如字符串)的任何列表,也可以使用模型類的列表。事情是這樣的:

這是我簡單的數據集,我將在ListView中使用的列表:

val data = ArrayList<TopicDTO>() 
data.add(TopicDTO("1", "Info 1", true)) 
data.add(TopicDTO("2", "Info 2", false)) 
data.add(TopicDTO("3", "Info 3", true)) 
data.add(TopicDTO("4", "Info 4", false)) 

我已經創建了一個名爲TopicDTO一個模型類,其中包含ID,標題,它的地位。

現在,讓我們填充到ListView的這個:

list.adapter = ButtonListAdapter(baseContext, data) 

下面是一個簡單的適配器:

class ButtonListAdapter(//Class for rendering each ListItem 

     private val context: Context, private val rowItems: List<TopicDTO>) : BaseAdapter() { 

    override fun getCount(): Int { 
     return rowItems.size 
    } 

    override fun getItem(position: Int): Any { 
     return rowItems[position] 
    } 

    override fun getItemId(position: Int): Long { 
     return rowItems.indexOf(getItem(position)).toLong() 
    } 


    private inner class ViewHolder { 
     internal var main_text: TextView? = null //Display Name 
     internal var subtitle: TextView? = null //Display Description 
     internal var can_view_you_online: Button? = null //Button to set and display status of CanViewYouOnline flag of the class 

    } 

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 
     var convertView = convertView 
     var holder: ViewHolder? = null 


     val mInflater = context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE) as LayoutInflater 

     holder = ViewHolder() 


     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.main_lp_view_item, null) 

      holder.main_text = convertView!!.findViewById(R.id.main_lp_text) as TextView 
      holder.subtitle = convertView.findViewById(R.id.main_lp_subtitle) as TextView 
      holder.can_view_you_online = convertView.findViewById(R.id.can_view_you_online) as Button 

      convertView.tag = holder 

     } else { 
      holder = convertView.tag as ViewHolder 
     } 

     val rowItem = rowItems[position] 

     val main_text: String 
     val subtitle: String 


     holder.main_text!!.text = rowItem.info 
     holder.subtitle!!.text = rowItem.info 

     if (rowItem.canViewYouOnline) { 
      holder.can_view_you_online!!.setBackgroundColor(context.resources.getColor(R.color.colorPrimary)) 
     } else { 
      holder.can_view_you_online!!.setBackgroundColor(context.resources.getColor(R.color.colorAccent)) 
     } 


     holder.can_view_you_online!!.setOnClickListener(object : View.OnClickListener { 
      internal var buttonClickFlag: Boolean = false 


      override fun onClick(v: View) {   //The Onclick function allows one to click the button on the list item and set/reset the canViewYouOnline flag. It is working fine. 

      } 
     }) 


     return convertView 

    } 


} 

現在,你可以得到你所選擇的項目是這樣的:

list.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id -> 
      // This is your listview's selected item 
      val item = parent.getItemAtPosition(position) as TopicDTO 
     } 

希望你瞭解這一點。

+0

我來自iOS開發平臺。希望你不介意我問,如何使它在附加的代碼中工作? –

+0

首先指定您的要求。你基本上想要什麼?所選項目的ArrayList或只是一個點擊項目? – AndiM

+0

只是在問題中提到的點擊項目。 –

1

您可以將getView()方法類似內部使用:

view.setOnClickListener(object : View.OnClickListener { 
    override fun onClick(v: View?) { 
     //use getItem(position) to get the item 
    } 
}) 

或使用拉姆達:

view.setOnClickListener({ v -> //use theItem(position) }) 

只是一個提示:

我試圖瞭解名單而不是回收商視圖來首先理解基本知識。

在我看來,在您的項目中,您將在99%的案例中使用RecyclerView

+0

這就是Java代碼。 –

+0

@AshishKapoor你是對的。 –

+0

爲此創建了一個Github存儲庫。你可以顯示與拉請求相同嗎? https://github.com/AshishKapoor/ListViewDemo –

相關問題