I am designing an application in Kotlin which parses json-array.
I have completed the application successfully but I was stuck on one minor(in my case major) glitch.I have successfully parsed the array and displayed the required array of objects in CardView
and also opened the link in the browser when the card item is clicked.
Problem
The JSON contains certain links which are expired or are no longer in use. How do I handle such(expired) links from within the app when the user clicks on that particular card(with any flowery message or `ImageView)? I am comfortable in Android basics and still learning to devise Kotlin in android application and searched on the internet to the best of my knowledge but all my efforts went in vein.
Any help is warmly and benignly welcomed....
Below is my Code fragment(FeedViewHolder.kt):
class FeedViewHolder(itemView: View):RecyclerView.ViewHolder(itemView), View.OnClickListener,View.OnLongClickListener {
var txtTitle: TextView
var txtPubdate: TextView
var txtContent: TextView
private var itemClickListener: ItemClickListener? = null
init {
txtTitle = itemView.findViewById(R.id.txtTitle) as TextView
txtPubdate = itemView.findViewById(R.id.txtPubdate) as TextView
txtContent = itemView.findViewById(R.id.txtContent) as TextView
itemView.setOnClickListener(this)
itemView.setOnLongClickListener(this)
}
fun setItemClickListener(itemClickListener: ItemClickListener) {
this.itemClickListener = itemClickListener
}
override fun onClick(v: View?) {
itemClickListener!!.onClick(v, adapterPosition, false)
}
override fun onLongClick(v: View?): Boolean {
itemClickListener!!.onClick(v, adapterPosition, true)
return true
}
}
Adapter Class
class FeedAdapter(private val rssObject: RSSObject, private val
mContext:Context): RecyclerView.Adapter<FeedViewHolder>()
{
private val inflater:LayoutInflater
init {
inflater = LayoutInflater.from(mContext)
}
override fun getItemCount(): Int {
return rssObject.items.size
}
override fun onBindViewHolder(holder: FeedViewHolder, position: Int) {
holder.txtTitle.text = rssObject.items[position].title
holder.txtContent.text = rssObject.items[position].Content
holder.txtPubdate.text = rssObject.items[position].pubDate
holder.setItemClickListener(ItemClickListener { view, position, isLongClick ->
if(!isLongClick){
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(rssObject.items[position].link))
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
mContext.startActivity(browserIntent)
}
})
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): FeedViewHolder {
val itemView = inflater.inflate(R.layout.row,parent,false)
return FeedViewHolder(itemView)
}