Firestore data retrieval not "instantaneous" - android-studio

I am working with Android Studio and I want to retrieve data from Firestore and use the document data as information for individual list items in the RecyclerView container.
Basically the components are an Activity consisting of a Fragment which houses the RecyclerView. This is the code I used in my Fragment to obtain and display the data (the one in the code is a sample):
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_transport_requests_client, container, false);
firebaseAuth = FirebaseAuth.getInstance();
db = FirebaseFirestore.getInstance();
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
swipeContainer = view.findViewById(R.id.swipeContainer);
final ArrayList<String> list = new ArrayList<>();
DocumentReference documentReference = db.collection("transportRequests").document("T_client#eldereach.com_23 May 20 03:19 PM_");
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
assert document != null;
list.add((String) document.getData().get("email"));
}
}
});
listAdapter = new TransportRequestsListAdapter(list);
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
The issue here isn't that the code doesn't work, I think the retrieval works, however, nothing is displayed when the Fragment is launched. The data is only displayed when I click the Fragment. This doesn't occur when I just add random strings instead of data from the document, e.g. list.add("random data"). Any idea what is the issue here? Thanks in advance!

Related

Get recycler view items in spinner in another activity

I use retrofit2 to get a category list with a model class and recyclerview adapter to get category items. In another activity I have a spinner to show category items.
How can I get category items in another activity and show in spinner?
Here is my code:
public class CatAdapter extends RecyclerView.Adapter<CatAdapter.MyViewHolder> {
private Context context;
private List<Category> cats;
public CatAdapter(Context context, List<Category> cats) {
this.context = context;
this.cats = cats;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.cats_list, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.cat_title.setText(cats.get(position).getTitle());
holder.cat_content.setText(cats.get(position).getContent());
holder.cat_qcount.setText(cats.get(position).getQcount());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context , CatPosts.class);
intent.putExtra("catid",cats.get(position).getCatid());
intent.putExtra("cattitle",cats.get(position).getTitle());
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return cats.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView cat_title,cat_content, cat_qcount;
public MyViewHolder(View itemView) {
super(itemView);
cat_title = itemView.findViewById(R.id.cat_title);
cat_content = itemView.findViewById(R.id.cat_content);
cat_qcount = itemView.findViewById(R.id.cat_qcount);
}
}
}
I don't want to get items with intent because there is no click action when need to cat items.
I have an activity for sending new posts. In this activity there is a spinner that shows cat items and user select a category.
The intent in adapter sends data to CatPosts when an item is clicked. I want to get cat list when the new Post activity is shown.
You were doing fine, now in the other activity, on your onCreate() method, do this:
Intent intent = getIntent();
catId = intent.getStringExtra("catid");
catTitle = intent.getStringExtra("cattitle");
String[] x = new String[]{catId,catTitle};
ArrayAdapter<String> adapterX = new ArrayAdapter<>(this, android.R.layout.your_spinner_in_xml, x);
spinner.setAdapter(adapterX);
If i get question you want access to List in two activity.
If so then you have several way
1 : Store To local Database or SharedPreferences
2 : use static variables(easy way)
When you get list of category from retrofit response, store it to static List like below
public static List<Category> cats;
Then in OnResponse()
cats = response.body().getCats();
Now you access cats in anOther activtiy.

How to create when i click 1st item in arrayList1 will open page with 1st item in arrayList2

When I click first or second item from 1st array list I get first item from array list two, but I need if i click second item on array list 1 I get shown second item from list two
public class SecondActivity extends AppCompatActivity {
ListView listView;
ArrayList<String> 1stArrayList;
ArrayList<String> 2ndArrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
listView = (ListView) findViewById(R.id.listView);
1stArrayList = new ArrayList<>();
1stArrayList .add("A");
1stArrayList .add("B");
1stArrayList .add("C");
2ndArrayList = new ArrayList<>();
2ndArrayList.add("1");
2ndArrayList.add("2");
2ndArrayList.add("3");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item, 1stArrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String 2ndArrayList= listView.getItemAtPosition(position).toString();
SharedPreferences settings = getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("2ndArrayList ", 2ndArrayList );
editor.apply();
Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);
startActivity(intent);
}
});
}
}
A possible solution would be getting the value directly from the array using the position of the view clicked. Like the following:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String secListItem = secArrayList.get(position);
SharedPreferences settings = getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("2ndArrayList", secListItem );
editor.apply();
Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);
startActivity(intent);
}
});
But you have to be sure that both the arrays will have the same size or you may have an IndexOutOfBoundsException.
Another solution would be using some Map with the values from arraylist1 and arraylist2, implementing a custom adapter that extends BaseAdapter, populating the views with the values from the first array with getView() and using getItem() to return the values from the second list, but that's harder to do.
Also if you don't mean to persist the item and only transfer them between activities, you can use the Intent to do it. In this way:
Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);
Intent.putExtra("2ndArrayList", secListItem);
startActivity(intent);
And recover in the third activity with something like:
String secArrayItem = "";
Bundle extras = getIntent().getExtras();
if (extras != null) secArrayItem = extras.getString("2ndArrayList");

"java.lang.IllegalArgumentException: view must not be null" Program Closes when navigated

The app closes whenever I navigate to ProfileActivity.
FATAL EXCEPTION: main Process: hfad.com.hallofmemesprototype, PID:
19092 java.lang.RuntimeException: Unable to start activity
ComponentInfo{hfad.com.hallofmemesprototype/hfad.com.hallofmemesprototype.Profile.ProfileActivity}:
java.lang.IllegalArgumentException: view must not be null
Here's the "ProfileActivity" code.
public class ProfileActivity extends AppCompatActivity {
private static final int ACTIVITY_NUM = 3;
private Context mContext = ProfileActivity.this;
private ProgressBar mProgressBar;
private ImageView profilePhoto;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
setupBottomNavigationView();
setupToolbar();
setupActivityWidgets();
setProfileImage();
}
private void setProfileImage(){
String imgURL = "www.androidzone.org/wp-content/uploads/2013/02/android-musical2.jpg";
UniversalImageLoader.setImage( imgURL, profilePhoto, mProgressBar, "https://");
}
private void setupActivityWidgets(){
mProgressBar = findViewById(R.id.profileProgressBar);
mProgressBar.setVisibility(View.GONE);
profilePhoto = findViewById(R.id.profile_photo);
}
private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.profileToolBar);
setSupportActionBar(toolbar);
ImageView profileMenu = findViewById(R.id.profileMenu);
profileMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext, AccountSettingsActivity.class);
startActivity(intent);
}
});
}
/**
* BottomNavigationView setup
*/
private void setupBottomNavigationView() {
BottomNavigationViewEx bottomNavigationViewEx = findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.bottomNavigationView(bottomNavigationViewEx);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationViewEx);
Menu menu = bottomNavigationViewEx.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
}
You must check that all the views that you are getting using findViewById() really exist into the activity_profile.xml layout.
One or more views doesn´t really exist, and you have a null value gettin the reference.
findViewById(R.id.profileProgressBar);
findViewById(R.id.profile_photo);
findViewById(R.id.profileToolBar);
findViewById(R.id.profileMenu);
findViewById(R.id.bottomNavViewBar);
initially you are not getting any error tryin to find the references of this views because they are really exist but in another layout but not in activity_profile.xml that you are loading via setContentView() in your Activity.
This type of exception rise at the time of the wrong variable declaration and
initialization.
Try this
replace your weighs declare and initialize val with var.
If you have to use java
final TextView helloTextView = (TextView)findViewById(R.id.text_view_id);
For Kotlin
val helloTextView = findViewById(R.id.text_view_id) as TextView
you can refer this link for more details

Add Viewpager in fragment

Hello I created my app using fragments and I need to implement a viewpager in one of them (In that viewpager user can swipe between different information (about using the application that I am currently developing. I assume there will be 3 to 5 views with text and details)). Bellow is the code to current fragment. Can you guys give me an idea what would be the best way to implement Viewpager in fragment with let's say 3 views and every view will have like 3 or 4 things on it(Imageview,Textview etc). I have already implemented Viewpager in other activity but this is my first time I'm implementing it inside a fragment. What's the logic about it. I tried using google but it sound to complicated.
public class AboutSectionFragment extends Fragment {
// The onCreateView method is called when Fragment should create its View object hierarchy,
// either dynamically or via XML layout inflation.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about_section, parent, false);
// Defines the xml file for the fragment
return rootView ;
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
}
I have used ViewPager inside a fragment and its working fine for me.
Fragment class
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_content, container, false);
fragments = getFragmentsList();
pagerAdapter = new HomeViewPagerAdapter(getChildFragmentManager(),fragments);
ViewPager = (ViewPager) view.findViewById(R.id.Home_View_Pager);
ViewPager.setAdapter(pagerAdapter);
private ArrayList<Fragment> getFragmentsList(){
fragments = new ArrayList<>();
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
return fragments;
}
ViewPager Fragment Class
public class HomeViewPagerFragment extends Fragment {
public static HomeViewPagerFragment getInstance(){
HomeViewPagerFragment fragment = new HomeViewPagerFragment();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_pager_fragment_layout,container,false);
return view;
}
}

SherlockFragmentActivity with SherlockListFragment

I have 3 tabs and all of them has lists inside(different ones of course). When i click on tab I see the proper result(result which i would like to get).
I know how to get the clicked item info and make request to database about that item and get result and create new intent and put that result in it as a extra. BUT i don't know how I can display my result inside the same fragment without creating new intent. Because when i create new intent and show result in new intent my tabs obviously disappears, but i need them always to be there.
My main activity:
public class MainActivity extends SherlockFragmentActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
Tab tab = actionBar.newTab()
.setText("Dictionary")
.setTabListener(new DictionaryFragment())
.setIcon(R.drawable.android);
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Play")
.setTabListener(new PlayFragment())
.setIcon(R.drawable.apple);
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Manage")
.setTabListener(new ManageFragment())
.setIcon(R.drawable.apple);
actionBar.addTab(tab);
}
}
One of my fragments:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
built_in_DB_setup dbOpenHelper = new built_in_DB_setup(getActivity().getBaseContext(), DB_NAME);
database = dbOpenHelper.openDataBase();
fillFreinds();
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, friends);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
And here is the item click listener inside this class
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String a=getData(((TextView) v).getText().toString());
Intent i = new Intent("com.example.fragmenttest.VIEWWORD");
i.putExtra("result", a);
startActivity(i);
}
Ones I have String a which i have got from onListItemClick how can I replace current Tab data with this string?

Resources