我有RecyclerView
,我想在单击AlertDialog
的项时打开RecyclerView
,我试图遵循This java based Question的概念,但没有为我工作
我的适配器
class OperationAdapter (val context: Context,private val arrayList: ArrayList <Operations>):
RecyclerView.Adapter <OperationAdapter.Holder> () {
companion object {
val TAG: String = OperationAdapter::class.java.simpleName
}
override fun onCreateViewHolder (parent: ViewGroup, viewType: Int): Holder {
return Holder (LayoutInflater.from (parent.context ).inflate (R.layout.operaitemlist , parent, false))
}
override fun getItemCount (): Int = arrayList. size
override fun onBindViewHolder (holder: Holder, position: Int) {
val opera = arrayList[position]
holder.setData(opera, position)
}
inner class Holder (itemView: View): RecyclerView.ViewHolder (itemView) {
private var currentOpera: Operations? = null
private var currentPosition: Int = 0
init {
itemView.cardview.setOnClickListener {
currentOpera?.let {
AlertDialog.Builder(context)
.setTitle("My Title")
.setMessage("My Message")
.create()
.show()
}
}
//the end of the init
}
//getting data from Operations and bind it into View
fun setData(operation: Operations?, position: Int) {
operation?.let {
itemView.txtphonenumber.text = operation.phone
itemView.txttime.text = operation.etime
}
this.currentOpera = operation
this.currentPosition = position
}
}
设置回收视图
//set up the recycleview
mRecyclerView.setHasFixedSize (true)
mRecyclerView. layoutManager = LinearLayoutManager(this)
//adapter
val adapter = OperationAdapter(applicationContext,arrayList)
adapter.notifyDataSetChanged()
mRecyclerView.adapter = adapter
请提出任何建议
答案 0 :(得分:2)
Your code works我检查并复制了您的适配器的代码,它可以正常工作,向我发送您的xml布局和该适配器的初始化代码
更新:
您无法发送applicationContext,您应该发送活动的上下文。 解决此问题
val adapter = OperationAdapter(this, arrayList
)您不能在应用程序类中创建对话框,因为该对话框应附加到窗口,应用程序不是UI类且没有窗口,因此无法显示该对话框。
答案 1 :(得分:1)
首先,切勿将onclick放在onBindViewHolder中。那不是一个好习惯。其次,如果要对项目执行任何click事件,则可以转到界面,也可以将项目单击侦听器放在扩展RecyclerView.ViewHolder的ViewHolder类(内部类)中。
item.setOnClickListenr{
AlertDialog.Builder(this)
.setTitle("My Title")
.setMessage("My Message"))
.setPositiveButton("Yes") { dialog, which -> todoFunctiononpositiveclick() }
.setNegativeButton("No") { dialog, which -> dialog.dismiss() }
.show()
}
使用此链接可更好地了解使用接口https://android.jlelse.eu/click-listener-for-recyclerview-adapter-2d17a6f6f6c9与适配器进行交互
答案 2 :(得分:0)
您忘记了create()
#!/usr/local/bin/python
from elasticsearch import Elasticsearch
from elasticsearch import helpers
src = Elasticsearch(['localhost:9202'])
dst = Elasticsearch(['localhost:9200'])
body = {"query": { "match_all" : {}}}
source_index='src-index'
target_index='dst-index'
scroll_time='60s'
batch_size='500'
def transform(hits):
for h in hits:
h['_index'] = target_index
yield h
rs = src.search(index=[source_index],
scroll=scroll_time,
size=batch_size,
body=body
)
helpers.bulk(dst, transform(rs['hits']['hits']), chunk_size=batch_size)
while True:
scroll_id = rs['_scroll_id']
rs = src.scroll(scroll_id=scroll_id, scroll=scroll_time)
if len(rs['hits']['hits']) > 0:
helpers.bulk(dst, transform(rs['hits']['hits']), chunk_size=batch_size)
else:
break;
希望有帮助