我感觉很困,我可能会错过错误。当我通过意图传递变量时,只有 1 个变量(3 个)不会显示在另一个屏幕上的问题。
intent.putExtra("title", model.title)
和intent.putExtra("city", model.city)
有效,但不能intent.putExtra("content", model.description)
这是我的广告适配器.class :
class AdAdapter(ads: FirebaseRecyclerOptions<AdModel?>) : FirebaseRecyclerAdapter<AdModel,
AdAdapter.AdViewHolder>(ads) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_list,
parent, false)
return AdViewHolder(itemView)
}
override fun onBindViewHolder(holder: AdViewHolder, position: Int, model: AdModel) {
//holder.imageView.setImageURI(model.uri)
holder.textView1.text = model.title;
holder.textView2.text = model.city;
holder.relativeLayout.setOnClickListener { view -> //Toast.makeText(view.getContext(),"click on item: "+model.getTitle(),Toast.LENGTH_LONG).show();
val intent = Intent(view.context, Details::class.java)
intent.putExtra("title", model.title) // shows in another screen
intent.putExtra("city", model.city) //shows in another screen
intent.putExtra("content", model.description) // doesn't pass through
view.context.startActivity(intent)
}}
class AdViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//val imageView: ImageView = itemView.findViewById(R.id.image_view)
val textView1: TextView = itemView.findViewById(R.id.text_view_1)
val textView2: TextView = itemView.findViewById(R.id.text_view_2)
val relativeLayout: RelativeLayout = itemView.findViewById(R.id.relativeLayout);
}
}
这是我的详细信息.class :
class Details : AppCompatActivity() {
var detail_title: TextView? = null
var author_title: TextView? = null
var detail_detail: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details)
detail_title = findViewById<View>(R.id.detail_title) as TextView
author_title = findViewById<View>(R.id.detail_city) as TextView
detail_detail = findViewById<View>(R.id.detail_content) as TextView
val intent = intent
val title = intent.getStringExtra("title")
val author = intent.getStringExtra("city")
val description = intent.getStringExtra("content")
detail_title!!.text = title
author_title!!.text = author
detail_detail!!.text = description
}
}
这里还有一个 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Details">
<TextView
android:id="@+id/detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="200dp"
android:layout_marginRight="200dp"
android:layout_marginBottom="682dp"
android:text="TextView"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/detail_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="93dp"
android:layout_marginLeft="93dp"
android:layout_marginTop="196dp"
android:layout_marginEnd="260dp"
android:layout_marginRight="260dp"
android:layout_marginBottom="516dp"
android:text="TextView"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/detail_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="176dp"
android:layout_marginEnd="100dp"
android:layout_marginRight="100dp"
android:layout_marginBottom="445dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/detail_title" />
以下是将数据放入适配器的方法。
class Main : Fragment() {
var mbase: DatabaseReference? = null
var adapter: AdAdapter? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_main, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var list_recycler_view:RecyclerView = view.findViewById(R.id.list_recycler_view);
mbase = FirebaseDatabase.getInstance().reference
val ads = FirebaseRecyclerOptions.Builder<AdModel>()
.setQuery(mbase!!, AdModel::class.java)
.build()
adapter = AdAdapter(ads)
list_recycler_view.setHasFixedSize(true)
list_recycler_view.setLayoutManager(LinearLayoutManager(activity))
list_recycler_view.setAdapter(adapter)
}
override fun onStart() {
super.onStart()
adapter!!.startListening()
}
// Function to tell the app to stop getting
// data from database on stoping of the activity
override fun onStop() {
super.onStop()
adapter!!.stopListening()
}
任何帮助都会得到赞赏。
android:layout_marginLeft="90dp"
android:layout_marginTop="176dp"
android:layout_marginEnd="100dp"
android:layout_marginRight="100dp"
看起来它只有 10dp 宽,这就是你没有看到它的原因吗?还是屏幕外?