首先让我谢谢大家
我已经使用JDBC创建了到MS sqlserver数据库的连接,并且一切都很好,使用Groupie库可以很容易地将数据处理到recyclerview,但是当我使用onitemclicklistener时,在其他活动中以及当它会收到整个数组而不是单击的项目吗?
情况是:我将数据接收到Rampdata()内的recyclerview中,然后当我单击recyclerview中的任何项目时,它应该带我进入Tripdetails()并提供项目详细信息,以便我可以在那里重复使用数据>
注意:我没有包括JDBC连接代码。
活动一
class RampArea : AppCompatActivity() {
companion object{
val flightkey:String = "flight"
}
lateinit var rv: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ramp_area)
val db = databasehelper()
rv = findViewById(R.id.flightsrecyclerview)
rv.layoutManager = LinearLayoutManager(applicationContext,LinearLayoutManager.VERTICAL,false)
db.ctx = this
db.gettrips(rv)
db.gadapter2.setOnItemClickListener { item, view ->
val uit = item as flightitem2
val intent = Intent(this, TripDetails::class.java)
intent.putExtra(flightkey,uit.flights)
startActivity(intent)
}
活动二
class TripDetails : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_trip_details)
val flightdata1 = intent.getParcelableExtra<flighttrip>("ff")
Log.d("the data in the intento","" + flightdata1)
}
}
数据类
@Parcelize
class flighttrip(var fltnum:String ,var trcom:String,var aircraft:String ,var date:String,var atd:String, var ata:String, var pax:String,var fltfrom:String,var fltto:String ):Parcelable{
constructor():this("","","","","","","","","")
}
groupie适配器
class flightitem2(var flights:ArrayList<flighttrip>): Item<GroupieViewHolder>(){
override fun getLayout(): Int {
return R.layout.flights_table
}
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
val flightslist:flighttrip = flights[position]
viewHolder.itemView.fltno.text = flightslist.fltnum
viewHolder.itemView.tdate.text = flightslist.ata
viewHolder.itemView.atdid.text = flightslist.atd
viewHolder.itemView.ataid.text = flightslist.date
viewHolder.itemView.pax.text = flightslist.pax
viewHolder.itemView.from.text = flightslist.fltfrom
viewHolder.itemView.to.text = flightslist.fltto
}
帮助程序类
class databasehelper(){
lateinit var ctx:Context
private var isconnected = false
lateinit var query:String
private lateinit var rv: RecyclerView
lateinit var gadapter2: GroupAdapter<GroupieViewHolder>
var recordcount = 0
var functype = 0
var records =ArrayList<flighttrip>()
val connect = dbconnection()
inner class syncdata:AsyncTask<String, String, String>(){
var message:String = "Something wrong"
lateinit var dialogo:AlertDialog.Builder
override fun onPreExecute() {
records.clear()
dialogo = AlertDialog.Builder(ctx)
dialogo.setMessage("Please wait until the data appears")
dialogo.show()
}
override fun doInBackground(vararg params: String?): String {
try {
val myconnect = connect.connection()
if (myconnect == null) {
isconnected = false
} else {
val statment = myconnect.createStatement()
val curser = statment.executeQuery(query)
if (curser != null) {
while (curser.next())
try {
when (functype) {
1 -> records.add(flighttrip(curser.getString("tr_fl_num"),curser.getString("tr_com_id"),curser.getString("tr_ac_reg"), curser.getString("tr_date"),
curser.getString("tr_atd"),
curser.getString("tr_ata"),curser.getString("tr_pax"),
curser.getString("tr_sf") ,curser.getString("tr_sto")
)
)
}
records
recordcount++
} catch (e: Exception) {
}
message = "found $recordcount"
isconnected = true
} else {
message = "no records in db"
isconnected = false
}
}
}catch (e:Exception){
e.printStackTrace()
val writer = StringWriter()
message = writer.toString()
isconnected = false
}
return message
}
override fun onPostExecute(result: String?) {
dialogo.setOnCancelListener {
it.cancel()
}
Toast.makeText(ctx,message,Toast.LENGTH_LONG).show()
if(isconnected == false){
Log.d("errore","is connected came with nulled data")
}
else {
try {
for(i in records) {
rv.adapter = gadapter2
gadapter2.add(flightitem2(records))
}
}catch (ex:Exception){
Log.d("error",ex.toString())
}
}
}
}
fun gettrips(rv:RecyclerView){
this.rv = rv
functype = 1
query = "SELECT TOP 200 [tr_fl_num],[tr_com_id],[tr_ac_reg],[tr_date],[tr_atd],[tr_ata],[tr_pax],[tr_sf],[tr_sto] FROM dbo.trhand order by [tr_date] DESC"
gadapter2 = GroupAdapter()
syncdata().execute("")
}
}