【问题标题】:Edit EditText from button click inside a List Adapter Android从 List Adapter Android 中的按钮单击编辑 EditText
【发布时间】:2015-12-02 17:54:34
【问题描述】:

我有一个 ListView 和一些 EditTexts 在它下面。列表中的每一行都有一个编辑按钮,单击该按钮应填满 Listview 下方的 EditTexts。现在的问题是我为 ListView 定义了一个自定义适配器。当我在 Adapter 类中膨胀布局并且我的 EditTexts 在另一个中时,如何在单击 ListView 行内的按钮时填充 EditText?

编辑

这是我的适配器代码

public class CompleteCommentsAdapter : BaseAdapter 
    {
        private Activities.CommentListActivity commentListActivity;
        private List<Comments> dummyCommentList;
        private TextView txtUserName;
        private TextView txtCommentTime;
        private ImageView imgUserImage;
        private TextView txtCommentText;
        private ImageButton ibtnEdit;
        private ImageButton itbtnDelete;

        public CompleteCommentsAdapter(Activities.CommentListActivity commentListActivity, List<Comments> dummyCommentList)
        {
            // TODO: Complete member initialization
            this.commentListActivity = commentListActivity;
            this.dummyCommentList = dummyCommentList;
        }
        public override int Count
        {
            get { return  dummyCommentList.Count(); }
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            if (convertView == null)
            {

                convertView = commentListActivity.LayoutInflater.Inflate(Resource.Layout.comment_list_row, null);
                 txtUserName = convertView.FindViewById<TextView>(Resource.Id.txtCommenterName);
            txtCommentTime = convertView.FindViewById<TextView>(Resource.Id.txtCommenterTime);
            imgUserImage = convertView.FindViewById<ImageView>(Resource.Id.imgProfileUserImage);
            txtCommentText = convertView.FindViewById<TextView>(Resource.Id.txtCommentText);
            ibtnEdit = convertView.FindViewById<ImageButton>(Resource.Id.imgBtnEdit);
            itbtnDelete = convertView.FindViewById<ImageButton>(Resource.Id.imgBtnDelete);
            }


            Comments mComments = dummyCommentList.ElementAt(position);

            txtUserName.Text = mComments.UserName;
            txtCommentTime.Text = mComments.CommentTime;



            txtCommentText.Text = mComments.CommentText;
            ibtnEdit.Click += ibtnEdit_Click;
           return convertView;
        }

        void ibtnEdit_Click(object sender, EventArgs e)
        {

        }
    }

【问题讨论】:

  • 在适配器中为您的编辑文本设置 onclick() 方法
  • @GaneshGudghe 如何从我的适配器访问 EditText?
  • 列表视图中有 2 种不同类型的适配器?或 2 个列表视图?提供一些代码让我们看看。
  • @helloworld 请发布您的适配器代码
  • 检查this

标签: android listview android-listview listadapter


【解决方案1】:

如果必须将侦听器附加到按钮,而不是整行,您可以将这些编辑文本的引用传递给适配器。或者,您可以在您的应用程序中构建一个 EventBus,通过不同的类发送 Clicked Text。

【讨论】:

    【解决方案2】:
         public override View GetView(int position, View convertView, ViewGroup parent)
                    {
                        if (convertView == null)
                        {
    
                            convertView = commentListActivity.LayoutInflater.Inflate(Resource.Layout.comment_list_row, null);
                             txtUserName = convertView.FindViewById<TextView>(Resource.Id.txtCommenterName);
                        txtCommentTime = convertView.FindViewById<TextView>(Resource.Id.txtCommenterTime);
                        imgUserImage = convertView.FindViewById<ImageView>(Resource.Id.imgProfileUserImage);
                        txtCommentText = convertView.FindViewById<TextView>(Resource.Id.txtCommentText);
                        ibtnEdit = convertView.FindViewById<ImageButton>(Resource.Id.imgBtnEdit);
                        itbtnDelete = convertView.FindViewById<ImageButton>(Resource.Id.imgBtnDelete);
                        }
    
    
                        Comments mComments = dummyCommentList.ElementAt(position);
    
                        txtUserName.Text = mComments.UserName;
                        txtCommentTime.Text = mComments.CommentTime;
    
                        txtCommentTime.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
    //do what you want here
    
    }
                        });
    
    
                       return convertView;
                    }
    

    【讨论】:

      【解决方案3】:

      编辑按钮:

       ibtnEdit.setOnClickListener(new OnClickListener() {
           @Override
             public void onClick(View v) {
             txtUserName.setText(""); 
             txtCommentTime.setText("");
             txtCommentText.setText("");
             }
       });
      

      【讨论】:

      • txtUserName 与 ListView 的布局不同
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 2018-12-07
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多