Failed to create target binding for binding SelectedItem - mvvmcross

I try to bind the selected item of a MvxListView to my property. This is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
layout="#layout/toolbar_actionbar" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:paddingRight="6dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_search_text"
local:MvxBind="Text SearchText" />
<MvxListView
android:id="#+id/category_list"
android:orientation="vertical"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxItemTemplate="#layout/listitem_category"
local:MvxBind="ItemsSource Categories; SelectedItem SelectedCategory" />
</LinearLayout>
</LinearLayout>
The ItemSource here is properly bound.
ViewModel:
[ImplementPropertyChanged]
public class SelectCategoryListViewModel : AbstractCategoryListViewModel
{
/// <summary>
/// Creates an CategoryListViewModel for the usage of providing a category selection.
/// </summary>
/// <param name="categoryRepository">An instance of <see cref="IRepository{T}" /> of type category.</param>
/// <param name="dialogService">An instance of <see cref="IDialogService" /></param>
public SelectCategoryListViewModel(IRepository<Category> categoryRepository,
IDialogService dialogService) : base(categoryRepository, dialogService)
{}
public Category SelectedCategory { get; set; }
public MvxCommand DoneCommand => new MvxCommand(Done);
public MvxCommand CancelCommand => new MvxCommand(Cancel);
private void Done()
{
MessageHub.Publish(new CategorySelectedMessage(this, SelectedCategory));
Close(this);
}
private void Cancel()
{
Close(this);
}
}
Not the notify PropertyChanged is done via Fody and Categories are in the parent VM.
public class SelectCategoryListActivity : MvxFragmentCompatActivity<SelectCategoryListViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.fragment_category_list);
SetSupportActionBar(FindViewById<Toolbar>(Resource.Id.toolbar));
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
}
...
The Warning I get is:
[0:] MvxBind:Warning: 5.11 Failed to create target binding for binding SelectedItem for SelectedCategory
Ideas?

I just came across a similar issue with a SelectedItem binding failing. The solve was to update Mvx to version 4.1.6. The reason being is that you need to register MvxAppCompatSetupHelper.FillTargetFactories, with 4.1.6 this is now done automatically.
Alternatively, you can manually register it in you setup.cs by overriding FillTargetFactories:
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
MvxAppCompatSetupHelper.FillTargetFactories(registry);
base.FillTargetFactories(registry);
}

Related

Why Fragment SupportMapFragment has not been attached yet

I'm trying to implement supportmapfragment on my fragment but for some reason it gives me error. What I'm doing wrong?
Error I'm getting: java.lang.IllegalStateException: Fragment SupportMapFragment{67446a6} (09078183-572b-41c6-8795-7441157371d5) has not been attached yet.
supportMapFragment.getMapAsync(this). Is never called it crash before that
class MapFragment : Fragment(), OnMapReadyCallback {
private lateinit var googleMap: GoogleMap
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.fragment_map, container, false)
val supportMapFragment = SupportMapFragment()
supportMapFragment.childFragmentManager.findFragmentById(R.id.google_map)
supportMapFragment.getMapAsync(this)
return view
}
}
And fragment_map .xml file looks like:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/primaryBackground"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.fragment.app.FragmentContainerView
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/google_map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Adding:
private lateinit var mapFragment: SupportMapFragment
removing
val supportMapFragment = SupportMapFragment()
and changing
supportMapFragment.childFragmentManager.findFragmentById(R.id.google_map)
to
mapFragment = childFragmentManager.findFragmentById(R.id.google_map) as SupportMapFragment
fixes this

when i click to recyclarview iteam it should hide from my recyclarview

I want to make that people visit my website one time one site per person.
i am using mysql database for my data base .
i want to make when the user click on the visit button the he will redirect to the site (i had configured).
and that persion not able to go same site again. the button should show visited and its should be disable or hide form recyclar view (How to do,I want this).
My adapter class:
public class MyAdapter extends RecyclerView.Adapter<ImageViewHOlder> {
private Context context;
private List<ModelImage> imageList;
public MyAdapter(Context context, List<ModelImage> imageList) {
this.context = context;
this.imageList = imageList;
}
#NonNull
#Override
public ImageViewHOlder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_layout_item,parent,false);
return new ImageViewHOlder(view);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHOlder holder, final int position) {
holder.title.setText(imageList.get(position).getPost_title());
holder.date.setText("Uploaded on: "+imageList.get(position).getDate());
Glide.with(context).load(imageList.get(position).getFeatured_image()).into(holder.imageView);
holder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String url = imageList.get(position).getPost_slug();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
context.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return imageList.size();
}
}
class ImageViewHOlder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView title,date;
Button check;
public ImageViewHOlder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
title = itemView.findViewById(R.id.title);
date = itemView.findViewById(R.id.date);
check = itemView.findViewById(R.id.check);
}
}
my custom layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp"
android:layout_marginEnd="5dp"
android:background="#color/colorPrimaryDark"
android:text="Visit"
android:textColor="#000"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView"
android:padding="5dp"
android:layout_marginTop="7dp"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/date"
android:layout_marginEnd="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/check"
android:layout_alignParentEnd="true"
android:layout_marginStart="15dp"
android:textSize="18sp"
android:textStyle="italic" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginStart="15dp"
android:layout_toEndOf="#+id/imageView"
android:layout_toStartOf="#+id/check"
android:singleLine="true"
android:textSize="20sp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_toEndOf="#+id/imageView"
android:singleLine="true"
android:textSize="20sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>

customizing Tab text color, stacked color, and swipe functionality in Xamarin Android?

I have three tabs in xamarin android, I have used tab Host to create those tabs. Now, I want to change the text color in those tabs, and I want to use the swipe effect just like the Tabbed Page in Xamarin Forms. How can I achieve this? Please do tell me the library also ,if required?
I want to change the text color in those tabs, and I want to use the swipe effect just like the Tabbed Page in Xamarin Forms
You could use TabLayout, Usage like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tablayout"
app:tabTextColor="#color/colorPrimary"
app:tabSelectedTextColor="#color/colorAccent"
app:tabIndicatorColor="#android:color/holo_orange_light">
<android.support.design.widget.TabItem
android:text="tab 1"/>
<android.support.design.widget.TabItem
android:text="tab 2"/>
<android.support.design.widget.TabItem
android:text="tab 3"/>
</android.support.design.widget.TabLayout>
</LinearLayout>
Attribute to change the text color :
app:tabTextColor --> The default text color to be applied to tabs.
app:tabSelectedTextColor --> The text color to be applied to the currently selected tab.
app:tabIndicatorColor --> Color of the indicator used to show the currently selected tab.
Effect like this.
use the swipe effect just like the Tabbed Page in Xamarin Forms
In Xamarin.Forms, tabs is associated with Page, actually when it render in native Android, it was associated with ViewPager. So if you want implement the swipe effect feature, you have to write the code by yourself. For more details, you could read the document.
EDIT :
To use TabLayout, you have to add Xamarin.Android.Support.v4 and Xamarin.Android.Support.Design nuget package.
EDIT 2 ;
Here is a simple demo about implement the swipe effect :
public class MainActivity : AppCompatActivity
{
private List<Android.Support.V4.App.Fragment> mFragments = new List<Android.Support.V4.App.Fragment>();
private List<string> mTabTitles = new List<string>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
TabLayout tabLayout = FindViewById<TabLayout>(Resource.Id.tablayout);
ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.view_pager);
initTab();
mFragments.Add(new Fragment1());
mFragments.Add(new Fragment1());
mFragments.Add(new Fragment1());
mFragments.Add(new Fragment1());
viewPager.Adapter = new ViewPagerAdapter(SupportFragmentManager, mFragments,mTabTitles);
tabLayout.SetupWithViewPager(viewPager);
}
private void initTab()
{
for (int i = 1; i < 5; i++)
{
mTabTitles.Add("Tab" + i);
}
}
}
public class ViewPagerAdapter : FragmentPagerAdapter
{
private List<string> mTabTitles;
private static int FRAGMENT_COUNT = 4;
public ViewPagerAdapter(Android.Support.V4.App.FragmentManager fragmentManager, List<Android.Support.V4.App.Fragment> fragments, List<string> tabTitles):base(fragmentManager)
{
mTabTitles = tabTitles;
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
Fragment1 testFragment = new Fragment1();
return testFragment;
}
public override int Count => FRAGMENT_COUNT;
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(mTabTitles[position]);
}
}
axml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tablayout"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
Effect.
EDIT 3 :
In my demo, Fragment should use Android.Support.V4.App.Fragment, code like this :
public class Fragment1 : Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
return inflater.Inflate(Resource.Layout.YourFragment, container, false);
return base.OnCreateView(inflater, container, savedInstanceState);
}
}

How to dynamically add MvxFrameControl in MvvmCross Android

I need to dynamically create MvxFrameControll, inflate it with content and add to another Layout (FrameLayout / LinearLayout).
My code is
HomeScreen.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<krista.fm.iMonitoring.Android.Native.TopBar
android:layout_width="match_parent"
android:layout_height="50dp"
local:MvxBind="DataContext TopBar"
local:MvxTemplate="#layout/topbar"
android:id="#+id/TopBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/SubjectBodyContainer" />
</LinearLayout>
public class SubjectBody : MvxFrameControl
{
public SubjectBody (Context context, IAttributeSet attrs) : base (context, attrs)
{
}
public SubjectBody (int templateId, Context context, IAttributeSet attrs) : base (context, attrs)
{
}
protected override void OnContentSet ()
{
base.OnContentSet ();
}
}
HomeScreenView
[Activity (Label = "HomeScreen")]
public class HomeScreenView : MvxActivity
{
public override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
}
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.HomeScreen);
AddSubjectBody();
}
protected override void OnStart()
{
base.OnStart();
}
protected virtual void AddSubjectBody()
{
var container = FindViewById<FrameLayout>(Resource.Id.SubjectBodyContainer);
var subjectBody = new SubjectBody(Resource.Layout.SubjectBody, this, null);
subjectBody.DataContext = new SubjectBodyViewModel();
container.AddView(subjectBody, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
}
}
Why this code doesn't add content on screen and how it rewrite rightly? When I use SubjectBody adding inside axml with templateId it works correctly. But what if i need adding this dynamically?
protected virtual void AddSubjectBody()
{
var container = FindViewById<FrameLayout>(Resource.Id.SubjectBodyContainer);
var subjectBody = new SubjectBody(Resource.Layout.SubjectBody, this, null);
subjectBody.DataContext = new SubjectBodyViewModel();
container.AddView(subjectBody, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
}
Are you sure your code is running on the UI thread? I'm using pretty a similar code in one app and the view gets added into the parent. What I suggest you to try is:
this.RunOnUiThread(() => container.AddView(this.subjectBody));

App unfortunately stopped Working

This is my code for MainActivity.java
Here I am creating a very basic LoginApp. When the App runs, it gives "unfortunately stopped working ERROR..."
package com.example.loginapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText t1,t2;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button) b1.findViewById(R.id.button1);
t1=(EditText) t1.findViewById(R.id.editText1);
t2=(EditText) t2.findViewById(R.id.editText2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View arg0)
{
//String user;
//user = t1.setText("user");
if (arg0.findViewById(R.id.button1)==b1)
{
if ((boolean)((t1.getText().toString()=="user")) && (t2.getText().toString()=="user"))
{
Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
t1.setText("");
t2.setText("");
}
}
}
}
Following is code for activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/editText1"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_centerVertical="true"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="115dp"
android:layout_marginTop="22dp"
android:ems="10" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/editText1"
android:ems="10"
android:inputType="numberPassword" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginTop="46dp"
android:layout_toRightOf="#+id/textView2"
android:clickable="true"
android:onClick="onClick"
android:text="Login" />
</RelativeLayout>
Code for AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.loginapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Hi please repleace the onCreate method as
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button) findViewById(R.id.button1);
t1=(EditText) findViewById(R.id.editText1);
t2=(EditText) findViewById(R.id.editText2);
}
It will helpful for you.
After modified the code it working fine for me.Please try and let me know.