convert Java to C #, xamarin android

1

Hello, someone could help me with this code, what happens is that I'm having problems passing it to c #

   @Override
   public void onBindViewHolder(ViewHolder viewHolder, int position) {
   final int pos = position;
   viewHolder.tvName.setText(stList.get(position).getName());
   viewHolder.tvEmailId.setText(stList.get(position).getEmailId());
   viewHolder.chkSelected.setChecked(stList.get(position).isSelected());
   viewHolder.chkSelected.setTag(stList.get(position));

This is the part with which I have more problems, the previous thing already solved it

   viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        CheckBox cb = (CheckBox) v;
        Student contact = (Student) cb.getTag();

        contact.setSelected(cb.isChecked());
        stList.get(pos).setSelected(cb.isChecked());

        Toast.makeText(
                v.getContext(),
                "Clicked on Checkbox: " + cb.getText() + " is "
                        + cb.isChecked(), Toast.LENGTH_LONG).show();
    }
});
    
asked by Rafael 31.08.2016 в 21:18
source

3 answers

1

You can use something like this:

{
    //...   
    viewHolder.chkSelected.Click += chkSelected_Click;
}

private void chkSelected_Click(object sender, EventArgs e)
{
    CheckBox cb = (CheckBox)sender;
    Student contact = (Student) cb.Tag;

    //  Asumiendo que usas propiedades
    contact.Selected = cb.Checked;
    stList[pos].Selected = cb.Checked;

    Toast.MakeText(
                cb.Context,
                "Clicked on Checkbox: " + cb.Text + " is "
                        + cb.Checked, ToastLength.Long).Show();
}
    
answered by 20.09.2016 в 07:37
0

First you have to define the following class:

public class JavaObject<T> : Java.Lang.Object
{
    public JavaObject(T obj)
    {
        Value = obj;
    }

    public T Value { get; private set; }
}

In the case of the first part you would do something like that for the Tag.

view.Tag = new JavaObject<Student>(stList[position]);

And in the last part you would do the following:

viewHolder.chkSelected.Click += (s,a) => 
{
    var checkbox = (CheckBox)s;
    var contact = ((JavaObject<Student>)checkbox.Tag).Value;

    //...
};
    
answered by 09.09.2016 в 23:32
0

You can not "translate" it as such, because what this piece of code does in java does not have direct translation in C #

viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    //manejar el click
    }
}

What you are doing is creating an on-the-fly implementation of the View.onClickListener interface, to assign it to chkSelected.

In C # you can not do that, it would be normal for the view (Activity, Fragment, Adapter, etc.) that is using that widget to implement the interface.

It would be something like this:

MiAdapter : View.IOnClickListener
{
    public void OnClick()
    {
    //maneja el click
    }

    ...
    //cuando tengas que asignaler el listener:
    viewHolder.chkSelected.SetOnClickListener(this);

}

Or directly use the click event:

viewHolder.chkSelected.Click += (s,e)=>{//maneja el click};
    
answered by 20.10.2016 в 21:30