0

I'm want to do this :

  1. Display a list of item in a list view (done)
  2. Set a background for the even an odd position (done)
  3. When clicking on an element, toggle subitems related to the clicked element.

As you know, ArrayAdapter by default works with TextView only (as root element).

So how can I achieve this ? (I've seen this but still a TextView required after changes).

Here's my custom adapter :

    class ListAdapterScolaire (context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?, private val forScol : Boolean) : ArrayAdapter<String>(context, resource, list) {

    private val TAG = ListAdapterScolaire::class.java.simpleName

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = super.getView(position, convertView, parent)

        val arret = view.findViewById<TextView>(R.id.arret)
//        val rootView = view.findViewById<LinearLayout>(R.id.root_View)
//
//        val list = getViewsByTag(rootView, "toToggleAction")
//
//        list.forEach{it ->
//            it.setOnClickListener{ v: View ->
//                val viewParent = v.parent
//                if (viewParent is LinearLayout) {
//                    val taggedView : View? = viewParent.findViewWithTag<View>("toToggle")
//
//                    taggedView?.visibility = when {
//                        taggedView?.visibility == View.GONE -> View.VISIBLE
//                        else -> View.GONE
//                    }
//                }
//            }
//        }


        arret.setOnClickListener { //part to code for the toggle
    }

        if (position % 2 == 1) {
            view.setBackgroundResource(R.color.colorWhite)
        } else {
            view.setBackgroundResource(R.color.grayBackground)
        }
        return view
    }

How i call it in my code :

adapterScolaire = ListAdapterScolaire(view.context, R.layout.list_adapter, lignesScolaires, resultArgument, true)

And my xml for my item :

    <?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/arret"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:layout_margin="0dp"
    android:text="Nom arret"

    />
Anmol
  • 8,110
  • 9
  • 38
  • 63
Projet Sin
  • 337
  • 8
  • 20
  • 1
    see [this](https://gist.github.com/pskink/2dd4d17a93caf02ff696533e82f952b0) generic `Filterable` adapter – pskink Jan 09 '19 at 17:08
  • In what point will this help me ? – Projet Sin Jan 10 '19 at 08:54
  • simply use it as a base class: `class MyAdapter extends MatchableArrayAdapter { ...` – pskink Jan 10 '19 at 08:56
  • I've tried to implement this. It says : "you must override onBind", what does "bind" means? – Projet Sin Jan 10 '19 at 09:10
  • do you know how to override methods from the base class? if so, override `protected void onBind(T item, View itemView, int position)` method and bind your data here, something like [this](https://gist.github.com/pskink/2dd4d17a93caf02ff696533e82f952b0#file-matchablearrayadapter-java-L22) – pskink Jan 10 '19 at 09:15
  • Where should I override ? In my adapter or in matcharrayadapter ? – Projet Sin Jan 10 '19 at 09:44
  • in your class, for example inside `MyAdapter` - you dont touch `MatchableArrayAdapter` - it is read only – pskink Jan 10 '19 at 09:59
  • And what kind of code should I add into onBind() ? Cause my logic is in getView() for now – Projet Sin Jan 10 '19 at 10:15
  • 1
    http://pastebin.com/07mBMT0t – pskink Jan 10 '19 at 10:17
  • Helpful, it's working. Now I have to find how to pass complex data (such as jsonarray) in the adapter. Thanks ! – Projet Sin Jan 10 '19 at 10:54
  • you pass an array or `List` in the constructor – pskink Jan 10 '19 at 10:57
  • For now, I'm passing an arrayList with only simple data (1 list row = 1 data in arrayList). I have to display data contained in a nested jsonArray (go is to display data1.1... and data2.1... under name : `[{"name":"name","array":{"sub1":[["data1.1.0","data1.1.1","1"],["data1.2.0","data1.2.1","1"]],"sub2":[["data2.1.0","data2.1.1","1"]]}}]` – Projet Sin Jan 10 '19 at 11:03

0 Answers0