2015-09-26 98 views
0

我創建了動作條分頁查看 一個peroject在需要定製的ListView在片段 但getView方法不獲取調用的Android getView()不叫

一個ArrayAdapter類

public class MySimpleArrayAdapter extends ArrayAdapter<ChildPojo> { 

     static class ViewHolder { 
      public TextView text1; 
      public TextView text2; 
     } 

     private final Context context; 
     private final List<ChildPojo> values; 
     public MySimpleArrayAdapter(Context context,List<ChildPojo> values) { 
      super(context, R.layout.item_list, values); 
      this.context = context; 
      this.values = values; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      Log.i("class/getView", "not getting called"); 
      View rowView = convertView; 
      if (rowView == null) { 
       rowView = LayoutInflater.from(this.getContext()) 
         .inflate(R.layout.item_list, parent, false); 
       // configure view holder 
       ViewHolder viewHolder = new ViewHolder(); 
       viewHolder.text1 = (TextView) rowView.findViewById(R.id.item_label); 
       viewHolder.text2 = (TextView) rowView.findViewById(R.id.item_price); 


       rowView.setTag(viewHolder); 
      } 

      ViewHolder holder = (ViewHolder) rowView.getTag(); 


      String s = values.get(position).getNamec(); 
      holder.text1.setText(s); 
      int i = values.get(position).getPrice(); 
      holder.text2.setText(String.format("%d",i)); 


      return rowView; 
     } 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return this.values.size(); 
     } 

     @Override 
     public long getItemId(int arg0) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

    } 


FragmentClass extends List Fragment 

    DAOClass dao; 
    List<ChildPojo> childList = new ArrayList<ChildPojo>(); 

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

     final List<ChildPojo> data = new ArrayList<ChildPojo>(); 
     data.add(new ChildPojo(1,"name 1",100,1)); 
     data.add(new ChildPojo(2,"name 2",200,1)); 
     MySimpleArrayAdapter adapter = new    MySimpleArrayAdapter(getActivity(),data); 

     setListAdapter(adapter); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.row_item, container, false); 
     return view; 
    } 
} 
+0

還可以提供'R.layout.row_item'源代碼? –

+0

檢查你的'R.layout.row_item'文件,根據這個文件必須包含'ListView'的id'@android:id/list' - [check docs](http://developer.android.com/參考/ android /應用程序/ ListFragment.html) –

+0

fragment_main.xml –

回答

0

我假設你在onCreateView中誇大錯誤視圖

在FragmentClass的onCreateView方法中,編輯如下的行

View view = inflater.inflate(R.layout.R.layout.item_list, container, false); 
在SimpleArrayAdapterClass

改變以下線這樣

rowView = LayoutInflater.from(this.getContext()) 
      .inflate(R.layout.row_item, parent, false); 
+0

在代碼我寫了View view = inflater.inflate(R.layout.row_item ,容器,假); BT仍然無法正常工作 –

+0

正如我之前所說的在onCreateView方法 – waleedsarwar86

+0

中將「R.layout.row_item」替換爲「R.layout.item_list」,請添加兩個有問題的XML文件 – waleedsarwar86

0
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.row_item, container, false); 
    final List<ChildPojo> data = new ArrayList<ChildPojo>(); 
     data.add(new ChildPojo(1,"name 1",100,1)); 
     data.add(new ChildPojo(2,"name 2",200,1)); 
     MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(getActivity(),data); 

     setListAdapter(adapter); 
     return view; 
    }