0

所以基本上,我在ViewPagerTabLayout中有兩個選項卡,用於在選項卡視圖中存儲兩個片段。事實上,我在recycler view中使用card view,而card view就像Like Button。基本上,當用戶點擊按鈕時,它應該出現在下一個選項卡上 - 但它不會。只有當用戶離開整個頁面並返回到該頁面並選擇該選項卡時,纔會顯示所選的card view。我檢查了我的數據庫,它正在做它應該是,但它是不更新的recycler view爲什麼當我進入下一個選項卡時,我的recyclerview不會更新?

這是在card view我的liking內容代碼:

IN CUSTOM ADAPTER CLASS 
holder.likebutton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Database database = new Database(fragment.getContext()); 
       database.likeContent(userID, contentID, name); 
      } 
     }); 

DATABASE CLASS 
public void likeContent(int userID, int contentID, String name){ 
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(USER_ID, userID); 
    contentValues.put(CONTENT_ID, contentID); 
    contentValues.put(NAME, name); 
    sqLiteDatabase.insert(DB_TABLE_NAME, null, contentValues); 
} 

BACKGROUND CLASS 
public void showFavourites(final ArrayList<Integer> likes){ 
     theURL = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 
     requestQueue = Volley.newRequestQueue(fragment.getActivity()); 
     JsonArrayRequest jsonArrayRequest = new JsonArrayRequest 
       (theURL, new Response.Listener<JSONArray>(){ 
        @Override 
        public void onResponse(JSONArray jsonArray) { 
         try { 
          for (int i = 0; i < jsonArray.length(); i++) { 
           ... 
           adapter.notifyDataSetChanged(); 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener(){ 

        @Override 
        public void onErrorResponse(VolleyError volleyError) { 

        } 
       } 
       ); 
     requestQueue.add(jsonArrayRequest); 
    } 

SECOND TAB CLASS 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.likes, container, false); 
     recyclerView = (RecyclerView) rootView.findViewById(R.id.rlikes); 
     recyclerView.setHasFixedSize(true); 
     list = new ArrayList<UserInfo>(); 
     adapter = new CAdapter(list, SecondTab.this); 
     LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); 
     linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     recyclerView.setLayoutManager(linearLayoutManager); 
     recyclerView.setAdapter(adapter); 
     conditionNames = new ArrayList<>(); 
     Background = new Background(SecondTab.this, list, adapter); 
     inputs = new ArrayList<>(); 
     populate(); 
     return rootView; 
    } 

    public void populate(){ 
     background.showFavourites(inputs); 
    } 

所以後,我點擊它在database更新Custom Adapter Classlike按鈕,但它不會在第二個選項卡更新recyclerview

編輯:我的尋呼機適配器:

public class TabAdapter extends FragmentPagerAdapter{ 

    String tabNames[] = new String[] {"Current", "Likes"}; 
    Context context; 

    public PodcastGUITabAdapter(FragmentManager fragmentManager, Context context){ 
     super(fragmentManager); 
     this.context = context; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position){ 
      case 0: 
       return new CurrentTab(); 
      case 1: 
       return new LikesTab(); 
      default: 
       return null; 
     } 
    } 

    @Override 
    public int getCount() { 
     return tabNames.length; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return tabNames[position]; 
    } 

} 

我缺少什麼?

+0

後尋呼機適配器代碼。很可能你需要在getItem函數中返回一個新的fragment實例。 –

+0

@vipulmittal,我編輯了我的問題以包含'getItem()'函數 –

+0

將與從onCreate填充列表相關的代碼放入onResume。此外,如果這不工作檢查http://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible-in-viewpager –

回答

0

我有同樣的問題,並使用廣播解決它。

我在兩個標籤中註冊了BroadcastReceiver

然後,更新數據庫後,我發送一個廣播,它被兩個選項卡和onReceive方法接收,我將數據重新加載到回收站視圖。

如何使用廣播(所有代碼進入YourFragementClass):

首先需要聲明廣播:

public static final String BROADCAST_NAME = "com.yourapp.broadcast_name"; 

發送廣播:

getContext().sendBroadcast(new Intent(BROADCAST_NAME)); 

註冊廣播接收機:

private static IntentFilter broadcastIntentFilter = new IntentFilter(BROADCAST_NAME); 
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
     @Override public void onReceive(Context context, Intent intent) { 
      YourFragmentClass.this.refreshRecyclerAdapter(); 
     } 
    }; 
private boolean broadcastReceiverRegistered = false; 

還需要註銷並在onDestroy()onResume()重新註冊接收器:

@Override 
    public void onResume() { 
     super.onResume(); 
     getActivity().registerReceiver(broadcastReceiver, broadcastIntentFilter); 
     broadcastReceiverRegistered = true; 
    } 

    @Override 
    public void onDestroy() { 
     if(broadcastReceiverRegistered) { 
      getActivity().unregisterReceiver(broadcastReceiver); 
      broadcastReceiverRegistered = false; 
     } 
     super.onDestroy(); 
    } 

你應該實現YourFragmentClass.this.refreshRecyclerAdapter();方法是這樣的:

private void refreshRecyclerAdapter() { 
     List<Items> items = getYourItems(); 
     ItemsRecyclerAdapter adapter = new ItemsRecyclerAdapter(getActivity(), items); 
     mRecyclerView.setAdapter(adapter); 
    } 
+0

我該如何使用它? –

相關問題