-2

Here is my custom adapter. When I use this adapter with listview or gridview in fragment, It shows the list on page but not clickable. I cant use setOnItemClickListener with this adapter. What's wrong this code? please help...

public class CustomAdapter extends BaseAdapter {

List<Kayitlar> kayit=new ArrayList<>();
LayoutInflater li;
Context context;

public CustomAdapter(Context _context, List<Kayitlar> _kayit) {
    this.context=_context;
    this.kayit = _kayit;
}

@Override
public int getCount() {
    //listview de gösterilecek satır sayısı

    return kayit.size();
}

@Override
public Kayitlar getItem(int position) {
    // position ile sırası gelen eleman

    return kayit.get(position);
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

@Override
public boolean areAllItemsEnabled() {
    return super.areAllItemsEnabled();
}

@Override
public long getItemId(int position) {
    // varsa niteleyici ID bilgisi
    return position;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //  position ile sırası gelen satır için bir view döndürür

    li=LayoutInflater.from(context);
    View gunler =li.inflate(R.layout.gun,null);
    TextView text1=gunler.findViewById(R.id.text1);
    TextView text2=gunler.findViewById(R.id.text2);
    TextView text3=gunler.findViewById(R.id.text3);
    TextView text4=gunler.findViewById(R.id.text4);

    Kayitlar kayitlar =kayit.get(position);
    text1.setText(kayitlar.getGun());
    text2.setText(kayitlar.getGelir_tutar());
    text3.setText(kayitlar.getGider_tutar());
    text4.setText(kayitlar.getNot_metin());
    Toast.makeText(context, "daptor tıklandı", Toast.LENGTH_SHORT).show();

    gunler.setTag(kayit.get(position).getGun());

    return gunler;
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
clk
  • 17
  • 3
  • 1
    how and where are you setting `setOnItemClickListener ` to listview – prashant17 Jul 28 '18 at 15:27
  • When I use in MainActivity, it is working. But I added listview or gridview with this adapter to any Fragment. It is not working. – clk Jul 28 '18 at 15:44

1 Answers1

0

You just have to set it up properly and implement the callbacks. So start by making yourself an interface. you can either nest it in your adapter class (which is common practice) or you can separate it out, up to you. For this example, I nested it at the bottom of the class.

NOTE* I would recommend using RecyclerViews and RecyclerView.Adapter unless you have a static list or know your content is small. I will supply an example of a small list of filter items where a simple baseAdapter is adequate for your knowledge sharing.

Then let's make an adapter. I will share an example of a filter menu I created. Notice the ViewHolder pattern for ensuring reusable UI and passing the callback listener into place.

import android.content.Context
import android.support.v7.widget.SwitchCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import android.widget.Space
import android.widget.TextView
import com.appstudio35.yourappstudio.R
import com.appstudio35.yourappstudio.models.CategoryModel
import java.util.*

/**
 * Copyright © 2017 App Studio 35. All rights reserved.
 *
 * Created by App Studio 35 on 7/3/17.
 */
class FilterListAdapter : BaseAdapter {

    ////////////////////////////////////////////////////////////////
    // MEMBERS
    ////////////////////////////////////////////////////////////////
    private var mData: ArrayList<CategoryModel>
    private var mContext: Context
    private var mActionMenuItemClickListener: IActionMenuItemClickListener?
    private var layoutInflater: LayoutInflater


    /////////////////////////////////////////////////////////////////
    // CONSTRUCTOR
    /////////////////////////////////////////////////////////////////
    constructor(context: Context, data: ArrayList<CategoryModel>, clickListener: IActionMenuItemClickListener): super(){
        mContext = context
        mData = data
        mActionMenuItemClickListener = clickListener
        layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    }


    /////////////////////////////////////////////////////////////////
    // BASE CLASS OVERRIDES
    /////////////////////////////////////////////////////////////////
    override fun getCount(): Int {
        return mData.size
    }
    override fun getItem(position: Int): Any? {
        return mData[position]
    }
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val holder: ViewHolder
        val model = mData!![position]
        var retView: View

        if (convertView == null) {
            holder = ViewHolder()
            retView = layoutInflater.inflate(R.layout.row_filter_model_action, parent, false)

            holder.llRoot = retView.findViewById<View>(R.id.llRoot) as LinearLayout
            holder.txtDisplayText = retView.findViewById<View>(R.id.txtDisplayText) as TextView
            holder.swIsSelected = retView.findViewById<View>(R.id.swIsSelected) as SwitchCompat
            holder.spcFilter = retView.findViewById<View>(R.id.spcFilter) as Space
            holder.dividerBottom = retView.findViewById(R.id.dividerBottom)

            retView.tag = holder
        } else {
            holder = convertView.tag as ViewHolder
            retView = convertView
        }

        holder.txtDisplayText!!.text = model.getCategoryName()
        //holder.dividerBottom.setVisibility(model.getIsTitleRow() ? View.VISIBLE : View.GONE);
        holder.swIsSelected!!.visibility = View.VISIBLE
        holder.swIsSelected!!.isChecked = model.getIsFilterApplied()
        //holder.spcFilter.setVisibility(model.getIsTitleRow() ? View.GONE : View.VISIBLE);

        actionMenuItem_onClick(retView, position)

        return retView
    }


    /*///////////////////////////////////////////////////////////////
    // CLICK LISTENER
    *////////////////////////////////////////////////////////////////
    private fun actionMenuItem_onClick(convertView: View, position: Int) {
        convertView.setOnClickListener {
            mActionMenuItemClickListener?.onMenuItemClicked(convertView, position)
        }
    }


    /*///////////////////////////////////////////////////////////////
    // VIEWHOLDER PATTERN
    *////////////////////////////////////////////////////////////////
    class ViewHolder {
        var llRoot: LinearLayout? = null
        var txtDisplayText: TextView? = null
        var swIsSelected: SwitchCompat? = null
        var spcFilter: Space? = null
        var dividerBottom: View? = null
    }


    /*///////////////////////////////////////////////////////////////
    // INTERFACE
    *////////////////////////////////////////////////////////////////
    interface IActionMenuItemClickListener {
        fun onMenuItemClicked(v: View, position: Int)
    }
}

Now you just implement the interface and catch the click and you are done.

class myFragment : BaseFragment(), FilterListAdapter.IActionMenuItemClickListener {

now in the class you must implement the callback.

override fun onMenuItemClicked(v: View, position: Int) {
    //handle your clicked item
}

That's all there is to it, and you are done. Happy coding.

Sam
  • 5,342
  • 1
  • 23
  • 39