• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm using a recycler view in fragment which is loaded into main activity.When my recycler view item is clicked i want to set text to textview which is inside main activity. I tried doing this:

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)
    }

But it doesn't work my application crashes Please help me debug this as i'm still a newbie any help will be appreciated.

This is my main activity class:

    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)
            }
        }
    }

This is my adapter class:

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
    }
}
}

Use interfacing to get recylcerView item click callback inside activity class. If you aren't comfortable to use interfacing, google for interface communication or ask me to put code example.

1. Interface:

interface OnAdapterItemClickListener {
    fun onItemClicked(id: Int, text: String)
}

2. Your adapter class

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. Implement interface in your fragment

    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. Implement interface in your activity too

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)
    }

}
  • 2
Reply Report

hi i solve this problem with below steps :


1-add a TextView in adapter constructor

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;
}


2- then in onBindViewHolder method you can set a value for textView

public void onBindViewHolder(final CartRecyclerViewHolder holder, final int position) {
activityTv.setText("ali Yazdanifar");

}


3- then you should pass your MainActivity TextView to adapter in other places

TextView activityTv=findViewById(R.id.tv);
RecyclerAdapter adapter = new RecyclerAdapter(context, list, activityTv);
recyclerView.setAdapter(adapter);
  • -1
Reply Report