我正在使用一个加载到主活动中的片段中的回收站视图。单击我的回收站视图项目时,我想将文本设置为主要活动内的textview。 我尝试这样做:
displayText(MainActivity(),R.id.name,"Text you want to display");
MainActivity().textView!!.setText(name)
fun displayText(activity: Activity, id:Int, text:String) {
val tv = activity.findViewById(id) as TextView
tv.setText(text)
}
但是它不起作用我的应用程序崩溃 请帮助我调试此程序,因为我仍然是新手,将不胜感激。
这是我的主要活动课:
class MainActivity : AppCompatActivity() {
private var adapter: Tabadapter? = null
private var tabLayout: TabLayout? = null
private var viewPager: ViewPager? = null
var PlayPauseView : PlayPauseView? = null
var textView : TextView?=null
var context : Context?=null
var slidingUpPanelLayout : SlidingUpPanelLayout?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
).withListener(object : MultiplePermissionsListener {
override fun onPermissionsChecked(report: MultiplePermissionsReport) {/* ... */
}
override fun onPermissionRationaleShouldBeShown(permissions: List<PermissionRequest>, token: PermissionToken) {/* ... */
}
}).check()
viewPager = findViewById<View>(R.id.viewPager) as ViewPager
tabLayout = findViewById<View>(R.id.tabLayout) as TabLayout
adapter = Tabadapter(supportFragmentManager)
adapter!!.addFragment(Songs(), "Songs")
adapter!!.addFragment(Playlist(), "Playlist")
adapter!!.addFragment(Folders(), "Folders")
adapter!!.addFragment(Albums(), "Albums")
adapter!!.addFragment(Artists(), "Artists")
adapter!!.addFragment(Genres(), "Genres")
viewPager!!.adapter = adapter
tabLayout!!.setupWithViewPager(viewPager)
slidingUpPanelLayout = findViewById(R.id.sliding_layout)
PlayPauseView=findViewById(R.id.play_pause_view)
PlayPauseView!!.setOnClickListener{v->
PlayPauseView!!.toggle()
}
textView = findViewById(R.id.name)
textView!!.startAnimation(AnimationUtils.loadAnimation(this, R.anim.translate) as Animation)
slidingUpPanelLayout!!.addPanelSlideListener(object : SlidingUpPanelLayout.PanelSlideListener {
override fun onPanelSlide(panel: View, slideOffset: Float) {
}
override fun onPanelStateChanged(panel: View, previousState: PanelState, newState: PanelState) {
Log.i("State", "onPanelStateChanged $newState")
}
})
}
fun update_counter(value:String) {
try
{
runOnUiThread() {
textView = findViewById(R.id.name)
textView !!.setText(value)
}
// val mHandler = Handler()
//textView!!.text=value
}
catch (ex:Exception) {
Log.d("Exception", "Exception of type" + ex.message)
}
}
}
这是我的适配器类:
class Songsadapter(private val sname: List<String>, private val sartist: List<String>, private val surl : List<String>, internal var mContext: Context) : RecyclerView.Adapter<Songsadapter.ProjectViewHolder>() {
var mPlayer : MediaPlayer?=null
var ps : Int?=null
override fun onCreateViewHolder(parent: ViewGroup, i: Int): ProjectViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.custom_songs, parent, false)
return ProjectViewHolder(view)
}
override fun onBindViewHolder(holder: ProjectViewHolder, position: Int) {
val pos = holder.adapterPosition
var name = sname.get(pos)
var artist = sartist.get(pos)
var songurl = surl.get(pos)
try {
var metaRetriver: MediaMetadataRetriever
metaRetriver = MediaMetadataRetriever()
metaRetriver.setDataSource(songurl)
var art: ByteArray
art = metaRetriver.embeddedPicture
var songImage = BitmapFactory.decodeByteArray(art, 0, art.size)
var ob: BitmapDrawable = BitmapDrawable(mContext.getResources(), songImage)
holder.covimg!!.setBackgroundDrawable(ob)
}catch (e: Exception){
e.printStackTrace()
}
holder.tvsname.text = name
holder.tvsartist.text = artist
holder.itemView.setOnClickListener(View.OnClickListener {
MainActivity().update_counter(name)
stoplaying()
var i:Int = pos
mPlayer = MediaPlayer()
mPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
mPlayer!!.setDataSource(mContext.getApplicationContext(), Uri.parse(songurl))
mPlayer!!.prepare()
mPlayer!!.start()
mPlayer!!.setOnCompletionListener {
i = i+1
mPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer!!.setDataSource(mContext.getApplicationContext(), Uri.parse(surl.get(i)))
mPlayer!!.prepare()
mPlayer!!.start()
}
})
}
override fun getItemCount(): Int {
return sname.size
}
inner class ProjectViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tvsname: TextView
var tvsartist: TextView
var covimg: ImageView
init {
tvsname = itemView.findViewById(R.id.sname)
tvsartist = itemView.findViewById(R.id.sartist)
covimg = itemView.findViewById(R.id.imgscov)
}
}
fun stoplaying(){
if (mPlayer != null) {
mPlayer!!.stop()
mPlayer!!.release()
mPlayer = null
}
}
}
答案 0 :(得分:2)
使用接口获取活动类中的recylcerView项目单击回调。如果您不习惯使用接口,请使用google进行界面通讯或要求我提供代码示例。
1。界面:
interface OnAdapterItemClickListener {
fun onItemClicked(id: Int, text: String)
}
2。您的适配器类
class Songsadapter(private val clickListener: OnAdapterItemClickListener, private val sname: List<String>, private val sartist: List<String>, private val surl : List<String>, internal var mContext: Context) : RecyclerView.Adapter<Songsadapter.ProjectViewHolder>() {
//adapter code
}
3。在片段中实现界面
class SearchFragment : Fragment(), OnAdapterItemClickListener{
private var mListener: OnAdapterItemClickListener? = null
fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnAdapterItemClickListener) {
mListener = context as OnAdapterItemClickListener
} else {
throw RuntimeException("$context must implement OnFragmentInteractionListener")
}
}
fun onViewCreated(@NonNull view: View, @Nullable savedInstanceState: Bundle) {
super.onViewCreated(view, savedInstanceState)
//add fist parameter as 'this'
Songsadapter mAdapter = Songsadapter(this, others....., )
}
fun onItemClicked(id: Int, text: String) {
//You're getting click callback here in fragment
mListener.onItemClicked(id, text)
}
}
4。在您的活动中也实现界面
class MainActivity : AppCompatActivity(), OnAdapterItemClickListener {
override fun onItemClicked(id: Int, text: String) {
//here you go
Log.i("clicked: ", text)
val tv = findViewById(id) as TextView
tv.setText(text)
}
}
答案 1 :(得分:0)
嗨,我通过以下步骤解决了这个问题:
private Context mContext;
private List<RecyclerRowItem> mList;
private TextView activityTv;
public CartRecyclerAdapter(Context mContext, List<RecyclerRowItem> mList, TextView activityTv) {
this.mContext = mContext;
this.mList = mList;
this.activityTv = activityTv;
}
设置一个值
public void onBindViewHolder(final CartRecyclerViewHolder holder, final int position) {
activityTv.setText("ali Yazdanifar");
}
TextView activityTv=findViewById(R.id.tv);
RecyclerAdapter adapter = new RecyclerAdapter(context, list, activityTv);
recyclerView.setAdapter(adapter);