错误图像列表视图片段

时间:2016-12-01 20:51:58

标签: android listview android-fragments

enter image description here以下是我的编码,似乎我在

上遇到了问题

AgentAdapter adapter = new AgentAdapter(getActivity(),R.layout.list_agent,member_names,profile_pics);

如何将图像和文本一起用于我的片段中的列表适配器?请帮忙。

public class AgentFragment extends Fragment{




        String[] member_names;
        TypedArray profile_pics;

        ListView mylistview;




       String[] agentname={"Robert","Shanni","Rachel","Mady","Nikhil"};
        Integer [] imgid = {R.drawable.robert,R.drawable.shanni,R.drawable.rachael,R.drawable.maddy_pic,R.drawable.nikhil_pic};

        public AgentFragment(){

        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.list_layout1, null);
            setupList(view);

            return view;
        }
        private void setupList(View view){
            AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
            mylistview = (ListView) view.findViewById(R.id.listView);
            mylistview.setAdapter(adapter);

        }
    }

1 个答案:

答案 0 :(得分:0)

由于片段可能尚未在运行时放置在任何活动中,因此可以通过将getActivity()更改为view参数中的上下文来实现此目的:

private void setupList(View view){
    AgentAdapter adapter = new AgentAdapter(view.getContext(), R.layout.list_agent, member_names, profile_pics);
    mylistview = (ListView) view.findViewById(R.id.listView);
    mylistview.setAdapter(adapter);
}