gpt4 book ai didi

java - 在Android Studio中使用两个ViewModel从Fragments获取不同类型的数据

转载 作者:行者123 更新时间:2023-12-01 16:14:49 25 4
gpt4 key购买 nike

我对此很陌生,需要一些帮助。我正在尝试使用两个 ViewModels 从不同的片段获取 LiveData

  1. DayViewModel 工作正常。它从 DayFragment 收集 CharSequence 输入,然后将其传递到 MainActivity,从那里我可以获取在不同片段中使用的数据。
  2. MediaRecorderViewModel 使用相同的方法。

日志显示 MediaRecorderViewModel 正确地从不同片段收集 CharSequence,但在 MainActivity 中未接收到相同的内容!我究竟做错了什么?下面附加了 ViewModelsMainActivity 代码。

DayViewModel:

import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class DayViewModel extends ViewModel {


private MutableLiveData<CharSequence> selected = new MutableLiveData<> ();

public void setData(CharSequence dIndex) {
selected.setValue (dIndex);
Log.d("MessageI", "selected: " + dIndex);
}

public LiveData<CharSequence> getDayIndex() {
Log.d("LiveView1", "selected: " + selected);
return selected;

}


}

MediaRecorderViewModel:

import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class MediaRecorderViewModel extends ViewModel {


private MutableLiveData<CharSequence> selected = new MutableLiveData<> ();

public void setMasterRecording(CharSequence input) {
selected.setValue (input);
Log.d("Message", "selected: " + input);
}

public LiveData<CharSequence> getValue() {
Log.d("LiveView", "selected: " + selected.toString ());
return selected;

}

}

主要 Activity :

public class MainActivity extends AppCompatActivity {
private DayViewModel dayViewModel;
private MediaRecorderViewModel mediaRecorderViewModel;
public int j = 0;
public String newRecording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
// adding the three fragments to activity_main
.add(R.id.phrase_fragment_container, new PhrasesFragment())
.add(R.id.media_fragment_container, new MediaRecorderFragment())
.add(R.id.days_fragment_container, new DaysFragment())
.commit();

mediaRecorderViewModel = new ViewModelProvider(this).get(MediaRecorderViewModel.class);
mediaRecorderViewModel.getValue().observe(this, new Observer < CharSequence > () {
@Override
public void onChanged(CharSequence c) {
CharSequence rec = c;
newRecording = String.valueOf(c);

//*this one does not work*
Log.d("Rec", "newRecording: " + rec);
}
});

dayViewModel = new ViewModelProvider(this).get(DayViewModel.class);
dayViewModel.getDayIndex().observe(this, new Observer < CharSequence > () {
@Override
public void onChanged(CharSequence charSequence) {
CharSequence newDay = charSequence;
int dayIndex = Integer.parseInt(newDay.toString());
if (Integer.toString(dayIndex) != null) {
j = dayIndex;

// this one works fine
Log.d("JValue", "J: " + newDay + "dayIndexCharSeq: " + dayIndex);
}

Log.d("Convert", "newDay value is: " + dayIndex);
}
});

// instantiate BottomNav
BottomNavigationView navView = findViewById(R.id.nav_view);

}
for

// Getter dIndex. This is coming from DayViewModel

public int getJ() {
return j;
}

// Getter for newRecording. This is coming from MediaRecorderViewModel

public String getNewRecording() {
return newRecording;
}
}

最佳答案

我解决了。我需要在 Fragment 的 onViewCreated() 方法中实例化 MediaRecorderViewModel。另外,所有者必须从this更改为getActivity()

这是片段中的代码:


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated (view, savedInstanceState);
mediaRecorderViewModel = new ViewModelProvider (getActivity ())
.get (MediaRecorderViewModel.class);
}

关于java - 在Android Studio中使用两个ViewModel从Fragments获取不同类型的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62431600/

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