Friday, May 24, 2024

Android Java: spinner in fragment

layout_fragment_me.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment"
        android:textSize="38sp"
        />
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_spinner"
        android:minHeight="50dp"
        />

    <androidx.appcompat.widget.AppCompatButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HELLO BUTTON"
        />
</androidx.appcompat.widget.LinearLayoutCompat> 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <androidx.fragment.app.FragmentContainerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_view_me"
        />
    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Activity"
        android:textSize="30dp"
        />
</androidx.appcompat.widget.LinearLayoutCompat>

MyData.java

package com.dedetok.tutorialcustomdropdown;

public class MyData {
    public String name, phone;

    @Override
    public String toString() {
        return name+" "+phone;
    }
}

FragmentMe.java

package com.dedetok.tutorialcustomdropdown;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;

public class FragmentMe extends Fragment {

    ArrayList<MyData> arrayList = new ArrayList<>();

    public FragmentMe() {
        super(R.layout.layout_fragment_me);
        Log.e("dedetok", "Create Fragment ME"); // debug
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        init();
        Log.e("dedetok", "size "+arrayList.size()); // debug
        Spinner mySpinner = view.findViewById(R.id.my_spinner);
        ArrayAdapter<MyData> arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, arrayList);
        Log.e("dedetok", "setAdapter"); // debug
        mySpinner.setAdapter(arrayAdapter);
        mySpinner.setOnItemSelectedListener(mySpinnerListener);

    }

    AdapterView.OnItemSelectedListener mySpinnerListener =
            new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    if (parent.getItemAtPosition(pos) instanceof MyData) {
                        MyData myData = (MyData) parent.getItemAtPosition(pos);
                        // TODO
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                    // Do nothing
                }
            };
    private void init() {
        MyData myData = new MyData();
        myData.name = "aaaa";
        myData.phone = "1111";
        arrayList.add(myData);
        myData = new MyData();
        myData.name = "bbbb";
        myData.phone = "2222";
        arrayList.add(myData);
        myData = new MyData();
        myData.name = "ccccc";
        myData.phone = "2233322";
        arrayList.add(myData);
        myData = new MyData();
        myData.name = "4ddddd";
        myData.phone = "444444";
        arrayList.add(myData);
    }
}

MainActivity.java

package com.dedetok.tutorialcustomdropdown;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        FragmentMe fragment = new FragmentMe();
        transaction.setReorderingAllowed(true);
        transaction.add(R.id.fragment_view_me, fragment);
        transaction.commit();
    }
}

No comments:

Post a Comment