Heres the error
I want to display the text and images from firestore.
or are there other ways how to display data from firestore database to recyclerview.
AllProducts.java
public class AllProducts extends AppCompatActivity {
ActivityAllProductsBinding binding;
MyProductsAdapter myProductsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityAllProductsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
myProductsAdapter=new MyProductsAdapter(this);
binding.productsRecycler.setAdapter(myProductsAdapter);
loadProduct();
}
private void loadProduct(){
FirebaseFirestore.getInstance()
.collection("PRODUCTS")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
List<DocumentSnapshot> dsList=queryDocumentSnapshots.getDocuments();
for (DocumentSnapshot ds:dsList){
Product product=ds.toObject(Product.class);
myProductsAdapter.add(product);
}
}
});
}
}
Related
I create a function that retrieves data from realtime firebase and Displays it to the user on android studio and when I run it, it displays null.
Here's the code :
public class ProcessSuccessed extends AppCompatActivity {
ActivityProcessSuccessedBinding binding;
DatabaseReference reference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityProcessSuccessedBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
}
private void readData(String name) {
reference = FirebaseDatabase.getInstance().getReference("users");
reference.child(name).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
#Override
public void onComplete(#NonNull Task<DataSnapshot> task) {
DataSnapshot dataSnapshot = task.getResult();
String name = String.valueOf(dataSnapshot.child("name").getValue());
binding.textView.setText(name);
}});}}
I am new to android development and I am confused about how to add admob banner to a fragment activity(I think its calling fragment).
the issues is that, Where should I write the java code for this admob banner.
if anyone can explain to me how correctly add banner in fragment android studio
into this code:
Here is the java code as well as
public WalletFragment() { // Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
FragmentWalletBinding binding;
FirebaseFirestore database;
User user;
private AdView mAdView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
;
binding = FragmentWalletBinding.inflate(inflater, container, false);
database = FirebaseFirestore.getInstance();
database.collection("users")
.document(FirebaseAuth.getInstance().getUid())
.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
user = documentSnapshot.toObject(User.class);
binding.currentCoins.setText(String.valueOf(user.getCoins()));
//binding.currentCoins.setText(user.getCoins() + "");
}
});
binding.sendRequest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(user.getCoins() > 200000) {
String uid = FirebaseAuth.getInstance().getUid();
String payPal = binding.emailBox.getText().toString();
WithdrawRequest request = new WithdrawRequest(uid, payPal, user.getName());
database
.collection("withdraws")
.document(uid)
.set(request).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Request sent successfully.", Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(getContext(), "You need more coins to get withdraw.", Toast.LENGTH_SHORT).show();
}
}
});
return binding.getRoot();
}
}```
I'm pulling the weekly data of students from firebase and adding an arraylist inside a class and I return this arraylist to Teacheractivity and I send this arraylist to spinner inside spinner class when i run emulator i can see data in spinner but when i click nothing happens. It doesn't give any error. I wrote it in separate classes to write clean code. Could this be a problem? I don't understand why this happened.
My TeacherActivity
public class TeacherDersActivity extends AppCompatActivity {
ActivityTeacherDersBinding binding;
String lesson_name;
ArrayList<String> WeekContentArraylist;
_Spinner spinner;
GetWeeks getWeeks;
static void SpinnerList(_Spinner spinner)
{
spinner.init();
}
public void init() {
Intent intent = getIntent();
lesson_name= intent.getStringExtra("teacher_lesson");
getWeeks = new GetWeeks(this);
WeekContentArraylist = getWeeks.GetWeekContent(lesson_name);
spinner = new _Spinner(TeacherDersActivity.this, WeekContentArraylist);
SpinnerList(spinner);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityTeacherDersBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
init();
}
}
GetWeeks class from which I pull data from Firebase
My GetWeeks class
public class GetWeeks {
FirebaseFirestore mFirestore;
ArrayList<String> arrayListWeekContent;
Activity _activity;
public GetWeeks(Activity activity)
{
_activity = activity;
mFirestore = FirebaseFirestore.getInstance();
arrayListWeekContent = new ArrayList<>();
}
public void init()
{
}
protected ArrayList<String> GetWeekContent(String lesson_name)
{
mFirestore.collection("Lessons")
.document(lesson_name)
.collection("Documents")
.document("Docs1")
.get()
.addOnSuccessListener(_activity, new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(#NonNull DocumentSnapshot documentsnapshots) {
arrayListWeekContent.add(documentsnapshots.getString("Week1"));
arrayListWeekContent.add(documentsnapshots.getString("Week2"));
arrayListWeekContent.add(documentsnapshots.getString("Week3"));
arrayListWeekContent.add(documentsnapshots.getString("Week4"));
arrayListWeekContent.add(documentsnapshots.getString("Week5"));
arrayListWeekContent.add(documentsnapshots.getString("meetLink"));
}
}).addOnFailureListener(_activity, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
System.out.println("GetWeeks FAIL");
}
});
return arrayListWeekContent;
}
}
My Spinner class
public class _Spinner extends Activity {
Activity activityTeacherDers;
ArrayList<String> WeekContentArraylist;
public _Spinner(Activity activityTeacherDers, ArrayList<String> WeekContentArraylist)
{
this.activityTeacherDers = activityTeacherDers;
this.WeekContentArraylist= WeekContentArraylist;
}
public void init()
{
Spinner spinner = activityTeacherDers.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activityTeacherDers,
android.R.layout.simple_spinner_item,WeekContentArraylist);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("Heloooasdasdsa dataa "); //<---- nothing happen hear
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
i got error message : recyclerview No adapter attached; skipping layout
when i try to fetch data and store to recyclerview
here is my code:
public class MainActivity extends AppCompatActivity {
private DataAdapter dataAdapter;
private ArrayList<OrderListArray> orderListArrays=new ArrayList<>();
private RecyclerView mRecyclerView;
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData();
mProgressBar=(ProgressBar)findViewById(R.id.progress_bar);
mProgressBar.setVisibility(View.VISIBLE);
mRecyclerView=(RecyclerView)findViewById(R.id.order_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public void getData(){
String userToken = Preferences.getUserToken(getBaseContext());
Log.d("[DEBUG]", "userToken: " + userToken);
Call<List<OrderListArray>> getDataCall = ApiClient.getUserService().getJson("Bearer " + userToken);
getDataCall.enqueue(new Callback<List<OrderListArray>>() {
#Override
public void onResponse(Call<List<OrderListArray>> call, Response<List<OrderListArray>> response) {
mProgressBar.setVisibility(View.GONE);
if (response.isSuccessful() && response.body()!=null) {
orderListArrays = new ArrayList<>(response.body());
dataAdapter=new DataAdapter(orderListArrays,MainActivity.this);
mRecyclerView.setAdapter(dataAdapter);
}
}
#Override
public void onFailure(Call<List<OrderListArray>> call, Throwable t) {
mProgressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,"Failure",Toast.LENGTH_SHORT).show();
}
});
}
}
use this code :
public class MainActivity extends AppCompatActivity {
private DataAdapter dataAdapter;
private ArrayList<OrderListArray> orderListArrays=new ArrayList<>();
private RecyclerView mRecyclerView;
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData();
mProgressBar=(ProgressBar)findViewById(R.id.progress_bar);
mProgressBar.setVisibility(View.VISIBLE);
mRecyclerView=(RecyclerView)findViewById(R.id.order_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//change here
dataAdapter=new DataAdapter(orderListArrays,this);
mRecyclerView.setAdapter(dataAdapter);
}
public void getData(){
String userToken = Preferences.getUserToken(getBaseContext());
Log.d("[DEBUG]", "userToken: " + userToken);
Call<List<OrderListArray>> getDataCall = ApiClient.getUserService().getJson("Bearer " + userToken);
getDataCall.enqueue(new Callback<List<OrderListArray>>() {
#Override
public void onResponse(Call<List<OrderListArray>> call, Response<List<OrderListArray>> response) {
mProgressBar.setVisibility(View.GONE);
if (response.isSuccessful() && response.body()!=null) {
orderListArrays = new ArrayList<>(response.body());
//change here
dataAdapter.notifyDataSetChanged()
}
}
#Override
public void onFailure(Call<List<OrderListArray>> call, Throwable t) {
mProgressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,"Failure",Toast.LENGTH_SHORT).show();
}
});
}
}
I guess it is because you have some operation that must run in the Main Thread, but you write your code in the background thread(onResponse method), to solve this, you can create an interface callback, and invoke your callback method in onResponse and update your UI in the main thread.
public interface MyCallBack {
void onGetResponse(Object response);
}
make your MainActivity to implement from MyCallBack
public class MainActivity extends AppCompatActivity implements MyCallBack
create a field like this in your activity
public class MainActivity extends AppCompatActivity implements MyCallBack {
private MyCallback myCallback;
and in your onCreate of your main activity initialize it
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myCallback = this;
setContentView(R.layout.activity_main);
in your onResponse do this:
public void onResponse(Call<List<OrderListArray>> call, Response<List<OrderListArray>> response) {
if (response.isSuccessful() && response.body()!=null) {
myCallback.onGetResponse(response.body());
}
}
on onGetResponse method update your ui:
#Override
public void onGetResponse(Object response) {
mProgressBar.setVisibility(View.GONE);
orderListArrays = new ArrayList<>(response.body());
dataAdapter=new DataAdapter(orderListArrays,MainActivity.this);
mRecyclerView.setAdapter(dataAdapter);
}
I am new in programing
and this is my java code below
I want the simplest way to add button to each row of listview and relate it with that row firebase child (like a vote button for example) ...
should I custom simple_list_item_1 ?? or creat a new xml file .. please answer me in detail because like I said I am new in android
tnx for help
public class Main2Activity extends ListActivity {
String us , userId ;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
ArrayList<String> listItems = new ArrayList<>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
userId = Profile.getCurrentProfile().getId() ;
us = Profile.getCurrentProfile().getName();
Toast.makeText(Main2Activity.this, "HI "+us,
Toast.LENGTH_SHORT).show();
ProfilePictureView profilePictureView;
profilePictureView = findViewById(R.id.ProfilePicture);
profilePictureView.setProfileId(userId);
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String value = childSnapshot.getValue(String.class);
listItems.add(value);
}
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
android.R.layout.simple_list_item_1 is a reference to an built-in XML layout document that is part of the Android OS which containt only one TextView, so if you want to add another view (e.g. button) you need to create your own layout.