кнопка просмотра списка не отправляет правильный номер на другой вид деятельности - PullRequest
0 голосов
/ 14 января 2020

Кнопки 1-7 (по одной для каждой строки) правильно отправляют номера 1-7 на экран / операцию редактирования компонента (номера соответствуют строке, в которой находится кнопка, в зависимости от «положения»). Однако, когда я нажимаю на кнопку 8-10, он по какой-то причине отправляет номера 1-3 на операцию / экран редактирования компонента, и когда я снова повторяю кнопки 1-3, все числа, отправленные на операцию редактирования компонента, не синхронизированы. c.

Я не понимаю, как кнопки для строк 1-7 отправляют правильные числа, но внезапно строки 8 и выше не делают.

Пользовательский адаптер просмотра списка класс

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;

        //Row
        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.listview_row, null, false);
        }

        //Set component details
        TextView Category = row.FindViewById<TextView>(Resource.Id.txtViewCategory);
        Category.Text = allComponents[position].CategoryName;

        TextView Name = row.FindViewById<TextView>(Resource.Id.txtViewName);
        Name.Text = allComponents[position].Name;

        TextView Price = row.FindViewById<TextView>(Resource.Id.txtViewPrice);
        Price.Text = allComponents[position].Price;

        ImageButton editComponent = row.FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);

        //Take the user to the edit screen for a given component
        if (!editComponent.HasOnClickListeners)
        {
            editComponent.Click += (sender, e) =>
            {
                // Declare the activityas intent
                var intent = new Intent(mContext, typeof(Edit_componentActivity));

                //Store the row position
                var rowPostion = (position + 1);

                //Transfer the component's row to the edit screen
                intent.PutExtra("edit_component_row_position", rowPostion);

                //Start the activity of intent
                mContext.StartActivity(intent);
            };
        }

        return row;
    }

Активность компонента редактирования

        // Create your application here
        SetContentView(Resource.Layout.edit_component);

        //Get the component's row position
        var editComponentRowPosition = Intent.GetIntExtra("edit_component_row_position", 0);

        Toast.MakeText(this, editComponentRowPosition.ToString(), ToastLength.Long).Show();

        //Get the notes text view of the screen
        TextView editName = FindViewById<TextView>(Resource.Id.txtEditEditComponentName);

        //Assign the notes text view the component's notes
        editName.Text = editComponentRowPosition.ToString();

Вот несколько скриншотов.

1 Ответ

0 голосов
/ 15 января 2020

Я воспроизвел вашу проблему на моем локальном сайте. Причина в том, что с помощью следующего кода для добавления нажатия для кнопки он не будет контролироваться. При нажатии метод GetView будет продолжать вызывать этот метод. Его не рекомендуется использовать в адаптере.

editComponent.Click += (sender, e) 

Решение , использующее SetOnClickListener для его замены. Как следует:

ImageButton editComponent = row.FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);
ImageButton.Tag = position; //Set position as Tag for button
ImageButton.SetOnClickListener(this);

public void OnClick(View v)
{
    int position = (int)v.Tag;
    Console.WriteLine("-------------button click---------------" + (position + 1));
    // Declare the activityas intent
    var intent = new Intent(mContext, typeof(Edit_componentActivity));

    //Store the row position
    var rowPostion = (position + 1);

    //Transfer the component's row to the edit screen
    intent.PutExtra("edit_component_row_position", rowPostion);

    //Start the activity of intent
    mContext.StartActivity(intent);
}

Не забудьте унаследовать View.IOnClickListener для класса адаптера.

...