Can't update the value of textview by settext - android

My settext do not update after I have updated it's value from database. But if I logout and login into my app settext is refreshed.I get the value of settext by SharedPreferences.
public class MySettingsActivity extends AppCompatActivity {
private TextView settingsDisplayName;
private TextView settingsDisplayStatus;
private User user = SharedPrefManager.getInstance(this).getUser();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_settings);
settingsDisplayName = (TextView) findViewById (R.id.settings_username);
settingsDisplayStatus = (TextView) findViewById(R.id.settings_status);
settingsChangeUsername.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String old_name = settingsDisplayName.getText().toString();
Intent usernameIntent = new Intent(MySettingsActivity.this, UsernameActivity.class);
usernameIntent.putExtra("user_name",old_name );
startActivity(usernameIntent);
}
});
}
#Override
protected void onStart() {
MySettingsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
settingsDisplayName.setText(user.getName());
settingsDisplayStatus.setText(user.getEmail());
}
});
super.onStart();
}
}

Related

how to write a variable from a singleton in firebase

There is a program where I can change a variable using a singleton. Created 2 activites, where users can change this variable. How to write the variable that is being changed to the Firebase database. I tried to use the addValueEventListener (new ValueEventListener () method.
Where do I use the firebase methods? In the main class or in singleton? And how to write a variable?
main activity
public class MainActivity extends AppCompatActivity {
private BubbleWrap bubbleWrap;
private TextView txtintent;
final int REQUEST_CODE_661_1 = 1;
private int bbbbb;
private TextView txt;
private String bubul;
FirebaseDatabase database=FirebaseDatabase.getInstance();
final DatabaseReference AllFacebase =database.getReference("всего").child("йй");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=findViewById(R.id.textView);
bubbleWrap=BubbleWrap.getInstance();
setupAddMoreButton();
setupPopActivityButton();
setupCocActivityButton();
txtintent=(TextView)findViewById(R.id.txtintent);
AllFacebase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
bubul=String.valueOf(dataSnapshot.getValue());
txtintent.setText(bubul);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
protected void onResume() {
super.onResume();
updateUI();
}
private void setupAddMoreButton(){
Button btn=findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bubbleWrap.addMoreBubbles();
updateUI();
}
});
}
private void setupPopActivityButton(){
Button btn=findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
private void setupCocActivityButton(){
Button btn=findViewById(R.id.buttoncoc);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
private void updateUI(){
txtintent.setText(String.format(Locale.getDefault(),"%d",
bubbleWrap.getNumBubbles()));
}
}
second activity
activity, where I change the variable, I want to write to Firebase
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop);
bubbleWrap=BubbleWrap.getInstance();
final TextView txt=findViewById(R.id.textView2);
setupPopButton();
updateUI();
Intent intent=new Intent();
intent.putExtra("bubles", bubbleWrap.getNumBubbles());
}
private void setupPopButton(){
Button btn=findViewById(R.id.cocbtb);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bubbleWrap.popBubble();
updateUI();
}
});
}
private void updateUI(){
TextView txt=findViewById(R.id.textView2);
txt.setText(String.format(Locale.getDefault(),
"Bubbles left: %d",
bubbleWrap.getNumBubbles()));
}
}
Singleton
public class BubbleWrap {
private static final int ADD_MORE_BUBBLES = 10;
private int numBubbles;
private static BubbleWrap instance;
private BubbleWrap(){
}
public static BubbleWrap getInstance(){
if(instance==null){
instance=new BubbleWrap();
}
return instance;
}
public int getNumBubbles() {
return numBubbles;
}
public void addMoreBubbles(){
numBubbles+=ADD_MORE_BUBBLES;
}
public void popBubble() {
numBubbles--;
}
public void saveintent(){
}
}

Failed to convert value of type java.util.HashMap to String and app crashes

public class MainActivity extends AppCompatActivity {
//points to the root node of the database
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mConditionRef = mRootRef.child("condition");
TextView content;
Button sunny, foggy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = (TextView) findViewById(R.id.condition);
sunny = (Button) findViewById(R.id.sunny);
foggy = (Button) findViewById(R.id.foggy);
}
#Override
protected void onStart() {
super.onStart();
mConditionRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
content.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
sunny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mConditionRef.setValue("Sunny");
mRootRef.child("condition").push().setValue("Abhijeet Jain2");
}
});
foggy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mConditionRef.setValue("Foggy");
}
});
}
}
The string gets added in the realtime database with a automatic made id but the app crashes as soon as I press the button "sunny".
What could be the reason and the solution?

How to to disable the send button if my fields are not filled using a textwatcher?

I'm having a hard time figuring out a solution to this situation. I need assistance, I am trying to disable the send button if my fields are not filled using a textwatcher. Here is part of the code:
public class Main5Activity extends AppCompatActivity {
TextView shoppinglist, fullname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
fullname = (TextView) findViewById(R.id.fullname);
shoppinglist = (TextView) findViewById(R.id.shoppinglist);
public void send(View v) {
new Send().execute();
}
}
Try this, and use EditText, TextView is not inputable:
editText1.addTextChangedListener(this);
imageButton.setEnable(false);
#Override
public void afterTextChanged(Editable s) {
if (s.toString().equals("")) {
imageButton.setEnabled(false);
} else {
imageButton.setEnabled(true);
}
}
Actually you don't need to use the textwatcher for disabling send button. All you could do is disable the send button in onCreate method within an if loop.
Note: Use EditText instead of TextView for taking inputs from user.
public class Main5Activity extends AppCompatActivity {
EditText shoppinglist, fullname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
fullname = findViewById(R.id.fullname);
button1 = (Button) findViewById(R.id.buttsave);
shoppinglist = findViewById(R.id.shoppinglist);
final String username = fullname.getText().toString();
final String shopList = shoppinglist.getText().toString();
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(shopList)) {
fullname.setError("Please enter your fullname");
shoppinglist.setError("Please select shopping list");
button1.setEnabled(false);
}
else {
button1.setEnabled(true);
}
//close onCreate here
}
public void send(View v) {
// it is not possible to enable the send button here because you are already
// executing the send method. So it should have already been enabled.
new Send().execute();
}
Again, if there is a necessity of using the textwatcher then do this:
public class Main5Activity extends AppCompatActivity implements TextWatcher {
EditText shoppinglist, fullname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
fullname = findViewById(R.id.fullname);
button1 = (Button) findViewById(R.id.buttsave);
shoppinglist = findViewById(R.id.shoppinglist);
fullname.addTextChangedListener(this);
shoppinglist.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
final String username = fullname.getText().toString();
final String shopList = shoppinglist.getText().toString();
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(shopList)) {
fullname.setError("Please enter your fullname");
shoppinglist.setError("Please select shopping list");
button1.setEnabled(false);
}
else {
button1.setEnabled(true);
}
}
public void send(View v) {
button1.setEnabled(true);
new Send().execute();
}
Hope this helps. Reply in comment if you still have any doubts.

how to replace AsyncTask by RxAndroid

I am trying to use RxAndroid to do the same work which is AsyncTask exactly does. I coded a simple example, where I enter a n integer in an edittext and when I press
a button, I make the hread to sleep for 3 seconds using "SystemClock.sleep" and while the thread is sleeping I show a progressbar and when the three seconds
elapsed the progresssbar will adisappear and the value entered in the edittext will be multiplied by 10 and displayed in atextview.
normally, when I use AsyncTask, I show the progressbar in onPreExecute and make it to disappear in onPostExecute. but when I run the below code I receive the
following error:
Only the original thread that created a view hierarchy can touch its views.
how to handle it properly in RxAndroid?
code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private EditText mEditTextValueToProcess = null;
private Button mButtonGoAsynchronous = null;
private TextView mTextViewProcessedValue = null;
private ProgressBar mProgressBar = null;
private rx.Observable<String> mAsyncObservable = null;
Subscriber mAsyncSubscriber = new Subscriber<String>() {
#Override
public void onCompleted() {
Log.w(TAG, "onCompleted");
mProgressBar.setVisibility(View.GONE);
}
#Override
public void onError(Throwable e) {
Log.w(TAG, "onError: " + e.getMessage().toString());
mProgressBar.setVisibility(View.GONE);
}
#Override
public void onNext(String o) {
Log.w(TAG, "onNext:->after processing " + o);
mTextViewProcessedValue.setText(o);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intiViews();
mAsyncObservable = rx.Observable.create(new rx.Observable.OnSubscribe<String>() {
#Override
public void call(Subscriber<? super String> subscriber) {
mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mProgressBar.setVisibility(View.VISIBLE);
SystemClock.sleep(3000);
subscriber.onNext("" + Integer.valueOf(mEditTextValueToProcess.getText().toString()) * 10);
subscriber.onCompleted();
}
});
}
private void intiViews() {
mEditTextValueToProcess = (EditText) findViewById(R.id.edittext);
mButtonGoAsynchronous = (Button) findViewById(R.id.button_go_asynchronous);
mTextViewProcessedValue = (TextView) findViewById(R.id.textview_processed_value);
//mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mButtonGoAsynchronous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAsyncObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mAsyncSubscriber);
}
});
}
}
Have you tried
private Observable<String> createObservable(final int number) {
return rx.Observable.create(new rx.Observable.OnSubscribe<String>() {
#Override
public void call(Subscriber<? super String> subscriber) {
SystemClock.sleep(3000);
subscriber.onNext("" + number * 10);
subscriber.onCompleted();
}
});
}
private void intiViews() {
mEditTextValueToProcess = (EditText) findViewById(R.id.edittext);
mButtonGoAsynchronous = (Button) findViewById(R.id.button_go_asynchronous);
mTextViewProcessedValue = (TextView) findViewById(R.id.textview_processed_value);
mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mButtonGoAsynchronous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProgressBar.setVisibility(View.VISIBLE);
createObservable(Integer.valueOf(mEditTextValueToProcess.getText().toString()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext((ignored) -> {
mProgressBar.setVisibility(View.GONE);
})
.subscribe(mAsyncSubscriber);
}
});
}
?
You need to change few things to achieve this. Look at mAsyncSubscriber initialization, onStart() method & progressBar initialization. The reason why you are getting the error is you are initializing & making your progressbar visible in another thread then your UI thread (because you specified this -> subscribeOn(Schedulers.io()).
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private EditText mEditTextValueToProcess = null;
private Button mButtonGoAsynchronous = null;
private TextView mTextViewProcessedValue = null;
private ProgressBar mProgressBar = null;
private rx.Observable<String> mAsyncObservable = null;
Subscriber mAsyncSubscriber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intiViews();
mAsyncSubscriber = new Subscriber<String>() {
// Override onStart method
#Override
public void onStart() {
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onCompleted() {
Log.w(TAG, "onCompleted");
mProgressBar.setVisibility(View.GONE);
}
#Override
public void onError(Throwable e) {
Log.w(TAG, "onError: " + e.getMessage().toString());
mProgressBar.setVisibility(View.GONE);
}
#Override
public void onNext(String o) {
Log.w(TAG, "onNext:->after processing " + o);
mTextViewProcessedValue.setText(o);
}
};
mAsyncObservable = rx.Observable.create(new rx.Observable.OnSubscribe<String>() {
#Override
public void call(Subscriber<? super String> subscriber) {
SystemClock.sleep(3000);
subscriber.onNext("" + Integer.valueOf(mEditTextValueToProcess.getText().toString()) * 10);
subscriber.onCompleted();
}
});
}
private void intiViews() {
// Init your progressbar
mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mEditTextValueToProcess = (EditText) findViewById(R.id.edittext);
mButtonGoAsynchronous = (Button) findViewById(R.id.button_go_asynchronous);
mTextViewProcessedValue = (TextView) findViewById(R.id.textview_processed_value);
//mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mButtonGoAsynchronous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAsyncObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mAsyncSubscriber);
}
});
}

How to retrieve POJO in Firebase Android

public class MainActivity extends AppCompatActivity {
private Firebase mRef;
private EditText mName,mEmail,mAge;
private Button mAddStud;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mName = (EditText) findViewById(R.id.editTextName);
mEmail = (EditText) findViewById(R.id.editTextEmail);
mAge = (EditText) findViewById(R.id.editTextAge);
mAddStud = (Button) findViewById(R.id.addStudents);
mRef = new Firebase(Constants.FIREBASE_URL);
mAddStud.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Student student = new Student(mName.getText().toString(),mEmail.getText().toString(), Integer.parseInt(mAge.getText().toString()));
mRef.child("newStudent").setValue(student);
}
});
}
}
Display
public class Display extends AppCompatActivity {
private Firebase mRef;
private TextView mDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
mDisplay = (TextView) findViewById(R.id.textViewDisplay);
mRef = new Firebase("https://firecrestapp.firebaseio.com/newStudent");
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Student value = dataSnapshot.getValue(Student.class);
mDisplay.setText(value.getName()+" "+value.getEmailId()+" "+value.getAge() );
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
Im getting this error
com.firebase.client.FirebaseException: Failed to bounce to type
How am I supposed to retrieve the data?

Categories

Resources