如何为列表视图中的每个项目设置更新和删除按钮

activity_main.xml


 <?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

     xmlns:app="http://schemas.android.com/apk/res-auto"

     xmlns:tools="http://schemas.android.com/tools"

     android:layout_width="match_parent"

     android:layout_height="match_parent"

     tools:context=".MainActivity">


     <ScrollView

         android:id="@+id/scroll_view"

         android:layout_width="match_parent"

         android:layout_height="match_parent">


         <LinearLayout

             android:id="@+id/linearlayout1"

             android:layout_width="match_parent"

        android:layout_height="match_parent"

             android:orientation="vertical">


             <TextView

                 android:id="@+id/tv_name1"

                 android:layout_width="match_parent"

                 android:layout_height="wrap_content"

                 android:layout_marginLeft="30dp"

                 android:layout_marginTop="15dp"

                 android:layout_marginRight="30dp"

                 android:padding="15dp"

                 android:text="Name:"

                 android:textSize="15sp" />


             <EditText

                 android:id="@+id/et_name1"

                 android:layout_width="match_parent"

                 android:layout_height="wrap_content"

                 android:layout_marginLeft="30dp"

                 android:layout_marginRight="30dp"

                 android:hint="Enter Your Name"

                 android:padding="15dp"

                 android:textSize="15sp" />


            </LinearLayout>


         </LinearLayout>

    </ScrollView>



 </RelativeLayout>


一只名叫tom的猫
浏览 106回答 2
2回答

萧十郎

您需要在列表项的布局中添加两个按钮,例如更新和删除。所以列表持有者类看起来像:class ViewHolder {&nbsp; &nbsp; TextView tv_name;&nbsp; &nbsp; TextView tv_email;&nbsp; &nbsp; TextView tv_contact;&nbsp; &nbsp; TextView tv_dob;&nbsp; &nbsp; TextView tv_qualification;&nbsp; &nbsp; TextView tv_time;&nbsp; &nbsp; Button btn_update; //Update for item&nbsp; &nbsp; Button btn_delete; //Delete an item.}完成此操作后,请在适配器的 getView() 方法中执行以下操作。仔细观察注释,进一步理解实现逻辑。&nbsp; &nbsp; public View getView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; ViewHolder holder;&nbsp; &nbsp; if (convertView == null) {&nbsp; &nbsp; &nbsp; &nbsp; holder = new ViewHolder();&nbsp; &nbsp; &nbsp; &nbsp; convertView = LayoutInflater.from(mContext).inflate(R.layout.second_layout, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name1);&nbsp; &nbsp; &nbsp; &nbsp; holder.tv_contact = (TextView) convertView.findViewById(R.id.tv_phoneno);&nbsp; &nbsp; &nbsp; &nbsp; holder.tv_email=(TextView)convertView.findViewById(R.id.tv_email);&nbsp; &nbsp; &nbsp; &nbsp; holder.tv_dob=(TextView)convertView.findViewById(R.id.tv_dob);&nbsp; &nbsp; &nbsp; &nbsp; holder.tv_qualification=(TextView)convertView.findViewById(R.id.tv_qualification);&nbsp; &nbsp; &nbsp; &nbsp; holder.tv_time=(TextView)convertView.findViewById(R.id.tv_time);&nbsp; &nbsp; &nbsp; &nbsp; //bind holder.btn_update and holder.btn_delete here&nbsp; &nbsp; &nbsp; &nbsp; convertView.setTag(holder);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; holder = (ViewHolder) convertView.getTag();&nbsp; &nbsp; }&nbsp; &nbsp; holder.tv_name.setText(mPersonList.get(position).getName());&nbsp; &nbsp; holder.tv_contact.setText(mPersonList.get(position).getContactno());&nbsp; &nbsp; holder.tv_email.setText(mPersonList.get(position).getEmail());&nbsp; &nbsp; holder.tv_dob.setText(mPersonList.get(position).getDatepicker());&nbsp; &nbsp; holder.tv_qualification.setText(mPersonList.get(position).getQualification());&nbsp; &nbsp; holder.tv_time.setText(mPersonList.get(position).getTimepicker());&nbsp; &nbsp; holder.btn_update.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Call your dialog to update and pass "mPersonList.get(position)" model so that data in the model will be updated.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Once update is done call refreshList() from the confirmation dialog button.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; holder.btn_delete.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mPersonList.remove(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; refreshList();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return convertView;}private void refreshList() {&nbsp; &nbsp; notifyDataSetChanged();}一个建议,尽量避免像学生那样使用 button1、button2、editText1、textView1 等变量。取而代之的是,为变量使用适当且有意义的名称。

守着星空守着你

您可以使用按钮单击侦听器将逻辑放在 getView() 方法中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java