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 等变量。取而代之的是,为变量使用适当且有意义的名称。
添加回答
举报