访问自定义类的多维数组而不返回元素

时间:2017-05-09 14:04:08

标签: java android arrays

我正在尝试创建自定义类(PeopleInfo)的多维数组。

public class PeopleInfo {

    public String name;
    public String address;
    public Integer houseNumber;
    public String job;
    public String title;

    public PeopleInfo(String name, String address, Integer houseNumber, String job, String title) {
        this.name = name;
        this.address = address;
        this.houseNumber = houseNumber;
        this.job = job;
        this.title = title;
    }

在创建数组的java文件中,这是选项卡式应用程序中的一个片段:

public class Employees extends Fragment {
    PeopleInfo[] testArray = new PeopleInfo[] { new 
    PeopleInfo("Joe","Bloggs",4,"cleaner","Mr"), new 
    PeopleInfo("Kim","Lim",34,"CEO","Mrs.")};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.employees, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ListView listView = (ListView) getView().findViewById(R.id.listView);
        CustomAdapter customAdapter = new CustomAdapter();
        listView.setAdapter(customAdapter);
        Log.d("TEST", testArray[0].name);
    }

    class CustomAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return testArray.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getActivity().getLayoutInflater().inflate(R.layout.customlayout,null);

            ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
            TextView textView_title = (TextView)view.findViewById(R.id.textView_title);
            TextView textView_info = (TextView)view.findViewById(R.id.textView_info);
            Button button_one = (Button)view.findViewById(R.id.button_go);
            Button button_two = (Button)view.findViewById(R.id.button_info);

            return view;
        }
    }
}

使用以下语句在onViewCreated中打印数组元素时,它返回0.

Log.d("TEST", testArray[0].name);

打印数组的长度返回2.

0 个答案:

没有答案