1

我有一個FragmentActivity(main),它創建3 Fragments並且還有一個菜單。非常直接,並且來自Android SDK中的示例。從FragmentActivity中的對話框中更新ListView(使用ViewPager)

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    dbh = new DBHelper(this); 

    // Create the adapter that will return a fragment for each of the 
    // primary sections of the app. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
} 

// code // 


@Override 
    public Fragment getItem(int position) { 
     Fragment fragment = null; 
     switch(position){ 
      case 0: 
       fragment = new DaySectionFragment(Main.this, dbh); 
       break; 
      case 1: 
       fragment = new WeekSectionFragment(Main.this, dbh); 
       break; 
      case 2: 
       fragment = new FoodListSectionFragment(Main.this, dbh); 
       break; 
      case 3: 
       fragment = new AboutSectionFragment(); 
       break; 
     } 

     return fragment; 
    } 
//more code 

從主要活動的菜單中我有一個editText對話框。這個文本字段的值假設存儲在數據庫中,工作正常,並且也在片段中的列表視圖中彈出(不是ListFragment,而是帶有listview的片段)。簡單的方法是在ListView適配器上調用notifyDataSetChanged()。但是我不能那樣做。

這是使用ListView片段:

public class FoodListSectionFragment extends Fragment { 
private Context context; 
private DBHelper dbh; 
private ArrayList<FoodData> listItems = new ArrayList<FoodData>(); 
private FoodAdapter adapterFoodList; 
private AdapterView.AdapterContextMenuInfo adapterInfo; 
private ListView lvItems; 

public FoodListSectionFragment() { 
} 
public FoodListSectionFragment(Context context, DBHelper dbh) { 
    this.context = context; 
    this.dbh = dbh; 
    //setTag("FoodListFragment"); 
} 
public void updateList(){ 
    adapterFoodList.notifyDataSetChanged(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View myView = getLayoutInflater(null).inflate(R.layout.food_list, null); 

    listItems.clear(); 
    listItems = (ArrayList<FoodData>) dbh.selectAllFood(); 

    adapterFoodList = new FoodAdapter(context, R.layout.list_row_food, listItems); 

    lvItems = (ListView)myView.findViewById(R.id.listFood); 
    lvItems.setAdapter(adapterFoodList); 
    adapterFoodList.notifyDataSetChanged(); 

    return myView; 
} 
} 

這裏就是我試圖更新ListView的,雖然這是行不通的。

dialogAddFood = new Dialog(Main.this); 
      dialogAddFood.setContentView(R.layout.dialog_add_food); 
      dialogAddFood.setTitle(R.string.menu_add_food); 
      dialogAddFood.setCancelable(true); 

      Button btnSave = (Button) dialogAddFood.findViewById(R.id.btnSave); 
      btnSave.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        EditText edtFood = (EditText)dialogAddFood.findViewById(R.id.edtFood); 
        RatingBar ratingGrade = (RatingBar)dialogAddFood.findViewById(R.id.ratingGrade); 
        RatingBar ratingDiff = (RatingBar)dialogAddFood.findViewById(R.id.ratingDiff); 

        if(edtFood.getText().toString().length() > 0){ 
         dbh.insertFood(edtFood.getText().toString(), (int)ratingGrade.getRating(), (int)ratingDiff.getRating()); 
         Toast.makeText(Main.this, "Maträtt tillagd", Toast.LENGTH_LONG).show(); 

         //FoodListSectionFragment f = (FoodListSectionFragment)Main.this.getSupportFragmentManager().findFragmentByTag("FoodList"); 
         //f.updateList(); 

         ListView l = (ListView)mSectionsPagerAdapter.getItem(2).getView().findViewById(R.id.listFood); 
         FoodAdapter a = (FoodAdapter)l.getAdapter(); 
         a.notifyDataSetChanged(); 

        } 

        dialogAddFood.cancel(); 
       } 
      }); 
      Button btnCancel = (Button) dialogAddFood.findViewById(R.id.btnCancel); 
      btnCancel.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        dialogAddFood.cancel(); 
       } 
      }); 
      dialogAddFood.show(); 

請幫忙。

回答

2

您的代碼不起作用,因爲該行:

mSectionsPagerAdapter.getItem(2) 

不會在第2位的ViewPager返回Fragment,它創建該位置的新Fragment,並呼籲該更新方法Fragment實例顯然不會做任何更改,因爲此Fragment未附加到您的Activity(不是可見的)。

嘗試尋找那些Fragment使用FragmentManager像下面,看看它是如何去:

// ... 
Toast.makeText(Main.this, "Maträtt tillagd", Toast.LENGTH_LONG).show(); 

//FoodListSectionFragment f = (FoodListSectionFragment)Main.this.getSupportFragmentManager().findFragmentByTag("FoodList"); 
//f.updateList(); 
FoodListSectionFragment fr = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.theIdOfTheViewPager + ":2"); 
if (fr != null && fr.getView() != null) { 
    fr.updateList(); 
} 
+0

這工作!真棒!我只需要將GetSupportFragment()。findFragm ...轉換爲FoodListSectionFragment。 – Lubis

+0

但是有些奇怪。該列表不會直接更新,我必須刷幾次。 – Lubis

+0

@Lubis即使您在列表頁面(因此列表可見),列表也不會立即更新? – Luksprog

相關問題