2017-03-26 57 views
1

在片段數據我有一個創造,我想顯示列表視圖中的片段,但我的片段dislplayed wtihout ListView控件和我正在一個錯誤的ListView不顯示使用ArrayAdapter

我看了很多解決這個問題但我找不到合適的人。 一個小小的幫助會很棒。謝謝!

public class ServicesFragment extends Fragment { 
    public TextView servicesName; 
    ListView listView; 
    String[] servicesNameArray; 
    int[] serviceImages = { 
      R.drawable.ic_menu_camera, 
      R.drawable.ic_menu_camera, 
      R.drawable.ic_menu_camera, 
      R.drawable.ic_menu_camera, 
      R.drawable.ic_menu_camera, 
      R.drawable.ic_menu_camera, 
      R.drawable.ic_menu_camera, 
      R.drawable.ic_menu_camera 
    }; 
    public ServicesFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view= inflater.inflate(R.layout.services_fragment, container, false); 
     listView = (ListView) view.findViewById(R.id.listView_services); 
     ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages); 
     listView.setAdapter(adapter); 
     Resources resources = getResources(); 
     resources.getStringArray(R.array.servicesName); 
     return view; 
    } 

    public class ServicesAdapter extends ArrayAdapter<String> { 

Context c; 
     int[] servicesImageArray; 
     String[] servicesNameArray; 
     public ServicesAdapter(Context context, String[] servicesNameArray, int[] serviceImages) { 
      super(context, R.layout.services_layout, R.id.listView_services); 
      this.c = context; 
      this.servicesImageArray = serviceImages; 
      this.servicesNameArray = servicesNameArray; 
     } 

     @NonNull 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View view = inflater.inflate(R.layout.services_layout, parent, false); 
      ImageView serviceImages = (ImageView) view.findViewById(R.id.image_of_services); 
      TextView servicesName = (TextView) view.findViewById(R.id.name_of_services); 
      serviceImages.setImageResource(servicesImageArray[position]); 
      servicesName.setText(servicesNameArray[position]); 
      return view;}}} 
+0

確保你所需要的數據傳遞到您的列表適配器 –

+0

是的,我通過它 –

+0

,你填寫servicesNameArray與數據??它似乎通過了空 –

回答

2

你是不是到字符串數組分配值:使用

servicesNameArray = getResources().getStringArray(R.array.servicesName); 

,而不是和:

ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages); 

使用活動背景:

ServicesAdapter adapter = new ServicesAdapter(this.getActivity(), servicesNameArray, serviceImages); 

你需要通知你的適配器關於你的dat一組大小:

super(context, R.layout.services_layout, servicesNameArray); //change here 
+0

得到片段謝謝......但它仍然不起作用 –

+0

我已編輯答案... – rafsanahmad007

+0

非常感謝你 –

0

我想這個問題,因爲你不正確的方式傳遞數據

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view= inflater.inflate(R.layout.services_fragment, container, false); 
Resources resources = getResources(); 
     servicesNameArray = resources.getStringArray(R.array.servicesName); 
     listView = (ListView) view.findViewById(R.id.listView_services); 
     ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages); 
     listView.setAdapter(adapter); 

     return view; 
    } 
+0

我編輯了代碼但它不起作用 –