Skip to content

Instantly share code, notes, and snippets.

@DiyahM
Created September 10, 2013 18:19
Show Gist options
  • Save DiyahM/6513376 to your computer and use it in GitHub Desktop.
Save DiyahM/6513376 to your computer and use it in GitHub Desktop.
public void onLoginSuccess() {
MyTwitterApp.getRestClient().getUserCredentials(new JsonHttpResponseHandler () {
@Override
public void onSuccess(JSONObject jsonUser) {
User user = User.fromJson(jsonUser);
UserModel currentUser = new UserModel(user.getScreenName(), user.getProfileImageUrl());
currentUser.save();
SharedPreferences pref = getSharedPreferences("myPrefs", MODE_PRIVATE);
Editor edit = pref.edit();
edit.putString("current_user", currentUser.getId().toString());
edit.commit();
// this currentUser.getId() returns -1
Log.d("DEBUG","current user id is " + currentUser.getId().toString());
}
@Override
public void onFailure(Throwable e, JSONObject response) {
Log.d("DEBUG", "error" + response.toString());
}
});
Log.d("DEBUG", "Login Activity");
Intent i = new Intent(this, TimelineActivity.class);
startActivity(i);
}
package com.codepath.apps.diyahtwitterapp.models;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
@Table(name = "users")
public class UserModel extends Model {
@Column(name = "screen_name")
private String screenName;
@Column(name = "profile_image_url")
private String profileImageUrl;
public UserModel() {
super();
}
public UserModel(String screenName, String profileImageUrl) {
super();
this.screenName = screenName;
this.profileImageUrl = profileImageUrl;
}
public String getScreenName() {
return this.screenName;
}
public String getProfileImageUrl() {
return this.profileImageUrl;
}
}
@DiyahM
Copy link
Author

DiyahM commented Sep 10, 2013

@timothy1ee @nesquena

I'm having issues with ActiveAndroid. When I create a UserModel, it doesn't appear to be saving. getId() is returning -1.

@nesquena
Copy link

So I tried your code snippets in a brand new project unmodified and tried saving a UserModel and it is saving properly with incrementing ids. On first launch, ActiveAndroid generates the schema for the sqlite database. You may want to delete the app off the emulator clearing the database and allowing activeandroid to recreate the schema.

Since I used your code unchanged in my sample app, it's unlikely that there's a major problem but you should also use breakpoints to check on UserModel currentUser = new UserModel(user.getScreenName(), user.getProfileImageUrl()); and verify that both the screen name and profile image are also what you expect them to be.

@nesquena
Copy link

@DiyahM Post back here if you can't get it working and we can chat about more possibilities.

@DiyahM
Copy link
Author

DiyahM commented Sep 10, 2013

@nesquena Thanks! That fixed it! I had to delete the app off the emulator.

@nesquena
Copy link

Great! When in doubt, often restarting the emulator and/or uninstalling the app (or wiping the emulator data) will fix a lot of problems. In this case, important to remember that ActiveAndroid only creates the database (and the tables) if there is no database yet created. That means if you run the app, the schema is created and then if you add models or change model attributes, those won't be updated unless the database is wiped (i.e app is uninstalled). Check out https://github.com/pardom/ActiveAndroid/wiki/Schema-migrations for how you can change the schema in situations where wiping the existing database isn't an option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment