Last active
December 19, 2018 03:03
-
-
Save henriquehorbovyi/e87c6ee53d531b9593ec20e315d2e2b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestActivity : AppCompatActivity() { | |
// you can use 'by lazy' in this case, and volley object will be instantiated only when needed | |
private val volleyRequest: RequestQueue by lazy { Volley.newRequestQueue(this) } | |
private val testList: MutableList<TestModel> = mutableListOf() | |
// you don't need to pass parameters, just need to change a bit the implementation of your Adapter class. | |
private val adapter: TestListAdapter = TestListAdapter(this) | |
// don't need to create this variable if you are not using more than 1 time | |
//private var layoutManager: RecyclerView.LayoutManager?=null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_Test) | |
setupRecyclerView() | |
val HelloLink = "https://www.abc.app" | |
getTest(HelloLink) | |
} | |
private fun setupRecyclerView(){ | |
recyclerViewTest.layoutManager = LinearLayoutManager(this) | |
recyclerViewTest.adapter = adapter | |
} | |
fun getTest(url: String) { | |
print("Url Value is:"+url) | |
val catRequest = JsonArrayRequest(Request.Method.GET, url, Response.Listener { | |
response: JSONArray -> | |
try { | |
for (i in 0..response.length() - 1) { | |
val catObj = response.getJSONObject(i) | |
val catName = catObj.getString("name") | |
val test = TestModel(name = catName) | |
testList.add(test) | |
Log.d("Name Result==>>", name) | |
} | |
// call the method update passing the list | |
adapter.update(testList) | |
}catch (e: JSONException) { e.printStackTrace()} | |
}, | |
Response.ErrorListener { | |
error: VolleyError? -> | |
try { | |
Log.d("Error:", error.toString()) | |
}catch (e: JSONException){ e.printStackTrace() } | |
}) | |
volleyRequest.add(catRequest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment