无法在第二个阵列上定位

时间:2019-09-08 06:38:43

标签: android kotlin

我正在使用GSON解析我的JSON。我有一个具有2个数组对象的对象。我在获取TransactionDetails位置时遇到问题。

我有一个填充“交易记录”的recyclerView,然后在我点击它时应打开“ TransactionDetails”。但是我很难在TransactionDetails上获得要读取的数组的位置。

正如您在代码上看到的那样,我需要在[0]上放置一个位置,因为它仅将第1个数组加载到交易明细上。

JSON Url

DetailedTransactionAdapter

class DetailedTransactionAdapter(val transactionFeed: TransactionFeed) :  RecyclerView.Adapter<DetailCustomViewHolder>() {

    override fun onBindViewHolder(holder: DetailCustomViewHolder, position: Int) {
        val tr = transactionFeed.Transactions[1].TransactionDetails[position]

  holder.view.txt_programType.text = transactionDetail.ProgramType
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailCustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.list_detailed_transaction, parent, false)
        return DetailCustomViewHolder(cellForRow)
    }

    override fun getItemCount(): Int {
         return transactionFeed.Transactions[0].TransactionDetails.count()
    }

}

class DetailCustomViewHolder(val view: View): RecyclerView.ViewHolder(view) {
    init{

    }
}
class TransactionFeed(val Transactions: List<Transactions>)

class Transactions(val TransactionCode: String,
                   val ProgramID: Int,
                   val ProgramName: String,
                   val ProgramType: String,
                   val ProgramDescription: String,
                   val TransactionID: Int,
                   val UserID: String,
                   val TransactionAmount: Int,
                   val TransactionDate: String,
                   val TransactionDetails: List<TransactionDetails>)

class TransactionDetails(val Clamied: Boolean,
                         val NextToClaim: Boolean,
                         val ProgramDetailID: Int,
                         val TransactionDetailID: Int,
                         val ProgramDetails: String,
                         val ProgramType: String,
                         val TransactionDetailAmount: Int,
                         val TransactionDetailMonth: String)

TransactionsAdapter

lass TransactionsAdapter(val transactionFeed: TransactionFeed) : RecyclerView.Adapter<CustomViewHolder>() {

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val tr = transactionFeed.Transactions.get(position)

        holder.view.txt_transaction_id.text = tr.TransactionID.toString()
        holder.view.txt_transaction_date.text = tr.TransactionDate
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.list_transaction, parent, false)
        return CustomViewHolder(cellForRow)
    }

    override fun getItemCount(): Int {
        return transactionFeed.Transactions.count()
    }
}

class CustomViewHolder(val view: View):RecyclerView.ViewHolder(view) {

    init{
        view.setOnClickListener {
            val intent = Intent(view.context, DetailedTransactionActivity::class.java)
            view.context.startActivity(intent)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须在DetailsTransactionAdapter的getItemCount()和onBindViewHolder()中设置适当的位置。这是您可以执行的操作:

首先通过意图传递给DetailsTransactionActivity,将所选适配器中的交易位置(当用户单击任何交易时)首先传递到TransactionsAdapter中。

class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view) {

    init {
        view.setOnClickListener {
            val intent = Intent(view.context, DetailedTransactionActivity::class.java)
            intent.putExtra("selectedTransaction", adapterPosition)
            view.context.startActivity(intent)
        }
    }
}

然后在DetailedTransactionActivity中获得该位置,并将其传递给DetailedTransactionAdapter。

    val transactionPos = intent.getIntExtra("selectedTransaction", -1)
    detailedTransactionAdapter = DetailedTransactionAdapter(transactionFeed, transactionPos)

您必须将自己的DetailedTransactionAdapter更改为还将selectedTransaction作为字段。这将有助于动态填充适配器并绑定适当的数据以进行查看。将适配器隐式更改为此:

    class DetailedTransactionAdapter(val transactionFeed: TransactionFeed, val selectedTransaction:Int) :  RecyclerView.Adapter<DetailCustomViewHolder>() {

    override fun onBindViewHolder(holder: DetailCustomViewHolder, position: Int) {
        if(selectedTransaction == -1){
            return
        }

        val tr = transactionFeed.Transactions[selectedTransaction].TransactionDetails[position]

        holder.view.txt_programType.text = transactionDetail.ProgramType
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailCustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.list_detailed_transaction, parent, false)
        return DetailCustomViewHolder(cellForRow)
    }

    override fun getItemCount(): Int {
        return transactionFeed.Transactions[selectedTransaction].TransactionDetails.count()
    }
}

这是魔术发生的地方。您将返回适当的交易明细计数

  

返回transactionFeed.Transactions [selectedTransaction] .TransactionDetails.count()

和绑定数据。

  

val tr = transactionFeed.Transactions [selectedTransaction] .TransactionDetails [position]

因此,当您选择第一笔交易时,这将返回交易[0] (第一笔交易)的交易明细计数。并且当选择任何其他事务时,这将返回其事务详细信息并使用其数据填充适配器。