Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.top10
- import android.content.Context
- import android.os.AsyncTask
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.os.PersistableBundle
- import android.util.Log
- import android.view.*
- import android.widget.ArrayAdapter
- import android.widget.ListView
- import android.widget.TextView
- import kotlinx.android.synthetic.main.activity_main.*
- import org.xmlpull.v1.XmlPullParser
- import org.xmlpull.v1.XmlPullParserFactory
- import java.lang.Exception
- import java.net.URL
- import kotlin.properties.Delegates
- private const val TAG = "MainActivity"
- class ResultEntry() {
- var title: String = ""
- var description: String = ""
- var link: String = ""
- }
- private const val BREAKING_NEWS = "https://www.nasa.gov/rss/dyn/breaking_news.rss"
- private const val EDUCATION_NEWS = "https://www.nasa.gov/rss/dyn/educationnews.rss"
- private const val SOLAR_SYSTEM_NEWS = "https://www.nasa.gov/rss/dyn/solar_system.rss"
- private const val STATE_URL = ""
- class MainActivity : AppCompatActivity() {
- private var url = BREAKING_NEWS
- private var downloadData: DownloadData? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- Log.d(TAG,"onCreate called!")
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- if(savedInstanceState != null) {
- url = savedInstanceState.getString(STATE_URL, url)
- }
- downloadURL(url)
- }
- override fun onDestroy() {
- Log.d(TAG,"onDestroy called!")
- super.onDestroy()
- }
- override fun onRestoreInstanceState(savedInstanceState: Bundle) {
- Log.d(TAG,"onRestoreInstanceState called!")
- super.onRestoreInstanceState(savedInstanceState)
- url = savedInstanceState.getString(STATE_URL, "")
- Log.d(TAG, url)
- downloadURL(url)
- }
- override fun onStart() {
- Log.d(TAG,"onStart called!")
- super.onStart()
- }
- override fun onResume() {
- Log.d(TAG,"onResume called!")
- super.onResume()
- }
- override fun onPause() {
- Log.d(TAG,"onPause called!")
- super.onPause()
- }
- override fun onStop() {
- Log.d(TAG,"onStop called!")
- super.onStop()
- }
- override fun onRestart() {
- Log.d(TAG,"onRestart called!")
- super.onRestart()
- }
- override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
- Log.d(TAG,"onSaveInstanceState called!")
- super.onSaveInstanceState(outState, outPersistentState)
- outState.putString(STATE_URL, url)
- }
- override fun onCreateOptionsMenu(menu: Menu?): Boolean {
- menuInflater.inflate(R.menu.top_menu, menu)
- return true
- }
- private fun downloadURL(url:String) {
- downloadData = DownloadData(this, xmlListView)
- downloadData?.execute(url)
- }
- override fun onOptionsItemSelected(item: MenuItem): Boolean {
- this.url = when(item.itemId) {
- R.id.breakingNews ->
- BREAKING_NEWS
- R.id.educationNews ->
- EDUCATION_NEWS
- R.id.solarSystemNews ->
- SOLAR_SYSTEM_NEWS
- else ->
- return super.onOptionsItemSelected(item)
- }
- downloadURL(url)
- return true
- }
- }
- class DownloadData(context: Context,listView: ListView) : AsyncTask<String, Void, String>() { // handles the downloading of xml data. Extends AsyncTask class. Enables data to be fetched asynchronously
- private val TAG = "DownloadData"
- private var propContext: Context by Delegates.notNull()
- private var propListView: ListView by Delegates.notNull()
- //
- init {
- propContext = context
- propListView = listView
- }
- override fun doInBackground(vararg url: String?): String {
- val rssFeed = URL(url[0]).readText()
- if (rssFeed.isEmpty()) {
- Log.e(TAG, "doInBackground: Cant download XML")
- }
- return rssFeed
- }
- override fun onPostExecute(result: String?) {
- super.onPostExecute(result)
- Log.d(TAG, "onPostExecute: parameter is $result")
- val parseResults = ParseResults()
- if (result != null) {
- parseResults.parse(result)
- }
- val feedAdapter = FeedAdapter(propContext, R.layout.list_record, parseResults.results)
- propListView.adapter = feedAdapter
- }
- }
- class ParseResults() {
- val results = ArrayList<ResultEntry>()
- // takes the xml data string and converts it i to data
- fun parse(xmlData: String): Boolean {
- var status = true
- var inEntry = false
- var textValue = ""
- try {
- val factory = XmlPullParserFactory.newInstance()
- factory.isNamespaceAware = true
- val xpp = factory.newPullParser()
- xpp.setInput(xmlData.reader())
- var eventType = xpp.eventType
- var currentRecord = ResultEntry()
- while (eventType != XmlPullParser.END_DOCUMENT) {
- val tagName = xpp.name?.toLowerCase()
- when (eventType) {
- XmlPullParser.START_TAG -> {
- if (tagName == "item") {
- inEntry = true
- }
- }
- XmlPullParser.TEXT -> textValue = xpp.text
- XmlPullParser.END_TAG -> {
- if (inEntry) {
- when (tagName) {
- "item" -> {
- results.add(currentRecord)
- inEntry = false
- currentRecord = ResultEntry()
- }
- "title" -> currentRecord.title = textValue
- "description" -> currentRecord.description = textValue
- "link" -> currentRecord.link = textValue
- }
- }
- }
- }
- eventType = xpp.next()
- }
- } catch (e: Exception) {
- status = false
- }
- return status
- }
- }
- class ViewHolder(v: View) {
- val title: TextView = v.findViewById(R.id.title)
- val description: TextView = v.findViewById(R.id.description)
- val link: TextView = v.findViewById(R.id.link)
- }
- class FeedAdapter(context: Context, private val resource : Int, private val results:
- List<ResultEntry>): ArrayAdapter<ResultEntry>(context,resource) {
- private val inflater = LayoutInflater.from(context)
- override fun getCount(): Int {
- return results.size
- }
- override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
- val view:View
- val viewHolder:ViewHolder
- if(convertView == null) {
- view = inflater.inflate(resource,parent,false)
- viewHolder = ViewHolder(view)
- view.tag = viewHolder
- }else{
- // reused the view
- view = convertView
- viewHolder = view.tag as ViewHolder
- }
- val currentApp = results[position]
- viewHolder.title.text = currentApp.title
- viewHolder.description.text = currentApp.description
- viewHolder.link.text = currentApp.link
- return view
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement