如何从视图模型正确调用网络获取功能

时间:2019-08-09 12:35:55

标签: java android networking viewmodel mutablelivedata

如何在QueryUtils.fetchData(REQUEST_URL内调用函数ViewModel),它返回List<Earthquakes>。该代码以应有的方式获取数据,但可能不会显示,因为它已在获取数据之前设置了数据。

public class MainActivity extends AppCompatActivity {

    private EarthquakeAdapter mEarthquakeAdapter;
    private EarthquakeViewModel aEarthquakeViewModel;

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

// Redacted

    aEarthquakeModel = ViewModelProviders.of(this).get(EarthquakeViewModel.class);

    aEarthquakeViewModel.fetchEarthquakes().observe(this, new Observer<ArrayList<Earthquake>>() {
            @Override
            public void onChanged(ArrayList<Earthquake> earthquakes) {
                if(earthquakes != null) {
                    mEarthquakeAdapter.clear();
                    mEarthquakeAdapter.addAll(earthquakes);
                }
            }
        });
    }
}


public class EarthquakeViewModel extends AndroidViewModel {

    public MutableLiveData<ArrayList<Earthquake>> earthquakesData;
    private ArrayList<Earthquake> earthquakes;

    public EarthquakeViewModel(@NonNull Application application) {
        super(application);
        Log.i("EarthquakeViewModel", "EarthquakeViewModel constructor entered");
        earthquakesData = new MutableLiveData<>();
        doIt();
        Log.i("EarthquakeViewModel", "EarthquakeViewModel constructor finished");
    }

    public LiveData<ArrayList<Earthquake>> fetchEarthquakes() {
        earthquakesData.setValue(earthquakes);
        return earthquakesData;
    }

    public void doIt() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                earthquakes = (ArrayList<Earthquake>) QueryUtils.fetchData(REQUEST_URL);
            }
        });
        thread.start();
    }
}

2 个答案:

答案 0 :(得分:0)

稍微重写一下您的viewmodel类:

 public class EarthquakeViewModel extends AndroidViewModel {

public MutableLiveData<ArrayList<Earthquake>> earthquakesData;
private ArrayList<Earthquake> earthquakes;

public EarthquakeViewModel(@NonNull Application application) {
    super(application);
    Log.i("EarthquakeViewModel", "EarthquakeViewModel constructor entered");
    earthquakesData = new MutableLiveData<>();
    doIt();
    Log.i("EarthquakeViewModel", "EarthquakeViewModel constructor finished");
}

public LiveData<ArrayList<Earthquake>> fetchEarthquakes() {
    return earthquakesData;
}

public void doIt() {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            earthquakes = (ArrayList<Earthquake>) QueryUtils.fetchData(REQUEST_URL);
          earthQuakesData.postValue(earthquakes);

        }
    });
    thread.start();
}
}

请尝试这个,让我知道

答案 1 :(得分:0)

您需要在此处了解MutableLiveData中的setValue()和postValue()的概念。在主线程中进行更新时,必须使用函数setValue()更新MutableLive数据,而在工作线程中,则需要使用postValue()。希望这可以帮助。以上的答案可以满足您的要求。