【发布时间】:2020-05-14 06:50:27
【问题描述】:
此应用程序使用 sharedPreferences 来填充 MainActivity.kt 中的回收器查看器。然后我想在每个回收器项目中有两个按钮,以便有一个事件侦听器将转到另一个活动。但是我一直无法这样做,试图操纵适配器和 MainActivity.kt。
MainActivity.kt:
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
populateRecycler()
//Pressing GPS icon will go to adding a new location
fabAddLocation.setOnClickListener { view -> addLocation(view) }
}
fun addLocation(x:View?){
val locationIntent: Intent = Intent(this,AddLocation::class.java)
startActivity(locationIntent)
}
fun createRecyclerContent(list :Array<String>): ArrayList<LocationItem> {
//Get the shared preference file data
var size = list.size
val itemList = ArrayList<LocationItem>()
for (i in 0 until size){
val name = list[i].substring(0, list[i].length -4)
lateinit var prefs:SharedPreferences
prefs = getSharedPreferences(name,Context.MODE_PRIVATE)
val getTitle = prefs.getString("Title","")
val getDescription = prefs.getString("Description","")
val getGps = prefs.getString("Longitude","")+","+prefs.getString("Latitude","")
val listItem = LocationItem(R.drawable.ic_location_icon,getTitle.toString(),getDescription.toString(),getGps)
itemList += listItem
}
return itemList
}
fun populateRecycler(){
val sharedPrefsDir = File(applicationInfo.dataDir, "shared_prefs")
if(sharedPrefsDir.exists() && sharedPrefsDir.isDirectory()){
//verifying that the directory is found and that it has the names
val locateList = sharedPrefsDir.list();
//This should be sending the number of recycler items to my createRecyclerContent function, to populate the recycler
val locationsList = createRecyclerContent(locateList)
recycler_view.adapter=LocationsAdapter(locationsList)
(recycler_view.adapter as LocationsAdapter).notifyDataSetChanged()
recycler_view.layoutManager= LinearLayoutManager(this)
recycler_view.setHasFixedSize(true)
}
}
适配器(LocationsAdapter):
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.saved_location_layout.view.*
class LocationsAdapter(private val locationList:List<LocationItem>):RecyclerView.Adapter<LocationsAdapter.LocationViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocationViewHolder {
//passes the layout to the recycler
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.saved_location_layout, parent, false)
return LocationViewHolder(itemView)
}
override fun onBindViewHolder(holder: LocationViewHolder, position: Int) {
val currentItem = locationList[position]
holder.imageButton.setImageResource(currentItem.image)
holder.textView1.text = currentItem.text1
holder.textView2.text = currentItem.text2
holder.textView3.text = currentItem.text3
}
//sets the count to the number of locations in the list
override fun getItemCount() = locationList.size
class LocationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val imageButton: ImageButton = itemView.btn_location
val textView1: TextView = itemView.text_view_1
val textView2: TextView = itemView.text_view_2
val textView3: TextView = itemView.text_view_3
val editButton:Button = itemView.btn_edit
}
}
有两个按钮,btn_edit 和 btn_location,我需要每个按钮去另一个活动。非常感谢任何有关如何做到这一点的帮助。
【问题讨论】:
标签: android kotlin android-recyclerview