Android 带有 ArrayAdapter 的 LiveData 列表视图
Android LiveData with ArrayAdapter to list view
您好,我试图将实时数据添加到主 activity 中的列表视图数据适配器,但卡住了,谁能告诉我为什么这不起作用?
RecordViewModel:
public class RecordViewModel extends AndroidViewModel {
private Repository mRepository;
public static LiveData<List<Record>> mAllRecords;
public RecordViewModel (Application application) {
super(application);
mRepository = new Repository(application);
mAllRecords = mRepository.getAllRecords();
}
public LiveData<List<Record>> getAllWords() {
mAllRecords=mRepository.getAllRecords();
return mAllRecords; }
主要Activity:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
viewModel = new RecordViewModel(getApplication());
//should I use viewModel.getAllWords() to fetch data from database into mAllRecords?
@Override
protected void onCreate(Bundle savedInstanceState) {
lv = findViewById(R.id.listview);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, mAllRecords);
//I am getting error here, can't resolve constructor
lv.setAdapter(adapter);
我不清楚实时数据到数组适配器的转换。
你只需要像这样观察来自记录的实时数据
public class MainActivity extends AppCompatActivity implements SensorEventListener {
viewModel = new RecordViewModel(getApplication());
//should I use viewModel.getAllWords() to fetch data from database into mAllRecords? yes you should do it on OnCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
lv = findViewById(R.id.listview);
viewModel.getAllWords();
final Observer<List<Record>> wordObserve = new Observer<List<Record>>() {
@Override
public void onChanged(@Nullable final allRecords List<Record>) {
// Update the UI, in this case, a TextView.
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, allRecords);
lv.setAdapter(adapter);
}
};
viewModel.mAllRecords().observe(this, wordObserve);
如果我错了请纠正我,因为如果在这里使用 Kotlin 会很容易 reference
您好,我试图将实时数据添加到主 activity 中的列表视图数据适配器,但卡住了,谁能告诉我为什么这不起作用?
RecordViewModel:
public class RecordViewModel extends AndroidViewModel {
private Repository mRepository;
public static LiveData<List<Record>> mAllRecords;
public RecordViewModel (Application application) {
super(application);
mRepository = new Repository(application);
mAllRecords = mRepository.getAllRecords();
}
public LiveData<List<Record>> getAllWords() {
mAllRecords=mRepository.getAllRecords();
return mAllRecords; }
主要Activity:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
viewModel = new RecordViewModel(getApplication());
//should I use viewModel.getAllWords() to fetch data from database into mAllRecords?
@Override
protected void onCreate(Bundle savedInstanceState) {
lv = findViewById(R.id.listview);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, mAllRecords);
//I am getting error here, can't resolve constructor
lv.setAdapter(adapter);
我不清楚实时数据到数组适配器的转换。
你只需要像这样观察来自记录的实时数据
public class MainActivity extends AppCompatActivity implements SensorEventListener {
viewModel = new RecordViewModel(getApplication());
//should I use viewModel.getAllWords() to fetch data from database into mAllRecords? yes you should do it on OnCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
lv = findViewById(R.id.listview);
viewModel.getAllWords();
final Observer<List<Record>> wordObserve = new Observer<List<Record>>() {
@Override
public void onChanged(@Nullable final allRecords List<Record>) {
// Update the UI, in this case, a TextView.
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, allRecords);
lv.setAdapter(adapter);
}
};
viewModel.mAllRecords().observe(this, wordObserve);
如果我错了请纠正我,因为如果在这里使用 Kotlin 会很容易 reference