数据仅从onStart加载,而不从片段中的OnActivityCreated加载[未解决]

时间:2019-06-22 11:19:21

标签: java android android-fragments

我有2个活动,一个是Splash活动,第二个是MainActivity

我在MainActivity中有3个片段。

用法:当用户第一次或任何时候打开我的应用程序时,我想使用适配器上的碎片和回收站视图显示碎片上的数据。

流程:首先,显然飞溅活动正在运行,然后在MainActivity中-在OnCreate中,我添加了3个片段,在片段的onCreateView和onStart中,这两种方法我都调用loadAllReminders();

问题:3个片段中的默认/第一个片段未显示首次数据。仅当我滑动到下一个片段并向后滑动时才有可能,以便调用onStart的时间并显示我的数据。

问题:但是我的问题是我写了loadAllReminders();。同样在OnActivityCreated和onStart中,那么为什么从一开始就不加载数据?

预先感谢您的帮助。

代码:   HomeFragment :(第一个/默认)

    import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tekitsolutions.remindme.Activity.AddReminderActivity;
import com.tekitsolutions.remindme.Activity.MainActivity;
import com.tekitsolutions.remindme.Activity.ReminderDetailActivity;
import com.tekitsolutions.remindme.Adapter.ReminderListAdapter;
import com.tekitsolutions.remindme.Interface.AdapterInterface;
import com.tekitsolutions.remindme.Interface.HamburgerMenuInterface;
import com.tekitsolutions.remindme.Interface.SearchListener;
import com.tekitsolutions.remindme.Model.General;
import com.tekitsolutions.remindme.Model.ParticularPayment;
import com.tekitsolutions.remindme.Model.Reminder;
import com.tekitsolutions.remindme.R;
import com.tekitsolutions.remindme.Receiver.AlarmReceiver;
import com.tekitsolutions.remindme.RestApi.ApiClient;
import com.tekitsolutions.remindme.RestApi.ApiInterface;
import com.tekitsolutions.remindme.Sql.DatabaseAdapter;
import com.tekitsolutions.remindme.Utils.RecyclerViewClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.tekitsolutions.remindme.Sql.DataBaseConstant.REMINDER_FAVORITE;
import static com.tekitsolutions.remindme.Sql.DataBaseConstant.REMINDER_ID;
import static com.tekitsolutions.remindme.Sql.DataBaseConstant.REMINDER_TYPE_ID;
import static com.tekitsolutions.remindme.Utils.CommonUtils.HOME;
import static com.tekitsolutions.remindme.Utils.CommonUtils.IS_EDIT;
import static com.tekitsolutions.remindme.Utils.CommonUtils.PICK_HOME;

public class HomeFragment extends Fragment implements AdapterInterface, HamburgerMenuInterface, Callback<String>, SearchListener {
    private static final String TAG = HomeFragment.class.getSimpleName();
    public MainActivity activity;
    View view;
    private DatabaseAdapter dbAdapter;
    private List<Reminder> listItems = new ArrayList<>();
    private ReminderListAdapter listAdapter;
    private RecyclerView listView;
    private TextView tvNoData;
    private AdapterInterface listener;
    private HamburgerMenuInterface menuInterface;
    private AlertDialog.Builder builder;
    private Reminder reminder;
    private long reminderId = 0;
    private int getFavorite = 0;

    public HomeFragment() {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home_fragment, container, false);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = view.findViewById(R.id.recycler_view);
        tvNoData = view.findViewById(R.id.tv_no_data);
        dbAdapter = new DatabaseAdapter(getContext());
        listAdapter = new ReminderListAdapter(getActivity(), listItems, HomeFragment.this, this, this);
        listView.setHasFixedSize(false);
        final LinearLayoutManager mLayoutManager;
        mLayoutManager = new LinearLayoutManager(getActivity());
        listView.setLayoutManager(mLayoutManager);
        listView.setItemAnimator(new DefaultItemAnimator());
        try {
            listView.setAdapter(listAdapter);
        } catch (Exception exp) {
            exp.printStackTrace();
        }

        listView.addOnItemTouchListener(new RecyclerViewClickListener.RecyclerTouchListener(getContext(),
                listView, new RecyclerViewClickListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {

            }

            @Override
            public void onLongClick(View view, int position) {
            }
        }

        ));
        loadAllReminders();
    }

    @Override
    public void onStart() {
        super.onStart();
        loadAllReminders();
    }

    private void loadAllReminders() {
        listItems.clear();
        List<Reminder> list = dbAdapter.getReminderById(REMINDER_TYPE_ID, HOME);
        if (list.size() > 0) {
            tvNoData.setVisibility(View.GONE);
            listItems.addAll(list);

            listAdapter.notifyDataSetChanged();
        } else {
            tvNoData.setVisibility(View.VISIBLE);
        }
    }

MainActivity:

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, AdapterInterface, HamburgerMenuInterface, SearchListener {
    private static final String TAG = MainActivity.class.getSimpleName();

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.list_view)
    RecyclerView listView;

    private String phoneNum = null, getImage = null, getUsername = null;
    private int first = 0;
    private ActionBar actionBar;
    private DatabaseAdapter dbAdapter;
    private ReminderListAdapter listAdapter;
    private List<Reminder> listItems = new ArrayList<>();
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private HamburgerMenuInterface menuInterface;
    private DrawerLayout drawer;
    private SharedPreferences profilePreference;
    private TextView navUsername, navMobile;
    private ImageView navImage;
    private View headerView;
    private NavigationView navigationView;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        CREATE_PASSCODE_ACTIVITY = 0;
        setThemeAndLoadLocale(MainActivity.this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        ButterKnife.bind(this);
        setToolbar(toolbar, getString(R.string.app_name));
        profilePreference = getSharedPreferences(PROFILE, MODE_PRIVATE);
        getDataPhoneNumber();
        tabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.view_pager);
        drawer = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        headerView = navigationView.getHeaderView(0);
        navUsername = headerView.findViewById(R.id.profile_name);
        navMobile = headerView.findViewById(R.id.mobile_number);
        navImage = headerView.findViewById(R.id.profile_image);

        if (!phoneNum.isEmpty()) {
            first = 1;
            getProfileData();
        }

        Menu menu = navigationView.getMenu();
        MenuItem tools = menu.findItem(R.id.nav_general_setting);
        SpannableString s = new SpannableString(tools.getTitle());
        s.setSpan(new TextAppearanceSpan(this, R.style.nav_header_color), 0, s.length(), 0);
        tools.setTitle(s);

        Menu menu2 = navigationView.getMenu();
        MenuItem tools2 = menu2.findItem(R.id.nav_communicate);
        SpannableString s2 = new SpannableString(tools2.getTitle());
        s2.setSpan(new TextAppearanceSpan(this, R.style.nav_header_color), 0, s2.length(), 0);
        tools2.setTitle(s2);

        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.AddFragment(new HomeFragment(), getResources().getString(R.string.fragment_home));
        adapter.AddFragment(new OfficeFragment(), getResources().getString(R.string.fragment_office));
        adapter.AddFragment(new OtherFragment(), getResources().getString(R.string.fragment_other));

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);


        listAdapter = new ReminderListAdapter(this, listItems, MainActivity.this, this, this);
        listView.setHasFixedSize(true);
        final LinearLayoutManager mLayoutManager;
        mLayoutManager = new LinearLayoutManager(this);
        listView.setLayoutManager(mLayoutManager);
        listView.setItemAnimator(new DefaultItemAnimator());
        try {
            listView.setAdapter(listAdapter);
        } catch (Exception exp) {
            exp.printStackTrace();
        }


        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, AddReminderActivity.class));
            }
        });


        dbAdapter = new DatabaseAdapter(this);

    }

    @Override
    public void onClickFavoriteById(int isFavorite, long id) {

    }

1 个答案:

答案 0 :(得分:0)

onCreateView中,您的视图仍未创建,因此您无法更改视图数据

像这样将代码移动到onActivityCreate()

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    dbAdapter = new DatabaseAdapter(getContext());
        listAdapter = new ReminderListAdapter(getActivity(), listItems, HomeFragment.this, this, this);
        listView.setHasFixedSize(false);
        final LinearLayoutManager mLayoutManager;
        mLayoutManager = new LinearLayoutManager(getActivity());
        listView.setLayoutManager(mLayoutManager);
        listView.setItemAnimator(new DefaultItemAnimator());
        try {
            listView.setAdapter(listAdapter);
        } catch (Exception exp) {
            exp.printStackTrace();
        }

        listView.addOnItemTouchListener(new RecyclerViewClickListener.RecyclerTouchListener(getContext(),
                listView, new RecyclerViewClickListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {

            }

            @Override
            public void onLongClick(View view, int position) {
            }
        }

        ));

        loadAllReminders();
}