我试图通过使用改造调用从数据库中获取数据来填充两个片段中的列表。问题是,由于异步执行,当片段中需要某些列表时,它们仍然是空的。然后在进行其他调用时使用这些列表,但是因为它们仍然是空的,所以什么也不会发生。回调本身从数据库获取数据,所以这部分是正常的。我对java和改型非常陌生,所以异步调用仍然很难理解。很抱歉代码墙的问题,但是我几乎在每一段代码中都使用了调用,所以我尝试提供了我所要做的一切。我查了一下合作伙伴,他们看起来可以解决我的问题,但他们只是在Kotlin。
布局中包含两个片段的主活动
public class BrowseActivity extends FragmentActivity {
private LinkedHashMap<Integer, RowFragment> fragments;
private HeaderFragment headerFragment;
private RowFragment rowFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse);
String jwt = getIntent().getSerializableExtra("Jwt").toString();
SharedPreferences shared = getApplicationContext().getSharedPreferences("User", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor = editor.putString("Jwt", jwt);
editor.commit();
shared = getSharedPreferences("User", MODE_PRIVATE);
editor = shared.edit();
editor = editor.putString("Jwt", jwt);
editor.commit();
headerFragment = new HeaderFragment();
rowFragment = new RowFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction
.replace(R.id.fragment_rows, rowFragment, "RowFragment")
.replace(R.id.fragment_headers, headerFragment, "HeaderFragment");
transaction.commit();
fragments = new LinkedHashMap<Integer, RowFragment>();
int subjectCount = this.getTeachers(jwt).size();
for(int i = 0; i < subjectCount; i++){
RowFragment fragment = new RowFragment();
fragments.put(i, fragment);
}
}
public LinkedHashMap<Integer, RowFragment> getRowFragments() {
return this.fragments;
}
}
getteachers()方法
public List<Teacher> getTeachers(String jwt){
APIService apiService = ApiServiceGenerator.createService(APIService.class, jwt);
Call<List<Teacher>> call = apiService.getTeachers();
List<Teacher> tempTeachers = new ArrayList<Teacher>();
call.enqueue(new Callback<List<Teacher>>(){
@Override
public void onResponse(Call<List<Teacher>> call, Response<List<Teacher>> response){
if(response.isSuccessful()){
if(response.body().isEmpty() != true){
tempTeachers.addAll(response.body());
}
else{
Toast.makeText(BrowseActivity.this, "You have no teachers assigned", Toast.LENGTH_SHORT).show();
}
}
else{
Log.e("abc", response.message().toString());
Toast.makeText(BrowseActivity.this, "You are not authorized, please log in again", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Teacher>> call, Throwable err) {
Log.i("err", err.getMessage());
}
});
return tempTeachers;
}
标题片段
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customSetBackground(R.color.fastlane_background);
//setOnItemSelectedListener(getDefaultItemSelectedListener());
setHeaderAdapter();
}
private void setHeaderAdapter() {
adapter = new ArrayObjectAdapter();
LinkedHashMap<Integer, RowFragment> fragments = new LinkedHashMap<>();
List<Teacher> teachers = new ArrayList<>();
Activity activity = getActivity();
if(activity instanceof BrowseActivity) {
fragments = ((BrowseActivity) activity).getRowFragments();
teachers = this.getTeachers();
}
for (int i = 0; i < teachers.size(); i++) {
HeaderItem header = new HeaderItem(i, teachers.get(i).subject);
ArrayObjectAdapter innerAdapter = new ArrayObjectAdapter();
innerAdapter.add(fragments.get(i));
adapter.add(i, new ListRow(header, innerAdapter));
}
setAdapter(adapter);
}
行片段
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(getView(), savedInstanceState);
loadRows();
}
private void loadRows() {
rowAdapter = new ArrayObjectAdapter(new ListRowPresenter());
sectionPresenter = new SectionPresenter();
List<Teacher> teachers = new ArrayList<>();
teachers = getTeachers();
for (int i = 0; i < teachers.size(); i++) {
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(sectionPresenter);
int teacherId = teachers.get(i).userId;
List<Section> list = SectionList.populateSections(teacherId, getActivity(), context);
for (int j = 0; j < list.size(); j++) {
listRowAdapter.add(list.get(i));
}
HeaderItem header = new HeaderItem(i, teachers.get(i).subject);
rowAdapter.add(new ListRow(header, listRowAdapter));
}
setAdapter(rowAdapter);
}
populatesections()方法
public static List<Section> populateSections(int teacherId, FragmentActivity activity, Context context) {
TeacherIdRequest teacherIdRequest = new TeacherIdRequest(teacherId);
SharedPreferences shared = context.getSharedPreferences("User", MODE_PRIVATE);
String jwt = shared.getString("Jwt", null);
APIService apiService = ApiServiceGenerator.createService(APIService.class, jwt);
Call<List<Section>> call = apiService.getTeacherSections(teacherIdRequest);
List<Section> tempSections = new ArrayList<Section>();
call.enqueue(new Callback<List<Section>>(){
@Override
public void onResponse(Call<List<Section>> call, Response<List<Section>> response){
if(response.isSuccessful()){
if(response.body().isEmpty() != true){
tempSections.addAll(response.body());
}
else{
Toast.makeText(activity, "You have no sections assigned", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(activity, "You are not authorized, please log in again13", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Section>> call, Throwable err) {
Log.i("err", err.getMessage());
}
});
return tempSections;
}
暂无答案!
目前还没有任何答案,快来回答吧!