我从 lifecycle 体系结构库中添加了一个 ViewModel 类。但这在我的代码中不起作用。我做错了什么。我是android的ViewModel Arch组件的新手。
这是我的代码:
等级:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
}
ViewModel:
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
public class NameViewModel extends ViewModel {
// Create a LiveData with a String
private MutableLiveData<String> mCurrentName;
public MutableLiveData<String> getmCurrentName() {
if (mCurrentName == null) {
mCurrentName = new MutableLiveData<>();
}
return mCurrentName;
}
}
MainActivity.java
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private NameViewModel nameViewModel;
private TextView lblText;
private EditText txtValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lblText = findViewById(R.id.textview);
txtValue = findViewById(R.id.editText);
final Observer<String> nameObserver = new Observer<String>() {
@Override
public void onChanged(@Nullable String data) {
lblText.setText(data);
}
};
nameViewModel.getmCurrentName().observe(this, nameObserver);
txtValue.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence value, int start, int before, int count) {
nameViewModel.getmCurrentName().setValue(value.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
我错过了必要的东西吗?
任何帮助将不胜感激。非常感谢。
答案 0 :(得分:1)
将其放入您的pd.DataFrame(list(itertools.product(set(x.user), y)), columns=['user', 'movie'])\
.merge(x, how='outer')
Out[76]:
user movie rating
0 1 100 1.0
1 1 200 4.0
2 1 300 3.0
3 1 400 NaN
4 2 100 5.0
5 2 200 3.0
6 2 300 NaN
7 2 400 NaN
8 3 100 NaN
9 3 200 NaN
10 3 300 2.0
11 3 400 NaN
方法中
onCreate
并参考this和this链接以获取有关nameViewModel = ViewModelProviders.of(this, null).get(NameViewModel.class);
的更多信息