我正在尝试获取具有深层链接的类别的帖子列表,但它返回Unresolved reference
Documents
my code
Errors
class CategoryLinkDetails : Fragment() {
private lateinit var mInterstitialAd: InterstitialAd
private lateinit var homeViewModel: HomeViewModel
private var recyclerView: RecyclerView? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreate(savedInstanceState)
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.categorylink_details, container, false)
recyclerView = root.findViewById(R.id.categorylinkRecycle)
handleIntent(intent)
return root
}
// api code
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIntent(intent)
}
private fun handleIntent(intent: Intent) {
val appLinkAction = intent.action
val appLinkData: Uri? = intent.data
if (Intent.ACTION_VIEW == appLinkAction) {
appLinkData?.lastPathSegment?.also { recipeId ->
Uri.parse("content://com.my.app/categories/")
.buildUpon()
.appendPath(recipeId)
.build().also { appData ->
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(activity)
val url = "https://example.com/v1/categories/$recipeId"
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
val jsonArray = JSONArray(response)
val list: ArrayList<MyCategoryLink> = ArrayList()
for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)
list.add(parseData(jsonObject))
}
// here you will have the complete list of data in your "list" variable
categoriesRecycle.layoutManager = LinearLayoutManager(activity)
Log.d("my list", list.toString())
categoriesRecycle.adapter = MyCategoryListAdapter(list)
},
Response.ErrorListener { error ->
//displaying the error in toast if occurrs
Toast.makeText(activity, error.message, Toast.LENGTH_SHORT)
.show()
})
// Add the request to the RequestQueue.
queue.add(stringRequest)
}
}
}
}
private fun parseData(jsonObject: JSONObject): MyCategoryLink {
var listingObject = MyCategoryLink(
jsonObject.getString("name"),
jsonObject.getString("slug"),
HtmlCompat.fromHtml(jsonObject.getString("body"), HtmlCompat.FROM_HTML_MODE_COMPACT),
jsonObject.getString("image")
)
return listingObject
}
}
注意:
我的代码和文档之间的唯一区别是,我使用
Fragment
,因为我需要显示帖子列表,但是在文档中他们使用了Activity
任何想法?
您需要在活动类中处理意图数据,然后需要将意图传递给片段构造函数请参阅下面的代码。
class TestActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleIntent(intent)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
handleIntent(intent)
}
private fun handleIntent(intent: Intent?) {
// now from here you can add your fragment here
var categoryDetails = CategoryDetailsFragment(intent)
var ft = supportFragmentManager.beginTransaction()
ft.add(R.id.container,categoryDetails)
}}
我希望这会有所帮助。