I hope this is not considered a duplicate post since i requested some help in here, which helped me to understand the problem but i could not a solution and how to achieve it.
Simply i am having a timing conflict in my code, between getting the view, for the ListView and CardView in the tabs, and the retrieval of the data form firebase to feed it into the views.
When i comment this line; holder.name.setText(mPlaces[position]); and run the app, the view get built fine, with the hint instead of the data being displayed in the list, and then when i go and and uncomment that line, and rerun the app, then i get the data extracted from firebase passed into the list.
So i searched online on how to achieve this by executing it in the UIthread as i understood from the previous question but i couldnt apply any of the methods, thus i would appreciate it if someone could guid me on how to achieve this in my code ?
MainActivity;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
TextView textView;
ArrayList<Long> addressList = new ArrayList<Long>();
static String[] arrayLocations;
private static String[] mPlaces;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new ListContentFragment(), "List");
adapter.addFragment(new CardContentFragment(), "Card");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
ListContentFragment;
public class ListContentFragment extends android.support.v4.app.Fragment {
private static String[] mPlaces;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycler_view, container, false);
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
Firebase ref = new Firebase("https://xxxxxxx.firebaseio.com/xxxxxxxxx");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// LENGTH = (int) dataSnapshot.getChildrenCount(); }
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
return recyclerView;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.item_list, parent, false));
name = (TextView) itemView.findViewById(R.id.list_title);
}
}
/**
* Adapter to display recycler view.
*/
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of List in RecyclerView.
private static final int LENGTH = 3;
public ContentAdapter(Context context) {
Resources resources = context.getResources();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ArrayList<Long> addressList = new ArrayList<Long>();
Firebase ref = new Firebase("https://xxxxxxx.firebaseio.com/xxxxxxxxx");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Map<Long, Long> allADR = (HashMap<Long, Long>) snapshot.getValue();
if (allADR != null) {
Collection<Long> BlkADR = allADR.values();
addressList.addAll(BlkADR);
Log.i("TAG", addressList.toString());
mPlaces = addressList.toArray(new String[addressList.size()]);
Log.i("TAG", String.valueOf(mPlaces.length) );
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
holder.name.setText(mPlaces[position]);
}
@Override
public int getItemCount() {
return LENGTH;
}
}
}