如何使用两个或三个SetOnClickListener修复我的列表视图

时间:2019-02-03 15:50:05

标签: c# android xamarin xamarin.android

我是xamarin android的初学者,正在寻找一种使我的代码能够正常工作的方法,

我的代码包含三个textview标题,主要和历史记录,以及每次单击两个按钮共享按钮和计数器

public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];
            View view = convertView;
            if (view == null) // no view to re-use, create new
                view = context.LayoutInflater.Inflate(Resource.Layout.list_ticket_view, null);
            view.FindViewById<TextView>(Resource.Id.titletxt).Text = item.title;
            view.FindViewById<TextView>(Resource.Id.maintxt).Text = item.main;
            view.FindViewById<TextView>(Resource.Id.txthistory).Text = item.history;

            var share_butn = view.FindViewById<ImageView>(Resource.Id.share_butn);



            var button1 = view.FindViewById<Button>(Resource.Id.button1);
            button1.Text = (item.counter).ToString();
            button1.SetOnClickListener(this);


            return view;
        }



        public void OnClick(View v)
        {
            var button = v as Button;
            int count = int.Parse(button.Text) - 1;
            //new two line under this:
            button.Text = count.ToString();
            if (count < 0)
            {
                count = 0;
                button.Clickable = false;

            }
            button.Text = count.ToString();

        }

我的计数器也可以工作,但是我找不到通过share_butn进行协调的方法

更新:我想在OnClick内放置以下代码

Intent intentsend = new Intent();
                        intentsend.SetAction(Intent.ActionSend);
                        intentsend.PutExtra(Intent.ExtraText,item.title+"\n"+item.main+"\n"+item.history);
                        intentsend.SetType("text/plain");
                        context.StartActivity(intentsend);

2 个答案:

答案 0 :(得分:1)

您还可以将点击修饰符添加到imageview中:

share_butn.SetOnClickListener(this);
share_butn.Tag = position;

然后在OnClick中,您可以通过ID判断它是否为share_butnor按钮:

public void OnClick(View v)
    {
       if(v.Id == Resource.Id.share_butn){
         int position = (int)v.Tag;
         Intent intentsend = new Intent();
         intentsend.SetAction(Intent.ActionSend);
         intentsend.PutExtra(Intent.ExtraText,item[position].title+"\n"+item[position].main+"\n"+item[position].history);
         intentsend.SetType("text/plain");
         context.StartActivity(intentsend);          
       }
       if(v.Id == Resource.Id.button1){
         var button = v as Button;
        int count = int.Parse(button.Text) - 1;
        //new two line under this:
        button.Text = count.ToString();
        if (count < 0)
        {
            count = 0;
            button.Clickable = false;

        }
        button.Text = count.ToString();
       }


    }

答案 1 :(得分:0)

由于您为1. Command to compile your test class >> **javac -cp junit-4.12.jar;. UserDAOTest.java ProductDAOTest.java** --> general command is >> **javac -cp <junit-jar-file>;. TestClass1.java TestClass2.java** 2. Run the test cases >> ** java -cp <junit-jar>;<hamcrest-jar>;. org.junit.runner.JUnitCore TestClass1 TestClass2 ** 3. Running the unit test cases by Maven Command Line >> mvn test >> mvn clean test >> mvn clean compile test 使用了本地Xamarin,并且Android有自己的监听点击事件的方式,因此建议您使用以下方式:

Xamarin.Android

更新:

尝试使用lambda表达式代替上面的代码

button1.Click+= HandleClick; // Applying the event

private void HandleClick(object sender, EventArgs e)
{
  // Click event code
}