|  |  | 
 |  |  | import android.widget.TextView | 
 |  |  | import androidx.recyclerview.widget.RecyclerView | 
 |  |  | import com.example.firstapp.R | 
 |  |  | import com.example.firstapp.model.PackageInfo | 
 |  |  | import com.example.firstapp.database.entity.Code | 
 |  |  | import java.text.ParseException | 
 |  |  | import java.text.SimpleDateFormat | 
 |  |  | import java.util.Date | 
 |  |  | import java.util.Locale | 
 |  |  |  | 
 |  |  |  | 
 |  |  | class PackageAdapter : RecyclerView.Adapter<PackageAdapter.PackageViewHolder>() { | 
 |  |  |  | 
 |  |  |     private var packages = listOf<PackageInfo>() | 
 |  |  |     private var packages = listOf<Code>() | 
 |  |  |  | 
 |  |  |     fun updatePackages(newPackages: List<PackageInfo>) { | 
 |  |  |     fun updatePackages(newPackages: List<Code>) { | 
 |  |  |         packages = newPackages | 
 |  |  |         notifyDataSetChanged() | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //此处是渲染数据 | 
 |  |  |     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PackageViewHolder { | 
 |  |  |         val view = LayoutInflater.from(parent.context) | 
 |  |  |             .inflate(R.layout.item_package_dashboard, parent, false) | 
 |  |  | 
 |  |  |         private val textCourierName: TextView = view.findViewById(R.id.text_courier_name) | 
 |  |  |         private val textTrackingNumber: TextView = view.findViewById(R.id.text_tracking_number) | 
 |  |  |         private val textTime: TextView = view.findViewById(R.id.text_time) | 
 |  |  |         private val textPickTime: TextView = view.findViewById(R.id.text_pick_time) | 
 |  |  |  | 
 |  |  |         fun bind(packageInfo: PackageInfo) { | 
 |  |  |             imgCourier.setImageResource(packageInfo.courierIcon) | 
 |  |  |             textCourierName.text = packageInfo.courierName | 
 |  |  |             textTrackingNumber.text = packageInfo.trackingNumber | 
 |  |  |             textTime.text = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()) | 
 |  |  |                 .format(packageInfo.receivedTime) | 
 |  |  |         fun bind(code: Code) { | 
 |  |  | //            imgCourier.setImageResource(code.category) | 
 |  |  |             textCourierName.text = code.type | 
 |  |  |             textTrackingNumber.text = code.code | 
 |  |  |             // 步骤1:定义解析器,将字符串转为 Date | 
 |  |  |             val parser = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) | 
 |  |  |             val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()) | 
 |  |  |             try { | 
 |  |  |                 val date: Date? = parser.parse(code.createtime)  // 解析字符串 | 
 |  |  |                 date?.let { | 
 |  |  |                     textTime.text = "到货:"+formatter.format(it)  // 格式化并赋值 | 
 |  |  |                 } ?: run { | 
 |  |  |                     // 处理解析失败(date 为 null 的情况) | 
 |  |  |                     textTime.text = "Invalid Date" | 
 |  |  |                 } | 
 |  |  |             } catch (e: ParseException) { | 
 |  |  |                 e.printStackTrace() | 
 |  |  |                 textTime.text = "Format Error" | 
 |  |  |             } | 
 |  |  |             try { | 
 |  |  |                 val date2: Date? = parser.parse(code.pickuptime)  // 解析字符串 | 
 |  |  |                 date2?.let { | 
 |  |  |                     textPickTime.text = "取件:"+formatter.format(it)  // 格式化并赋值 | 
 |  |  |                 } ?: run { | 
 |  |  |                     textPickTime.text = "未取件" | 
 |  |  |                 } | 
 |  |  |             } catch (e: ParseException) { | 
 |  |  |                 e.printStackTrace() | 
 |  |  |                 textPickTime.text = "未取件" | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } |