Android Studio开发之 Fragment子类中使用ArrayAdapter

Fragment子类中使用ArrayAdapter中的Context参数获取有三种方式:

第一种:getActivity()   -- 最方便的方式,推荐使用。

第二种:使用inflater.inflate()方式得到的view对象获取。view.getContext()。

第三种:使用onCreateView从上层传过来的ViewGroup类型的container对象来获取,container.getContext()。

 

下面给出部分源码实例Demo:

package com.terawins.www.t613_ui_tool;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

public class DeviceConnect extends Fragment{
    private RadioGroup rg_communication = null;
    private Button btn_connect = null;
    private Spinner spin_devPath = null, spin_baud = null;
    private boolean bFlagI2C = false;
    private boolean bFlagOpen = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_device_connect, container, false);
        //do others
        spin_devPath = view.findViewById(R.id.spin_devPath);
        spin_devPath.setSelection(1);
        spin_baud = view.findViewById(R.id.spin_baud);
        spin_baud.setSelection(7);

        rg_communication = view.findViewById(R.id.rg_communication);
        rg_communication.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i)
                {
                    case R.id.rb_i2c:
                    {
                        bFlagI2C = true;
                        String[] itemList = getResources().getStringArray(R.array.spin_devPath_i2c);
                        ArrayAdapter dataAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_dropdown_item, itemList);
//                        ArrayAdapter dataAdapter = new ArrayAdapter(view.getContext(), android.R.layout.simple_spinner_dropdown_item, itemList);
//                        ArrayAdapter dataAdapter = new ArrayAdapter(container.getContext(), android.R.layout.simple_spinner_dropdown_item, itemList);
                        spin_devPath.setAdapter(dataAdapter);
                        spin_devPath.setSelection(1);
                        //
                        TextView lab_baud = view.findViewById(R.id.lab_baud);
                        lab_baud.setVisibility(View.INVISIBLE);
                        spin_baud.setVisibility(View.INVISIBLE);
                        // ShowMsg("It will send command data by I2C.");
                    }
                    break;
                    case R.id.rb_uart:
                    {
                        bFlagI2C = false;
                        String[] itemList = getResources().getStringArray(R.array.spin_devPath_uart);
                        ArrayAdapter dataAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_dropdown_item, itemList);
                        spin_devPath.setAdapter(dataAdapter);
                        spin_devPath.setSelection(1);
                        //
                        TextView lab_baud = view.findViewById(R.id.lab_baud);
                        lab_baud.setVisibility(View.VISIBLE);
                        spin_baud.setVisibility(View.VISIBLE);
                        //  ShowMsg("It will send command data by UART.");
                    }
                    break;
                }
            }
        });

        return view;
    }
}

---- The End.

你可能感兴趣的:(Android布局,Fragment,ArrayAdapter)