2013-08-22 98 views
0

我想從Activity的onCreate()方法初始化listfragment中列表的內容。的活動代碼這兩種類型的片段初始化有什麼區別

部分:

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_dish); 
      FragmentTransaction transaction1 = getSupportFragmentManager() 
         .beginTransaction(); 
        mDishListFragment = new DishListFragment(); 
       transaction1.add(R.id.dishList_fragment, mDishListFragment, "DishListFragment"); 
       transaction1.commit(); 

mDishListFragment.showItem(totalList.get(0)); 
    } 

片段的部分代碼:

BinderData adapter = null; 
    List<HashMap<String, String>> dishDataCollection; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
     Log.e("emenu", "Dishlistfragment on create"); 
     super.onCreate(savedInstanceState); 
     adapter = new BinderData(getActivity(), R.id.article); 
    } 
public void showItem(List<HashMap<String, String>> map) { 
     dishDataCollection = map;  
     if (dishDataCollection != null) { 
      adapter.init(dishDataCollection); //NullPOinterException 
     } 
     setListAdapter(adapter); 
    } 

我得到初始化我的自定義適配器時,在一個NullPointerException異常。

現在,當我以不同的方式在活動中初始化我的片段時,此代碼工作得非常好。的

mDishListFragment = (DishListFragment) getSupportFragmentManager() 
        .findFragmentById(R.id.dishList_fragment); 

初始化,而不是我做的第一代碼上面博克的方式:是這樣的。

我需要使用片段事務add()方法,因爲我需要能夠隨時隱藏和顯示片段。

這兩種類型的初始化之間有什麼區別,爲什麼前者不工作而後者呢?

希望你們可以幫忙。如果您需要代碼的其他部分,請告訴我。

供您參考,適配器代碼:

LayoutInflater inflater; 
    ImageView thumb_image; 
    List<HashMap<String, String>> dishDataCollection; 
    ViewHolder holder; 

    public BinderData(Context context, int list_row) { 
     // super(context , list_row , map); 
     super(context, list_row); 
     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public void init(List<HashMap<String, String>> map) { 
     this.dishDataCollection = map; 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     // return idlist.size(); 
     return dishDataCollection.size(); 
    } 


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

    public View getView(int position, View convertView, ViewGroup parent) { 

     if (convertView != null) 
      if (position == mItemIndex) { 
       convertView.setSelected(true); 
       convertView.setPressed(true); 
       convertView.setBackgroundResource(R.drawable.gradient_bg_hover); 
      } else { 
       convertView.setBackgroundResource(R.drawable.gradient_bg); 
      } 

     View vi = convertView; 

     if (convertView == null) { 
      vi = inflater.inflate(R.layout.list_row, parent, false); 
      holder = new ViewHolder(); 

      holder.name = (TextView) vi.findViewById(R.id.dish); // city name 
      holder.rate = (TextView) vi.findViewById(R.id.rate); // city weather 
                    // overview 
      holder.shortdescription = (TextView) vi 
        .findViewById(R.id.shortdescription); // city temperature 
      holder.dishImage = (ImageView) vi.findViewById(R.id.list_image); // thumb 
                       // image 
                       // vi.setActivated(true); 
      vi.setTag(holder); 
     } else { 

      holder = (ViewHolder) vi.getTag(); 
     } 

     // Setting all values in listview 

     holder.name.setText(dishDataCollection.get(position).get(KEY_NAME)); 
     holder.rate.setText(dishDataCollection.get(position).get(KEY_RATE)); 
     holder.shortdescription.setText(dishDataCollection.get(position).get(
       KEY_SHORT)); 

     // Setting an image 
     String uri = "drawable/" 
       + dishDataCollection.get(position).get(KEY_ICON); 
     int imageResource = vi 
       .getContext() 
       .getApplicationContext() 
       .getResources() 
       .getIdentifier(
         uri, 
         null, 
         vi.getContext().getApplicationContext() 
           .getPackageName()); 
     Drawable image = vi.getContext().getResources() 
       .getDrawable(imageResource); 
     holder.dishImage.setImageDrawable(image); 

     return vi; 
    } 

    /* 
    * 
    * */ 
    static class ViewHolder { 

     TextView name; 
     TextView rate; 
     TextView shortdescription; 
     ImageView dishImage; 
    } 

    public void setSelectItem(int index) { 
     mItemIndex = index; 
    } 

    public int getSelectItem() { 
     return mItemIndex; 
    } 
+0

不同的是,在第一個例子中所創建的DishListFragment的一個新實例,並在第二個例子中要創建一個指向該片段的現有實例。您應該關注NullPointerExceptions並找出造成這種情況的原因。 – wyoskibum

+0

你可以把BinderData類的代碼? –

+0

@RaúlJuárez:我剛添加了適配器。請不要,即使我用一個簡單的textView替換列表視圖,我嘗試setText()上,我得到nullPointer錯誤。我的意思是說,這似乎不是適配器的問題。 – user2702768

回答

0

您可能需要膨脹視圖而不是setContentView(R.layout.activity_dish);

View view = inflater.inflate(R.layout.fragment_rsslist_overview, 
    container, false); 

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class DetailFragment extends Fragment { 

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

    public void setText(String item) { 
    TextView view = (TextView) getView().findViewById(R.id.detailsText); 
    view.setText(item); 
    } 
} 
相關問題