2015-05-14 66 views
-1

我的代碼,具體如下:問題是,getView()方法不會被調用一次我檢查了我的日誌本內BaseAdapter類的getView()沒有得到

我的片段類是

public class AlertListFragment extends Fragment { 
    // TODO: Rename parameter arguments, choose names that match 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 
    public ArrayList<AlertModel> mModelLIst=new ArrayList<AlertModel>(); 
    public AlertAdapter adapter; 
    ListView mAlertList; 
    CommentsDataSource datasource; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view=inflater.inflate(R.layout.fragment_alert_list, container, false); 
     mAlertList=(ListView)view.findViewById(R.id.alertlist); 

     System.out.println("details in fragment"); 
     datasource = new CommentsDataSource(getActivity()); 
     datasource.open(); 
     ArrayList<AlertModel> source = datasource 
       .getAllContacts(); 

     if (source.size() > 0) { 
      System.out.println("response number of items" 
        + source.size()); 

      adapter = new AlertAdapter(getActivity(), source,getActivity().getResources()); 
      mAlertList.setAdapter(adapter); 


      } 

     return inflater.inflate(R.layout.fragment_alert_list, container, false); 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 

    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 

    } 
} 

我的適配器類是

public class AlertAdapter extends BaseAdapter implements View.OnClickListener { 

    private Activity activity; 
    private ArrayList data; 
    ArrayList<String> btnpositions = new ArrayList<String>(); 
    public Resources res; 

    private static LayoutInflater inflater = null; 
    ViewHolder holder; 
    AlertModel tempValue = null; 

    public AlertAdapter(Activity activity, ArrayList data, Resources res) { 

     System.out.println("response number of items inside adapter view: "); 
     this.activity = activity; 
     this.data = data; 
     this.res = res; 

    } 




    public static class ViewHolder { 

     public TextView msubject, mbody; 
     public ImageView mTYpe; 

     public LinearLayout mTYpeLay; 

    } 


    @Override 
    public int getCount() { 


    System.out.println("response number of items inside adapter count: "+data.size()); 
     if (data.size() <= 0) 
      return 1; 

     System.out.println("response number of items inside adapter count: "+data.size()); 
     return data.size(); 

    } 

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

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

    @Override 
    public View getView(int arg0, View vis, ViewGroup arg2) { 


     System.out.println("response number of items inside adapter view: "+arg0); 
     View vi = vis; 

     int crntposition = arg0; 
     Context context; 

     if (vis == null) { 


      inflater = (LayoutInflater) activity. 
        getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      vi = inflater.inflate(R.layout.alert_list_item, null); 
     } else 
      holder = (ViewHolder) vi.getTag(); 

     holder = new ViewHolder(); 

     holder.msubject = (TextView) vi.findViewById(R.id.txtsubject); 
     holder.mbody = (TextView) vi.findViewById(R.id.txtbody); 
     holder.mTYpe = (ImageView) vi.findViewById(R.id.imgtype); 
     holder.mTYpeLay=(LinearLayout)vi.findViewById(R.id.typelay); 

     if (data.size() <= 0) { 
      holder.msubject.setText("No Data"); 

     } else { 
      vi.setTag(holder); 
      vi.setTag(R.id.txtsubject, holder.msubject); 
      vi.setTag(R.id.txtbody, holder.mbody); 
      vi.setTag(R.id.imgtype, holder.mTYpe); 

      holder.msubject.setTag(crntposition); 
      holder.mbody.setTag(crntposition); 
      holder.mTYpe.setTag(crntposition); 


      tempValue = null; 
      tempValue = (AlertModel) data.get(crntposition); 


      /************ Set Model values in Holder elements ***********/ 

      context = arg2.getContext(); 

      holder.msubject.setText(tempValue.getmMessage()); 
      if(tempValue.getmMessage().equalsIgnoreCase("1")) 
      { 
       holder.mTYpeLay.setBackgroundResource(R.color.quote_blue); 
       holder.mTYpe.setBackgroundResource(R.drawable.q); 
      } 
      else if(tempValue.getmMessage().equalsIgnoreCase("2")) 
      { 
       holder.mTYpeLay.setBackgroundResource(R.color.event_yellow); 
       holder.mTYpe.setBackgroundResource(R.drawable.e); 
      } 
      else 
      { 
       holder.mTYpeLay.setBackgroundResource(R.color.wishes_red); 
       holder.mTYpe.setBackgroundResource(R.drawable.bday); 
      } 




     } 

     vi.setOnClickListener(new OnItemClickListener(crntposition)); 

     return vi; 

    } 
    @Override 
    public void onClick(View v) { 

     Log.e("click","yess"); 

    } 
    private class OnItemClickListener implements View.OnClickListener { 
     private int mPosition; 

     OnItemClickListener(int position) { 

      mPosition = position; 



      System.out.println("pos"+mPosition); 
     } 

     @Override 
     public void onClick(View arg0) { 



     } 

    } 

} 

的片段我的XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    tools:context="fencer.geo.ti.geofencer.fragments.AlertListFragment"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:text="fragment" 
     android:id="@+id/txtt" 
     android:layout_height="wrap_content" /> 

    <!-- TODO: Update blank fragment layout --> 
    <ListView android:layout_width="match_parent" android:layout_height="wrap_content" 
android:layout_below="@+id/txtt" 
     android:background="@color/second_grey" 
     android:id="@+id/alertlist" 

     /> 

</RelativeLayout> 

這是我的日誌

05-14 15:10:04.362 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items13 

05-14 15:10:04.363 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items inside adapter view: 

05-14 15:10:04.363 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items inside adapter count: 13 

05-14 15:10:04.363 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items inside adapter count: 13 

05-14 15:10:04.363 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items inside adapter count: 13 

和我的輸出是

my output

+0

檢查我在這裏回答:http://stackoverflow.com/questions/16338281/custom-adapter-getview-method-is-not-called/16338380#16338380 – Blackbelt

+0

@Blackbelt請查看我的全部代碼我編輯了它我沒有犯你在回答中提到的任何錯誤 –

回答

0

,你必須在你onCreateView錯誤的返回,與

return inflater.inflate(R.layout.fragment_alert_list, container, false); 

你完全返回新的充氣視圖,ListView,完全沒有你的適配器知識。將其更改爲

return view; 
+0

THANKSSSSSSSSSSSSSS god bless –

+0

不客氣 – Blackbelt

相關問題