2017-06-05 103 views
0

我從ArrayList中存儲的動態ID到動態創建和內容在viewpager不更新不斷變化的標籤 - Tablayout

我用ViewPagerOnTabSelectedListener

每個標籤,並且獲得標籤的ID被點擊並存儲它在共享首選項。

我正在使用從sharedpreference ID到片段獲取基於該ID的每個選項卡中的自定​​義列表。

現在的問題是標籤點擊是沒有得到刷新customlist,但它已經取出舊的價值觀念,它沒有得到來自sharedpreference

public class ScrollableTabsActivity extends AppCompatActivity { 
 

 
    private Toolbar toolbar; 
 
    private TabLayout tabLayout; 
 
    private ViewPager viewPager; 
 
    ConnectionClass connectionClass; 
 
    ArrayList<String> al,idal,pidal; 
 

 

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

 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
 
     setSupportActionBar(toolbar); 
 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
 

 

 
     viewPager = (ViewPager) findViewById(R.id.viewpager); 
 
     tabLayout = (TabLayout) findViewById(R.id.tabs); 
 

 

 
     connectionClass = new ConnectionClass(); 
 

 

 
     al = new ArrayList<String>(); 
 
     idal = new ArrayList<String>(); 
 
     pidal = new ArrayList<String>(); 
 

 

 
     ScrollableTabsActivity.DoLogin doLogin = new ScrollableTabsActivity.DoLogin(); 
 
     doLogin.execute(""); 
 

 

 
    } 
 

 
    private void setupTags() { 
 
     int i; 
 

 

 

 
     for(i=0;i<al.size();i++) { 
 
      /* Tag t= new Tag(); 
 
      t.setId(idal.get(i)); 
 
      tabLayout.getTabAt(i).setTag(t); 
 
*/ 
 

 
      tabLayout.getTabAt(i).setTag(idal.get(i)); 
 

 
     } 
 

 
     tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager){ 
 
      @Override 
 
      public void onTabSelected(TabLayout.Tab tab) { 
 
       super.onTabSelected(tab); 
 

 
      // CharSequence numTab = ((Tag)tab.getTag()).getId(); 
 
       CharSequence numTab = tab.getTag().toString(); 
 

 
       SharedPreferences preferences2 = getSharedPreferences("AUTHENTICATION_FILE_NAME2", Context.MODE_WORLD_WRITEABLE); 
 
       SharedPreferences.Editor editor2 = preferences2.edit(); 
 

 
       editor2.putString("tabselected",numTab.toString()); 
 
       editor2.putString("Authentication_Status", "true"); 
 
       editor2.apply(); 
 

 

 

 
      } 
 

 
     }); 
 
    } 
 
    private void setupViewPager(ViewPager viewPager) { 
 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
 
     int i; 
 
     for(i=0;i<al.size();i++){ 
 

 
     adapter.addFrag(new MainActivity(), al.get(i),idal.get(i)); 
 

 
     } 
 
     viewPager.setAdapter(adapter); 
 

 

 
    } 
 

 
    class ViewPagerAdapter extends FragmentPagerAdapter { 
 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 
 
     private final List<String> mFragmentId = new ArrayList<>(); 
 

 

 
     public ViewPagerAdapter(FragmentManager manager) { 
 
      super(manager); 
 
     } 
 

 
     @Override 
 
     public Fragment getItem(int position) { 
 

 
       return mFragmentList.get(position); 
 

 
     } 
 

 
     @Override 
 
     public int getCount() { 
 
      return mFragmentList.size(); 
 

 
     } 
 

 
     public void addFrag(Fragment fragment, String title,String id) { 
 
      mFragmentList.add(fragment); 
 
      mFragmentTitleList.add(title); 
 
      mFragmentId.add(id); 
 

 
     } 
 

 
     @Override 
 
     public CharSequence getPageTitle(int position) { 
 
      return mFragmentTitleList.get(position); 
 
     } 
 

 
     public int getItemPosition(Object object) { 
 
      return POSITION_NONE; 
 
     } 
 

 
    } 
 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     // Take appropriate action for each action item click 
 
     switch (item.getItemId()) { 
 
      case R.id.action_cart: 
 
       cart(); 
 
       return true; 
 

 
      default: 
 
       return super.onOptionsItemSelected(item); 
 
     } 
 
    } 
 

 
    /** 
 
    * Launching new activity 
 
    * */ 
 
    private void cart() { 
 
     Intent i = new Intent(ScrollableTabsActivity.this, Cart.class); 
 
     startActivity(i); 
 
    } 
 

 
    @Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
     // Inflate the menu; this adds items to the action bar if it is present. 
 
     getMenuInflater().inflate(R.menu.main, menu); 
 
     return true; 
 
    } 
 

 

 
    @Override 
 
    public void onBackPressed() { 
 
     super.onBackPressed(); 
 
     Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); 
 
     v.vibrate(500); 
 
     MainActivity m= new MainActivity(); 
 
     m.vacatelist(); 
 

 
    } 
 

 
    public class DoLogin extends AsyncTask<String,String,String> 
 
    { 
 
     String z = ""; 
 
     Boolean isSuccess = false; 
 

 

 
     @Override 
 
     protected void onPreExecute() { 
 

 
     } 
 

 
     @Override 
 
     protected void onPostExecute(String r) { 
 

 
      if(isSuccess) { 
 
       SharedPreferences preferences2 = getSharedPreferences("AUTHENTICATION_FILE_NAME2", Context.MODE_WORLD_WRITEABLE); 
 
       SharedPreferences.Editor editor2 = preferences2.edit(); 
 

 
       editor2.putString("tabselected",idal.get(0).toString()); 
 
       editor2.putString("Authentication_Status", "true"); 
 
       editor2.apply(); 
 

 
       setupViewPager(viewPager); 
 
       tabLayout.setupWithViewPager(viewPager); 
 
       setupTags(); 
 

 
      } 
 
      else { 
 
       Toast.makeText(ScrollableTabsActivity.this,r, Toast.LENGTH_SHORT).show(); 
 
      } 
 
     } 
 

 
     @Override 
 
     protected String doInBackground(String... params) { 
 

 
      try { 
 
       Connection con = connectionClass.CONN(); 
 
       if (con == null) { 
 
        z = "Error in connection with SQL server"; 
 
       } else { 
 
        String query = "SELECT ID,Category_ItemName,Pi_Category_ID FROM [dbo].[MainMenuDetail] where Pi_Category_ID=0 and Delete_category=0"; 
 
        Statement stmt = con.createStatement(); 
 
        ResultSet rs = stmt.executeQuery(query); 
 
        while (rs.next()) 
 
        { 
 
         idal.add(rs.getString("ID")); 
 
         al.add(rs.getString("Category_ItemName")); 
 
         pidal.add(rs.getString("Pi_Category_ID")); 
 

 
        } 
 
        z = "Successful"; 
 
        isSuccess=true; 
 
       } 
 
      } 
 
      catch (Exception ex) 
 
      { 
 
       isSuccess = false; 
 
       z = "Exceptions"; 
 
      } 
 

 
      return z; 
 
     } 
 

 
    } 
 
}

更新每ID

public class MainActivity extends android.support.v4.app.Fragment { 
 
\t // Log tag 
 

 
\t private static final String TAG = MainActivity.class.getSimpleName(); 
 
\t ConnectionClass connectionClass; 
 
\t // Movies json url 
 
\t String url="http://i.imgur.com/mQPXunb.png"; 
 
\t private List<Movie> movieList = new ArrayList<Movie>(); 
 
\t private ListView listView; 
 
\t private CustomListAdapter adapter; 
 
\t private List templist = new ArrayList(); 
 
\t String image_url = "http://app.dartenogroup.com/admin/img/"; 
 
\t String image_url2=""; 
 
\t NetworkImageView img; 
 
\t String restid; 
 
\t Movie movie; 
 
\t String tabselected; 
 

 

 
\t @Override 
 
\t public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
 
\t \t return inflater.inflate(R.layout.activity_main, container, false); 
 
\t } 
 
\t @Override 
 
\t public void onViewCreated(View view,Bundle savedInstanceState){ 
 
\t \t super.onViewCreated(view,savedInstanceState); 
 

 
\t \t SharedPreferences prfs = getActivity().getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_PRIVATE); 
 
\t \t restid = prfs.getString("restid", ""); 
 

 
\t \t SharedPreferences prfs2 = getActivity().getSharedPreferences("AUTHENTICATION_FILE_NAME2", Context.MODE_PRIVATE); 
 
\t \t tabselected = prfs2.getString("tabselected", ""); 
 
\t \t Toast.makeText(getActivity(), tabselected, Toast.LENGTH_SHORT).show(); 
 

 
\t \t connectionClass = new ConnectionClass(); 
 
\t \t listView = (ListView) view.findViewById(R.id.list); 
 
\t \t adapter = new CustomListAdapter(getActivity(), movieList); 
 
\t \t listView.setAdapter(adapter); 
 

 
\t \t img = (NetworkImageView) view.findViewById(R.id.thumbnail); 
 

 
\t \t // changing action bar color 
 
\t \t //getActionBar().setBackgroundDrawable(
 
\t \t // \t new ColorDrawable(Color.parseColor("#ffffff"))); 
 

 
\t \t DoLogin doLogin = new DoLogin(); 
 
\t \t doLogin.execute(""); 
 

 

 
\t \t listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
 
\t \t \t @Override 
 
\t \t \t public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
 
\t \t \t \t Intent i2 = new Intent(getActivity(), ViewProduct.class); 
 
\t \t \t \t TextView tv=(TextView)view.findViewById(R.id.itemid); 
 
\t \t \t \t i2.putExtra("value",tv.getText().toString()); 
 
\t \t \t \t startActivity(i2); 
 
\t \t \t } 
 
\t \t }); 
 

 
\t } 
 

 

 
\t /** 
 
\t * On selecting action bar icons 
 
\t * */ 
 
\t @Override 
 
\t public boolean onOptionsItemSelected(MenuItem item) { 
 
\t \t // Take appropriate action for each action item click 
 
\t \t switch (item.getItemId()) { 
 
\t \t \t case R.id.action_cart: 
 
\t \t \t \t cart(); 
 
\t \t \t \t return true; 
 

 
\t \t \t default: 
 
\t \t \t \t return super.onOptionsItemSelected(item); 
 
\t \t } 
 
\t } 
 

 
\t /** 
 
\t * Launching new activity 
 
\t * */ 
 
\t private void cart() { 
 
\t \t Intent i = new Intent(getActivity(), Cart.class); 
 
\t \t startActivity(i); 
 
\t } 
 

 
\t public class DoLogin extends AsyncTask<String,String,String> 
 
\t { 
 
\t \t String z = ""; 
 
\t \t Boolean isSuccess = false; 
 

 

 
\t \t @Override 
 
\t \t protected void onPreExecute() { 
 
\t \t \t movieList.clear(); 
 
\t \t \t templist.clear(); 
 
\t \t } 
 

 
\t \t @Override 
 
\t \t protected void onPostExecute(String r) { 
 

 

 
\t \t \t if(isSuccess) { 
 

 
\t \t \t \t updatelist(); 
 
\t \t \t \t Vibrator v = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE); 
 
\t \t \t \t v.vibrate(500); 
 
\t \t \t } 
 
\t \t \t else{ 
 
\t \t \t \t Toast.makeText(getActivity(),z,Toast.LENGTH_SHORT).show(); 
 
\t \t \t } 
 
\t \t } 
 

 

 
\t \t @Override 
 
\t \t protected String doInBackground(String... params) { 
 

 
\t \t \t \t try { 
 
\t \t \t \t \t Connection con = connectionClass.CONN(); 
 
\t \t \t \t \t if (con == null) { 
 
\t \t \t \t \t \t z = "Error in connection with SQL server"; 
 
\t \t \t \t \t } else { 
 

 
\t \t \t \t \t \t String query = "select * from [dbo].[MainMenuDetail] WHERE Delete_category = 0 AND Pi_Category_ID ='"+tabselected+"'" ; 
 
\t \t \t \t \t \t Statement stmt = con.createStatement(); 
 
\t \t \t \t \t \t ResultSet rs = stmt.executeQuery(query); 
 

 
\t \t \t \t \t \t while (rs.next()) { 
 

 
\t \t \t \t \t \t \t movie = new Movie(); 
 
\t \t \t \t \t \t \t movie.setId(rs.getString("ID")); 
 
\t \t \t \t \t \t \t movie.setItemname(rs.getString("Category_ItemName")); 
 
\t \t \t \t \t \t \t movie.setDescription(rs.getString("Category_SHortDesc")); 
 
\t \t \t \t \t \t \t movie.setPrice(rs.getString("Category_Price")); 
 
\t \t \t \t \t \t \t image_url2=rs.getString("show_Image"); 
 
\t \t \t \t \t \t \t String img= image_url+image_url2; 
 
\t \t \t \t \t \t \t movie.setThumbnailUrl(img); 
 
\t \t \t \t \t \t \t update(); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t z = "Successful"; 
 
\t \t \t \t \t \t isSuccess=true; 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t \t catch (Exception ex) 
 
\t \t \t \t { 
 
\t \t \t \t \t isSuccess = false; 
 
\t \t \t \t \t z = "Exceptions"; 
 
\t \t \t \t } 
 

 
\t \t \t return z; 
 
\t \t } 
 
\t } 
 

 
\t public void updatelist(){ 
 
\t \t new Handler(Looper.getMainLooper()).post(new Runnable() { 
 
\t \t \t @Override 
 
\t \t \t public void run() { 
 
\t \t \t \t movieList.addAll(templist); 
 
\t \t \t \t adapter.notifyDataSetChanged(); 
 
\t \t \t } 
 
\t \t }); 
 
\t } 
 
\t public void refreshlist(){ 
 

 
\t \t new Handler(Looper.getMainLooper()).post(new Runnable() { 
 
\t \t \t @Override 
 
\t \t \t public void run() { 
 
\t \t \t \t adapter.notifyDataSetChanged(); 
 
\t \t \t } 
 
\t \t }); \t } 
 

 
\t public void vacatelist(){ 
 
\t \t new Handler(Looper.getMainLooper()).post(new Runnable() { 
 
\t \t \t @Override 
 
\t \t \t public void run() { 
 
\t \t \t \t movieList.clear(); 
 
\t \t \t } 
 
\t \t }); 
 
\t } 
 

 
\t public void update(){ 
 
\t \t templist.add(movie); 
 
\t } 
 

 
}

+0

顯示一些代碼... – snachmsm

+0

編輯。放置滾動選項卡活動並在所有選項卡中使用的片段 –

回答

0

你可以試試這個

@Override 
public void onTabSelected(TabLayout.Tab tab) { 
    CharSequence numTab = tab.getTag().toString(); 
    SharedPreferences preferences2 = getSharedPreferences("AUTHENTICATION_FILE_NAME2", Context.MODE_WORLD_WRITEABLE); 
    SharedPreferences.Editor editor2 = preferences2.edit(); 

    editor2.putString("tabselected",numTab.toString()); 
    editor2.putString("Authentication_Status", "true"); 
    editor2.apply(); 

    // super called last 
    super.onTabSelected(tab); 
} 

或存儲ViewPagerAdapter adapter上的ScrollableTabsActivity頂部的 「全球」(不僅內部setupViewPager法)

ViewPagerAdapter adapter; 

private void setupViewPager(ViewPager viewPager) { 
    adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
    //rest of your code 
} 

然後

@Override 
public void onTabSelected(TabLayout.Tab tab) { 
    // your current code 
    adapter.notifyDataSetChanged(); //ViewPagerAdapter's adapter 
} 
+0

這個問題完全解決後做了 super.onTabSelected(tab); 和 adapter.notifyDataSetChanged();正如你所說的那樣, 。但它創建了一個新的GUI問題: 刷新到新選項卡後,選定的選項卡不變粗體,但仍然在選項卡選擇gui工作正常 –