I am new to android dev. I am trying to setup a notification, but it is not working, and I am not sure why.
public class MainActivity extends AppCompatActivity {
public static final String SHARED_PREF="SHARED_PREFS";
public static final String KEY="KEY_TEXT";
private EditText editText;
private Button button;
private Button button2;
private TextView final_text;
private TextView editText3;
private int val_two;
private String food_list;
private String id;
private NotificationChannel channel;
#Override
protected void onCreate(Bundle savedInstanceState) {
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
channel = new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "notifChan2", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("A calorie notification");
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val = editText.getText().toString();
String food = editText3.getText().toString();
food_list.concat(food);
val_two = val_two + Integer.parseInt(val);
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF,0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(KEY, val_two);
editor.apply();
String finalText = "You current calories " + val_two ;
if(val_two>1000){
DisplayNotification();
}
final_text.setText(finalText);
callupdatewidget() function this will print the current calories into the wdiget
callUpdateWidget(MainActivity.this,finalText);
}
});
}
private void DisplayNotification() {
NotificationCompat.Builder notify = new NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_menu_agenda)
.setContentTitle("Congrats!")
.setContentText("You have reached your calorie goal of 1000!");
NotificationManagerCompat notificationMgr = NotificationManagerCompat.from((this));
notificationMgr.notify(1, notify.build());
}
}
I have tried googling many things, as well as many other stack overflow answers, but have not been able to come up with anything.
Thanks, and any help is greatly appreciated.
Related
It has been awhile so I am struggling with fragments a bit. I am working on demos right now to get familiar again. What I am doing is in the main activity, I have a button to click which opens a fragment to enter information like name, address, etc. For some reason, the main activity doesn't go away and shows over the fragment. What am I doing wrong???
Note: I have not wired the cancel button yet to go back to Main Activity without doing anything.
Main Activity:
public class MainActivity extends AppCompatActivity {
public static User mUser = new User();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button requestServiceButton = findViewById(R.id.servicesButton);
requestServiceButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
NewUser fragment = new NewUser();
transaction.replace(R.id.fragmentHolder, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
NewUser Class:
public class NewUser extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.dialog_new_user, container, false);
final EditText fName = v.findViewById(R.id.firstNameEditText);
final EditText lName = v.findViewById(R.id.lastNameEditText);
final EditText address = v.findViewById(R.id.editTextAddress);
final EditText city = v.findViewById(R.id.cityEditText);
final EditText email = v.findViewById(R.id.editTextEmailAddress);
final EditText password = v.findViewById(R.id.editTextPassword);
final EditText phoneNumber = v.findViewById(R.id.editTextPhone);
final EditText username = v.findViewById(R.id.usernameEditText);
final EditText zipCode = v.findViewById(R.id.zipEditText);
Button okButton = v.findViewById(R.id.btnOK);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.mUser.setFirstName(fName.getText().toString());
MainActivity.mUser.setLastName(lName.getText().toString());
MainActivity.mUser.setAddress(address.getText().toString());
MainActivity.mUser.setCity(city.getText().toString());
MainActivity.mUser.setEmailAddress(email.getText().toString());
MainActivity.mUser.setPassword(password.getText().toString());
MainActivity.mUser.setPhoneNumber(phoneNumber.getText().toString());
MainActivity.mUser.setUsername(username.getText().toString());
MainActivity.mUser.setZipCode(zipCode.getText().toString());
}
});
Button cancelButton = v.findViewById(R.id.btnCancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return v;
}
I am trying to retrieve all the products from the product structure in real time firebase. When I run the app crashes but when I comment "Product product = childSnapShot.getValue(Product.class);" it doesn't retrieve any data. Also when I try to retrieve from the Users structure it works.
Why does that happen?
public class User_products_list extends AppCompatActivity {
private ListView listView;
private FirebaseDatabase database;
private DatabaseReference myRef;
//Define Array list
private ArrayList<String> arrayList = new ArrayList<>();
// Define a ListView to display the data
private ListView listViewdata;
// Define an ArrayAdapter for the list
private ArrayAdapter<String> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_data);
myRef = FirebaseDatabase.getInstance().getReference();
myRef = myRef.child("Products");
listViewdata = (ListView) findViewById(R.id.list_View);
// Set the ArrayAdapter to the ListView
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
listViewdata.setAdapter(arrayAdapter);
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for(DataSnapshot childSnapShot : snapshot.getChildren()) {
Product product = childSnapShot.getValue(Product.class);
arrayList.add(product.getName());
arrayList.add(product.getDescription());
arrayAdapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
i am a new comer in android development and facing some problems using firebase ui.
i wanted to retrieve data from my firebase database and show it on my recyclerView.
but i am constantly getting an error while making FirebaseRecyclerAdapter.
Here is My Code Sample:
public class UsersActivity extends AppCompatActivity {
private Toolbar userToolbar;
private RecyclerView usersList;
private DatabaseReference mUsersDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
userToolbar = (Toolbar) findViewById(R.id.users_toolbar);
setSupportActionBar(userToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Users");
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
usersList = (RecyclerView) findViewById(R.id.users_list);
usersList.setHasFixedSize(true);
usersList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_user_list_layout,
UsersViewHolder.class,
mUsersDatabase
) {
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull Users model) {
holder.setName(model.getName());
}
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
usersList.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder
{
View mView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name)
{
TextView userNameView = (TextView) mView.findViewById(R.id.single_user_name);
userNameView.setText(name);
}
}
}
The Error i'm getting is this:
please help!!
Sorry wanted to comment but i ain't got enough points to do so.
Try this first, if it doesnt work try using FirebaseRecyclerOptions
(The latest version (3.x) of FirebaseUI implements a different method ofinitializing a FirebaseRecyclerAdapter than previous versions.)
Make your " firebaseRecyclerAdapter " variable global, so you would have something like this.
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_user_list_layout,
UsersViewHolder.class,
mUsersDatabase
)
FirebaseRecyclerAdapter - populateViewHolder is not populating the data for the first time it runs but when I closed the app and opened it, the data is binded in RecyclerView View Holder.
I am not getting why data is not populating for first time, it is showing blank screen
Here's my MainActivity
mDatabase = FirebaseDatabase.getInstance().getReference();
// [END create_database_reference]
mRecycler = (RecyclerView) findViewById(R.id.recycler_view);
mRecycler.setHasFixedSize(true);
setSupportActionBar(toolbar);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mRecycler.setLayoutManager(layoutManager);
// Set up FirebaseRecyclerAdapter with the Query
//Query postsQuery = mDatabase.child("EN").child("Courses") ;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("EN").child("Courses");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(MainActivity.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.toString(), Toast.LENGTH_SHORT).show();
}
});
Log.d("ref", String.valueOf(ref));
// Log.d("Query", String.valueOf(postsQuery));
mAdapter = new FirebaseRecyclerAdapter<Course, CourseViewHolder>(Course.class, R.layout.item_home,
CourseViewHolder.class, ref) {
#Override
protected void populateViewHolder(final CourseViewHolder viewHolder, final Course model, final int position) {
viewHolder.bindToPost(model);
}
};
mRecycler.setAdapter(mAdapter);
And the ViewHolder
public class CourseViewHolder extends RecyclerView.ViewHolder {
public TextView titleView;
public TextView descriptionView;
public ImageView IconView;
public TextView lessonCountView;
public CourseViewHolder(View itemView) {
super(itemView);
titleView = (TextView) itemView.findViewById(R.id.title);
descriptionView = (TextView) itemView.findViewById(R.id.description);
//IconView = (ImageView) itemView.findViewById(R.id.star);
}
public void bindToPost(Course post) {
// Log.d("Post", String.valueOf(post));
titleView.setText(post.getName());
descriptionView.setText(post.getDescription());
}
}
My Model Pojo
public class Course {
public String description;
public String title;
public Course() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public Course(String title, String description) {
// this.description = author;
this.title = title;
this.description =description;
}
public String getName() {
return title;
}
public String getDescription() {
return description;
}
}
Found the Solution.
The Problem is RecyclerView had a height of wrap_content. So Please make sure your RecyclerView height is set to match_parent. This will fix this issue.
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
If you don't want to use match_parent you shouldn't call mRecycler.setHasFixedSize(true);
It says unable to resolve constructor firebaseListAdapter.
This is my MainActivity.
Error on line:
listAdapter = new FirebaseListAdapter(this, chat.class, android.R.layout.two_line_list_item, mRef)
public class MainActivity extends AppCompatActivity {
private DatabaseReference mRef;
private FirebaseListAdapter < chat > listAdapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lv = (ListView) findViewById(R.id.lv);
mRef = FirebaseDatabase.getInstance().getReference();
listAdapter = new FirebaseListAdapter < chat > (this, chat.class, android.R.layout.two_line_list_item, mRef) {
#Override
protected void populateView(View view, chat chatMessage, int position) {
((TextView) view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
((TextView) view.findViewById(android.R.id.text2)).setText(chatMessage.getMsg());
}
};
lv.setAdapter(listAdapter);
}
}
This is my Chat.class
public class chat {
String name, msg;
public chat() {}
public chat(String name, String msg) {
this.name = name;
this.msg = msg;
}
public String getName() {
return name;
}
public String getMsg() {
return msg;
}
}
I got the answer. It was just a small glitch.
I had imported;
com.firebase.ui.FirebaseListAdapter
The correct import package should be;
com.firebase.ui.database.FirebaseListAdapter
That's it and the problem is solved.
add:
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.firebase:firebase-ui:0.1.0'
and then
import com.firebase.ui.FirebaseListAdapter;