gpt4 book ai didi

java - 无法将数据从房间数据库获取到 Spinner

转载 作者:行者123 更新时间:2023-12-02 01:31:47 26 4
gpt4 key购买 nike

此应用程序将从 api.github.com/users 获取数据并反射(reflect)在微调器上

我正在 Splash.java 上的日志中获取数据,但无法反射(reflect) Home.java 上的数据

MyAppDatabase.java

@Database(entities = {Users.class}, version = 1, exportSchema = false)
public abstract class MyAppDatabase extends RoomDatabase {
public abstract MyDao myDao();
}

MyDao.java

@Dao
public interface MyDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
void addUsers(Users users);

@Query("SELECT * FROM Users")
List<Users> getUsers();

@Query("SELECT node_id FROM Users")
List<String> getNodeId();

}
Splash.java 上的

getData()

   public class Splash extends AppCompatActivity {

MyAppDatabase myAppDatabase;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
myAppDatabase = Room.databaseBuilder(getApplicationContext(), MyAppDatabase.class, "MyDao").allowMainThreadQueries().build();


//get data from webservice and store locally

getData();
}
}, 500);
}

private void getData() {
//Creating a string request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {

Log.e("response", response.toString());
response.length();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);

Users users = new Users();

//Set fields in Users object.

users.setLogin(jsonObject.getString(Config.TAG_Login));
users.setId(jsonObject.getString(Config.TAG_Id));
users.setNode_id(jsonObject.getString(Config.TAG_Node));
users.setAvatar_url(jsonObject.getString(Config.TAG_Avatar));
users.setGravatar_id(jsonObject.getString(Config.TAG_Gravatar_id));
users.setUrl(jsonObject.getString(Config.TAG_url));
users.setHtml_url(jsonObject.getString(Config.TAG_htmi_url));
users.setFollowers_url(jsonObject.getString(Config.TAG_Followers_url));
users.setFollowing_url(jsonObject.getString(Config.TAG_Following_url));
users.setGists_url(jsonObject.getString(Config.TAG_Gists_url));
users.setStarred_url(jsonObject.getString(Config.TAG_Starred_url));
users.setSubscriptions_url(jsonObject.getString(Config.TAG_Subscriptions_url));
users.setOrganizations_url(jsonObject.getString(Config.TAG_Organizations_url));
users.setRepos_url(jsonObject.getString(Config.TAG_Repos_url));
users.setReceived_events_url(jsonObject.getString(Config.TAG_Received_events_url));
users.setType(jsonObject.getString(Config.TAG_Type));
users.setSite_admin(jsonObject.getString(Config.TAG_Site_admin));
users.setEvents_url(jsonObject.getString(Config.TAG_events_url));

myAppDatabase.myDao().addUsers(users);

} catch (JSONException e) {
e.printStackTrace();
}
}

Intent intent = new Intent(Splash.this, Home.class);
// intent.putExtra("remember", "true");
startActivity(intent);

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonArrayRequest);
}
}

Home.java 上的 putData()

  public class Home extends AppCompatActivity {

MyAppDatabase myAppDatabase;

//To add into spinner
private ArrayList<String> userType;

//List of users from Db
List<Users> users;

private Spinner spinner;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

userType = new ArrayList<>();

spinner = findViewById(R.id.sp_user_type);

myAppDatabase = Room.databaseBuilder(getApplicationContext(), MyAppDatabase.class,"UsersDb").allowMainThreadQueries().build();
//get data from Db and put into arrayList for spinner

putData();
}

private void putData() {

users =myAppDatabase.myDao().getUsers();
userType.add("Select UserType"); //Dummy addition

for(int i = 0; i < users.size(); i++){
userType.add(users.get(i).getType());
Log.e("DistNames",users.get(i).getType());
}

users.size();
spinner.setAdapter(new ArrayAdapter<>(Home.this, android.R.layout.simple_spinner_dropdown_item, userType));
}
}

最佳答案

因为您是在循环本身中启动 Activity ,而该 Activity 实际上应该在循环之外。

实际上,您犯的错误是它将在第一次迭代中启动 Home Activity ,因此您无法从 API 响应中获取所有数据。为了解决这个问题,让循环遍历每个条目并添加到数据库中,并在完成时启动 Activity ,因此在循环之外。

错误:

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {

Log.e("response", response.toString());
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);

Users users = new Users();

//Set fields in Users object.

users.setLogin(jsonObject.getString(Config.TAG_Login));
users.setId(jsonObject.getString(Config.TAG_Id));
users.setNode_id(jsonObject.getString(Config.TAG_Node));
users.setAvatar_url(jsonObject.getString(Config.TAG_Avatar));
users.setGravatar_id(jsonObject.getString(Config.TAG_Gravatar_id));
users.setUrl(jsonObject.getString(Config.TAG_url));
users.setHtml_url(jsonObject.getString(Config.TAG_htmi_url));
users.setFollowers_url(jsonObject.getString(Config.TAG_Followers_url));
users.setFollowing_url(jsonObject.getString(Config.TAG_Following_url));
users.setGists_url(jsonObject.getString(Config.TAG_Gists_url));
users.setStarred_url(jsonObject.getString(Config.TAG_Starred_url));
users.setSubscriptions_url(jsonObject.getString(Config.TAG_Subscriptions_url));
users.setOrganizations_url(jsonObject.getString(Config.TAG_Organizations_url));
users.setRepos_url(jsonObject.getString(Config.TAG_Repos_url));
users.setReceived_events_url(jsonObject.getString(Config.TAG_Received_events_url));
users.setType(jsonObject.getString(Config.TAG_Type));
users.setSite_admin(jsonObject.getString(Config.TAG_Site_admin));
users.setEvents_url(jsonObject.getString(Config.TAG_events_url));

myAppDatabase.myDao().addUsers(users);

Intent intent = new Intent(Splash.this, Home.class);
// intent.putExtra("remember", "true");
startActivity(intent);

} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});

正确:

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {

Log.e("response", response.toString());
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);

Users users = new Users();

//Set fields in Users object.

users.setLogin(jsonObject.getString(Config.TAG_Login));
users.setId(jsonObject.getString(Config.TAG_Id));
users.setNode_id(jsonObject.getString(Config.TAG_Node));
users.setAvatar_url(jsonObject.getString(Config.TAG_Avatar));
users.setGravatar_id(jsonObject.getString(Config.TAG_Gravatar_id));
users.setUrl(jsonObject.getString(Config.TAG_url));
users.setHtml_url(jsonObject.getString(Config.TAG_htmi_url));
users.setFollowers_url(jsonObject.getString(Config.TAG_Followers_url));
users.setFollowing_url(jsonObject.getString(Config.TAG_Following_url));
users.setGists_url(jsonObject.getString(Config.TAG_Gists_url));
users.setStarred_url(jsonObject.getString(Config.TAG_Starred_url));
users.setSubscriptions_url(jsonObject.getString(Config.TAG_Subscriptions_url));
users.setOrganizations_url(jsonObject.getString(Config.TAG_Organizations_url));
users.setRepos_url(jsonObject.getString(Config.TAG_Repos_url));
users.setReceived_events_url(jsonObject.getString(Config.TAG_Received_events_url));
users.setType(jsonObject.getString(Config.TAG_Type));
users.setSite_admin(jsonObject.getString(Config.TAG_Site_admin));
users.setEvents_url(jsonObject.getString(Config.TAG_events_url));

myAppDatabase.myDao().addUsers(users);

} catch (JSONException e) {
e.printStackTrace();
}
}

Intent intent = new Intent(Splash.this, Home.class);
// intent.putExtra("remember", "true");
startActivity(intent);

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});

关于java - 无法将数据从房间数据库获取到 Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57552223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com