我的應用程序總是使用相同的菜單,所以我定義了一個BaseActivity,並且每個活動現在都擴展了BaseActivity。從基本活動中獲取片段內容
的東西是菜單的一個元素被用於同步系統。所以,根據我在活動中,它應該刷新不同的元素:列表視圖,地圖等..
我能知道哪些活動是使用上onStartMethod SharedPreference頂部。
但是從我BaseActivity,我無法檢索它們內部子活動和片段。
我:
public class FragmentTabsPdv extends BaseActivity
裏面FragmentTabsPdv,我有一個福爾摩斯標籤,用2片段:
ListPdvTabFragment extends SherlockFragment
MapPdvTabFragment extends SherlockFragment
裏面ListPdvTabFragment,我想刷新我的ListView,但我不知道如何從BaseActivity獲得它。
當然,我的菜單是定義BaseActivity:
的同步菜單啓動的AsyncTask誰從服務器獲取新內容和更新sqlite的分貝。 和過程onPostExecute結束,我需要更新我的元素:
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (lTiendas == null){
Toast.makeText(ctx, "Hubo un error al sincronizar los datos, intentalo más tarde", Toast.LENGTH_LONG).show();
}
if (BaseActivity.refreshItem != null) {
String frontActivity = prefs.getString("FrontActivity", "");
if (!frontActivity.isEmpty()){
if (frontActivity.equals("ListPdvTabFragment")){
// get List to Refresh
}
}
BaseActivity.completeRefresh();
} else {
Intent i = new Intent(ctx, DashBoard.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
if ((SplashScreen) ctx != null)
((SplashScreen) ctx).finish();
}
}
選項卡活動:
public class FragmentTabsPdv extends BaseActivity {
private ArrayList<Pdv> listaPdv;
private Usuario mUser;
private SharedPreferences prefs;
private Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
DatabaseHandlerTienda db = new DatabaseHandlerTienda(this);
// listaPdv = (ArrayList<Pdv>) db.getAllTiendas();
String jsonUser = prefs.getString("usuario", "");
Gson gson = new Gson();
mUser = gson.fromJson(jsonUser, Usuario.class);
listaPdv = db.getAllTiendas();
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Lista");
tab1.setIcon(R.drawable.ic_action_view_as_list);
ActionBar.Tab tab2 = getSupportActionBar().newTab();
tab2.setText("Mapa");
tab2.setIcon(R.drawable.ic_action_location_map);
// create the two fragments we want to use for display content
tab1.setTabListener(new TabListener<ListPdvTabFragment>(this, "lista", ListPdvTabFragment.class));
tab2.setTabListener(new TabListener<MapPdvTabFragment>(this, "mapa", MapPdvTabFragment.class));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
public ArrayList<Pdv> getListaPdv() {
return listaPdv;
}
public void setListaPdv(ArrayList<Pdv> listaPdv) {
this.listaPdv = listaPdv;
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/**
* Constructor used each time a new tab is created.
*
* @param activity
* The host Activity, used to instantiate the fragment
* @param tag
* The identifier tag for the fragment
* @param clz
* The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
// ft.attach(mFragment);
ft.show(mFragment);
}
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
// ft.detach(mFragment);
ft.hide(mFragment); // Optimize map display
}
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.menu_sync:
DatabaseHandlerTienda db = new DatabaseHandlerTienda(this);
ArrayList<Pdv> lTiendas = db.getAllTiendas();
// adbTienda.notifyDataSetChanged();
// Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void refresh() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView iv = (ImageView) inflater.inflate(R.layout.refresh_action_view, null);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.refresh);
rotation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotation);
refreshItem.setActionView(iv);
// Sincronize:
SyncData sd = new SyncData(ctx);
sd.execute();
// GET LISTVIEW FRAGMENT AND UPDATE VIEW
}
}
難道你不只是創造一個getter? –
你能解釋一下你的想法嗎?我可以得到listView,如果我可以得到片段的實例 –
我不知道我確切地知道你想要什麼,但是,創建你的片段的實例並創建一個getter方法來返回變量? –