亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為列表視圖中的每個項目設置更新和刪除按鈕

如何為列表視圖中的每個項目設置更新和刪除按鈕

一只名叫tom的貓 2023-02-16 15:16:14
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>
查看完整描述

2 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

您需要在列表項的布局中添加兩個按鈕,例如更新和刪除。所以列表持有者類看起來像:


class ViewHolder {

    TextView tv_name;

    TextView tv_email;

    TextView tv_contact;

    TextView tv_dob;

    TextView tv_qualification;

    TextView tv_time;

    Button btn_update; //Update for item

    Button btn_delete; //Delete an item.

}

完成此操作后,請在適配器的 getView() 方法中執行以下操作。仔細觀察注釋,進一步理解實現邏輯。


    public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {

        holder = new ViewHolder();

        convertView = LayoutInflater.from(mContext).inflate(R.layout.second_layout, parent, false);

        holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name1);

        holder.tv_contact = (TextView) convertView.findViewById(R.id.tv_phoneno);

        holder.tv_email=(TextView)convertView.findViewById(R.id.tv_email);

        holder.tv_dob=(TextView)convertView.findViewById(R.id.tv_dob);

        holder.tv_qualification=(TextView)convertView.findViewById(R.id.tv_qualification);

        holder.tv_time=(TextView)convertView.findViewById(R.id.tv_time);


        //bind holder.btn_update and holder.btn_delete here


        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();

    }


    holder.tv_name.setText(mPersonList.get(position).getName());

    holder.tv_contact.setText(mPersonList.get(position).getContactno());

    holder.tv_email.setText(mPersonList.get(position).getEmail());

    holder.tv_dob.setText(mPersonList.get(position).getDatepicker());

    holder.tv_qualification.setText(mPersonList.get(position).getQualification());

    holder.tv_time.setText(mPersonList.get(position).getTimepicker());


    holder.btn_update.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            //Call your dialog to update and pass "mPersonList.get(position)" model so that data in the model will be updated.

            //Once update is done call refreshList() from the confirmation dialog button.

        }

    });


    holder.btn_delete.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            mPersonList.remove(position);

            refreshList();

        }

    });


    return convertView;


}


private void refreshList() {

    notifyDataSetChanged();

}

一個建議,盡量避免像學生那樣使用 button1、button2、editText1、textView1 等變量。取而代之的是,為變量使用適當且有意義的名稱。


查看完整回答
反對 回復 2023-02-16
?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

您可以使用按鈕單擊偵聽器將邏輯放在 getView() 方法中。



查看完整回答
反對 回復 2023-02-16
  • 2 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號