2016-07-28 63 views
0

首先請糾正我,如果我的意識形態不正確,這是我第一次使用片段,並不擅長ListView適配器。來自MainActivity的片段ListView適配器notifyDataSetChanged()

我目前使用DrawerLayout其中包含片段,如你所期望的,我有一個FAB,它通過點擊將添加一個新的條目到SQLite數據庫條目,並刷新ListView(從數據庫中拉出的數據)。

我的想法是我可以在ActivityMain中獲得ListView適配器(它位於Profiles配置文件中)(因爲FAB不是片段的一部分,將根據當前片段採取不同的操作),然後調用.notifyDataSetChanged ()

的配置文件片段類

public class Profiles extends Fragment{ 

    public ProfileListAdapter adapter; 

    (...) 

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

     DBHandler db = new DBHandler(getActivity().getBaseContext()); 

     adapter = new ProfileListAdapter(getActivity().getBaseContext(), db.getAllProfiles()); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 

     ListView profileListView = (ListView)view.findViewById(R.id.profileListView); 
     profileListView.setAdapter(adapter); 
    } 

ProfilesListAdapter(從here

public class ProfileListAdapter extends ArrayAdapter<Profile> { 

    private static class ViewHolder { 
     TextView name; 
    } 

    public ProfileListAdapter(Context context, ArrayList<Profile> profileList) { 
     super(context, R.layout.profile_row, profileList); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Get the data item for this position 
     Profile profile = getItem(position); 
     // Check if an existing view is being reused, otherwise inflate the view 
     ViewHolder viewHolder; // view lookup cache stored in tag 
     if (convertView == null) { 
      // If there's no view to re-use, inflate a brand new view for row 
      viewHolder = new ViewHolder(); 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      convertView = inflater.inflate(R.layout.profile_row, parent, false); 
      viewHolder.name = (TextView) convertView.findViewById(R.id.profileListRowValue); 
      // Cache the viewHolder object inside the fresh view 
      convertView.setTag(viewHolder); 
     } else { 
      // View is being recycled, retrieve the viewHolder object from tag 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 
     // Populate the data into the template view using the data object 
     viewHolder.name.setText(profile.name); 
     // Return the completed view to render on screen 
     return convertView; 
    } 
} 

的DBHelper類

// Getting All Profiles 
public ArrayList<Profile> getAllProfiles() { 
    ArrayList<Profile> profileListItems = new ArrayList<Profile>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_PROFILE; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, new String[] {}); 
    // looping through all rows and adding to list 
    if (cursor.moveToLast()) { 
     do { 
      Profile profile = new Profile(); 
      profile.setId(Integer.parseInt(cursor.getString(0))); 
      profile.setName(cursor.getString(1)); 
      // Adding contact to list 
      profileListItems.add(profile); 
     } while (cursor.moveToPrevious()); 
    } 
    // return contact list 
    cursor.close(); 
    return profileListItems; 
} 

而這正是我試圖在FAB點擊在MainActivity控制裏面做,神奇的數字僅僅是其在0和增量由一個我開始測試分貝/ ListView的一個int更輕鬆。

db.addProfile(new Profile("Test Profile " + magicNumber)); 
       magicNumber++; 
       FragmentManager fm = getSupportFragmentManager(); 
       Profiles fragment = (Profiles) fm.findFragmentById(R.id.fragmentProfiles); 
       //I gave the fragment xml main FrameLayout an ID 
       fragment.updateListView(); 

裏面的片段類

public void updateListView(){ 
    adapter.notifyDataSetChanged(); 
} 

我也嘗試從配置片段類,我覺得是錯誤的方式來解決這個問題返回適配器。

public ProfileListAdapter getAdapter(){ 
    return adapter; 
} 

我總是

java.lang.NullPointerException: Attempt to invoke virtual method 'void layout.Profiles.updateListView()' on a null object reference 

java.lang.NullPointerException: Attempt to invoke virtual method 'com.xxxxx.xxxxx.appName.ProfileListAdapter layout.Profiles.getAdapter()' on a null object reference 

落得我noobish眼睛似乎適配器不能從片段型材類的外部訪問,不知道這是它是什麼 - 即使如此不確定如何解決這個問題。讓我知道是否需要其他代碼/ logcat。謝謝!

+0

如果你想要一個FAB動作取決於片段,那麼爲什麼你不能在每個片段佈局中添加一個FAB? – CodeCody

+0

@CodeCody我不認爲這是誠實的,因爲FAB被自動添加到ActivityMain中,並在所有片段中都可見。我使用switch語句來區分正在使用哪個片段,並相應地設置onClick操作。如果將FAB添加到每個片段是更好的方法,我會放棄! – eeffoc

回答

0

對於任何人遇到這個問題,我找到的解決方案是在這裏:notifyDataSetChange not working from custom adapter(最佳答案效果很好)。

儘管我確實實施了CodeCody的將FAB放入單獨片段的想法,但我可以安全地恢復到之前的版本,不確定哪個選項更好。

相關問題