从 List Adapter Android 中的按钮单击编辑 EditText

Posted

技术标签:

【中文标题】从 List Adapter Android 中的按钮单击编辑 EditText【英文标题】:Edit EditText from button click inside a List Adapter Android 【发布时间】: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 【参考方案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 的布局不同

以上是关于从 List Adapter Android 中的按钮单击编辑 EditText的主要内容,如果未能解决你的问题,请参考以下文章

Android List Adapter

Android-Listview中Bitmap的缓存实现有哪些方式

android如何将从数据库读取的数据显示在listview中

Android List + Adapter 仅显示第一个条目

Android 自定义 ListView/Adapter

如何清空android ListView控件的内容