我试图在 android 中查看模型,所以对于 MainViewModel.java 我写了这个:
public class MainViewModel extends ViewModel {
private String textView;
private String editText;
//@Bindable
public String getTextView(){
return textView;
}
private void setTextView(String value){
textView=value;
}
//@Bindable
public TextWatcher getEditTextWatcher() {
return new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
setTextView(s.toString());
}
...
};
}
}
Run Code Online (Sandbox Code Playgroud)
在 ActivityMain.xml 中我写了这样的内容:
<TextView
android:text="View-Model / Data-Binding"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<TextView
android:id="@+id/main_text_view"
android:text="@{mainvm.textView}"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<EditText
android:id="@+id/main_edit_text"
app:textChangeListener="@{mainvm.editTextWatcher}"
android:layout_width="match_parent"
android:layout_height="40dp"/>
Run Code Online (Sandbox Code Playgroud)
我收到 2 个错误:
Cannot …Run Code Online (Sandbox Code Playgroud) 视图模型中字段的推荐模式似乎是:
val selected = MutableLiveData<Item>()
fun select(item: Item) {
selected.value = item
}
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,该selected字段不是私有的,这是正确的吗?)
但是如果我不需要订阅 ViewModel 字段中的更改怎么办?我只需要被动地将这个值拉到另一个片段中。
我的项目详细信息:
视图模型:
private var amount = 0
fun setAmount(value: Int) { amount = value}
fun getAmount() = amount
Run Code Online (Sandbox Code Playgroud)
片段1:
bnd.button10.setOnClickListener { viewModel.setAmount(10) }
Run Code Online (Sandbox Code Playgroud)
片段2:
if(viewModel.getAmount() < 20) { bnd.textView.text = "less than 20" }
Run Code Online (Sandbox Code Playgroud)
这是一种有效的方法吗?或者有更好的吗?或者我应该只使用 LiveData 或 Flow?
也许我应该使用SavedStateHandle?它可以注入到 ViewModel 中吗?
我正在使用视图模型和实时数据来观察编辑文本视图中的变化,如下所示。当我使用leakcanar运行应用程序时,它向我显示内存泄漏:Mainactivity.fragviewmodel泄漏。
FragViewModel model;
@Override
public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
model = new ViewModelProvider(requireActivity()).get(FragViewModel.class);
final Observer<String> savedTextObserver = newName -> {
list = MainActivity.quotesDatabaseClass.quoteDao().getQuotes();
adapter.setData(list);
};
model.getTextToSave().observe(requireActivity(), savedTextObserver);
}
@Override
public void onDestroyView() {
super.onDestroyView();
fragmentEmojiBinding = null;
model = null;
}
Run Code Online (Sandbox Code Playgroud) 我有一个SearchFragment带有以下代码的。
@AndroidEntryPoint
class SearchFragment :
Fragment(),
View.OnClickListener {
...
private var _binding: FragSearchBinding? = null
private val binding get() = _binding as FragSearchBinding
private val viewmodel by viewModels<SearchViewModel>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
)
: View {
_binding = FragSearchBinding.inflate(inflater, container, false)
binding.fragSearchSearchResultFilter.setOnClickListener(this)
return binding.root
}
...
private fun showFilterDialog() {
val dialog = FilterBottomSheetDialogFragment.newInstance()
dialog.show(parentFragmentManager, "filter_bsd_tag")
}
...
}
Run Code Online (Sandbox Code Playgroud)
我正在展示一个FilterBottomSheetDialogFragment使用它的方法SearchFragment。我想将 ViewModel 传递SearchFragment给DialogFragment. 我的FilterBottomSheetDialogFragment. …
viewmodel android-fragments kotlin android-viewmodel android-ktx
我正在开发一个交易应用程序。我需要列出用户股票及其价值(利润或损失)以及投资组合的总价值。
对于馆藏列表,在 MVP 架构中,我将为每个列表项创建一个演示者,但对于此应用程序,我决定使用 MVVM(Compose、ViewModels 和 Hilt)。我的第一个想法是为每个列表项创建一个不同的 ViewModel。我hiltViewModel()在可组合方法签名中使用来创建 ViewModel 的实例,但这总是给我相同的实例,而这不是我想要的。使用 MVVM 架构时,我正在尝试以正确的方式执行操作还是应该使用单个 ViewModel?您知道我可以看看的任何项目吗?下图是我的实际屏幕的超级简化,每个单元格都很复杂,这就是为什么我想为每个单元格使用不同的 ViewModel。任何建议都非常受欢迎。
android mvvm viewmodel android-architecture-components android-jetpack-compose
该函数将如何@Composable ContentFeed()访问viewModel在活动中创建的?依赖注入?或者这是一种错误的做事方式?应该viewModel始终只有一个实例。
// MainActivity.kt
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel by viewModels<MainViewModel>()
setContent {
PracticeTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
PulseApp(viewModel)
}
}
}
}
// TabItem.kt
typealias ComposableFun = @Composable () -> Unit
sealed class TabItem(var icon: Int, var title: String, var content: ComposableFun) {
object Feed : TabItem(R.drawable.ic_baseline_view_list_24, "Feed", { ContentFeed() })
}
// …Run Code Online (Sandbox Code Playgroud) 我不确定,但只要我读到有关 ddd 的内容,域模型就永远不应该离开应用程序层..如果这是真的,那么视图模型如何重用域模型的行为?
从 ddd 角度假设以下发票模型
public class Invoice
{
public int Id { get; set; }
public int CustomerID { get; internal set; }
public void ChangeCustomer(Customer customer)
{
if (customer.IsActive == false)
throw new Exception("Inactive customers cannot be used");
CustomerID = customer.Id;
//other properties change also that need to be reflected to the user interface
}
}
Run Code Online (Sandbox Code Playgroud)
现在让发票 ViewModel 尝试 #1。按照这个想法,我在重用域行为方面没有问题,但域层必须引用本例中的 UI 项目(WPF)。但在这里我担心我们不应该在应用程序层之外使用域层
public class InvoiceVMOption1 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyUI(string PropertyName)
{ …Run Code Online (Sandbox Code Playgroud) 当我尝试在 Android 应用程序中处理进程死亡时,我注意到范围为导航图的 ViewModel 在从进程死亡中重新创建时崩溃了。下面是 ViewModel 和使用 ViewModel 的 Fragment 的代码。
\n@ExperimentalCoroutinesApi\n@HiltViewModel\nclass ViewPrintOrderVM @Inject constructor(\nprivate val repository: Repository,\nprivate val application: Application,\nprivate val savedStateHandle: SavedStateHandle\n) : ViewModel() {\n\n // Code inside the ViewModel\n\n}\nRun Code Online (Sandbox Code Playgroud)\n在我的片段中,我得到的参考如下
\n@ExperimentalComposeUiApi\n@ExperimentalCoroutinesApi\n@AndroidEntryPoint\nclass ComposeFragmentPostPressDetails : Fragment() {\n\n //Here we are scoping the ViewModel to the Navigation graph with graph-id instead of this fragment\n private val viewModel: ManagePrintOrderVM by navGraphViewModels(R.id.print_order_flow)\n\n //Rest of the Fragment code\n\n}\nRun Code Online (Sandbox Code Playgroud)\n现在,当我尝试通过在模拟器中启动应用程序,将其放在后台,然后使用 android studio 中的“终止应用程序”按钮终止进程来测试应用程序的进程死亡时,它在使用以下命令重新创建时崩溃错误
\n2022-03-19 19:06:44.633 7174-7174/com.sivakasi.papco.jobflow E/AndroidRuntime: FATAL EXCEPTION: …Run Code Online (Sandbox Code Playgroud) 我相信ViewModels是一件好事.我刚刚尝试将部分应用程序转换为使用它们.但是我可能遇到了副作用,这是它们对模型绑定的影响,特别是Radiobutton助手的默认"检查"行为:
@Html.RadioButton("Value",Item.Value)
Run Code Online (Sandbox Code Playgroud)
如果默认模型具有"Value"属性,则上述方法有效.但是,如果我使用ViewModel,那么Value可能会降低一级,即:
没有VM
Model.Value
Run Code Online (Sandbox Code Playgroud)
使用VM:
Model.Content.Value;
MyContent = Model.Content.Value;
Run Code Online (Sandbox Code Playgroud)
这不再有效:
@Html.RadioButton("Value",Item.Value)
Run Code Online (Sandbox Code Playgroud)
也许我需要调整RB助手?
谢谢.
嗨,我正在使用telerik appBuilder进行我的第一个应用程序移动,我无法使用下拉列表来使用Kendo数据源.
我的webservices的结果如下,但我无法获得该结果的正确数据绑定.
{"d":[{"id":2209,"nom":"Test 1"},{"id":23608,"nom":"Test 2"},{"id":24061,"nom" :"测试3"},{"id":24741,"nom":"测试4"},{"id":27347,"nom":"测试5"}}}
请问,有什么想法吗?非常感谢.
/* product.html*/
<div id="product" data-role = "view"
data-layout = "sharedlayout" data-model="app.productService.viewModel">
<div class="view-content">
<form >
<div data-role="listview" data-style="inset">
<div>
Products:
<select id="product" data-role="dropdownlist"
data-bind="source: productsdataSource "
data-text-field="id"
data-value-field="product">
<option value="0"> </option>
</select>
</div>
</div>
</form>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
ProductViewModel.js
(function (global)
{
var ProductsViewModel,
app = global.app = global.app || {};
ProductsViewModel = kendo.data.ObservableObject.extend (
{
getProducts: function() {
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "urlexample",
type:"post",
contentType: "application/json; …Run Code Online (Sandbox Code Playgroud) datasource viewmodel kendo-ui kendo-dropdown telerik-appbuilder
viewmodel ×10
android ×6
mvvm ×3
java ×2
kotlin ×2
android-architecture-components ×1
android-ktx ×1
asp.net-mvc ×1
behavior ×1
c# ×1
datasource ×1
kendo-ui ×1
memory-leaks ×1
wpf ×1