我想知道如何在列表视图中的某个位置删除子视图并在列表顶部添加相同的子视图。我尝试使用以下方法,但它会生成不受支持的异常。请问怎么办,谁能帮帮我。
lvAddedContacts.removeViewAt(AddUserView,nAddUserPosition );//Here i want to remove this view from the list
lvAddedContacts.addView(AddUserView, 0); //Add the same at the top
lvAddedContacts.invalidate();//list is refreshed
contactsAdapter.notifyDataSetChanged();
private class ContactsListViewAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
public ContactsListViewAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
public int getCount()
{
int nListSize = DH_Constant.AddedContactsList_obj.response.size();
if(nListSize > 0)
{
return nListSize;
}
else
{
return 0;
}
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, final ViewGroup parent)
{
ViewHolder holder;
convertView = mInflater.inflate(R.layout.added_contacts_list, null);
holder = new ViewHolder();
//getting the id's
holder.tvName = (TextView) convertView.findViewById(R.id.xrays_Name_tv);
holder.btnRemove = (Button) convertView.findViewById(R.id.xrays_removebtn);
//Name
String strName = DH_Constant.AddedContactsList_obj.response.get(position).Name;
holder.tvName.setText(strName);
//Change the color for differentiate the dicom and non dicom users
if(DH_Constant.AddedContactsList_obj.response.get(position).IsDicomUser)
{
holder.tvName.setTextColor(Color.rgb(0, 135, 137));
}
else
{
holder.tvName.setTextColor(Color.BLUE);
}
//Remove button Listener
holder.btnRemove.setBackgroundResource(R.layout.xrays_contact_removebtn_widget);
holder.btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
lnIcontactId = DH_Constant.AddedContactsList_obj.response.get(position).ImportedContactsID;
nDicomUser = DH_Constant.AddedContactsList_obj.response.get(position).IsDicomUser?1:0;
//Alert for remove the contact
showDialog(DIALOG_removebtnalert);
}
});
//Copy the view and position if the user is added
if(DH_Constant.blnAddUserStatus)
{
System.out.println("IContactID(xrays):"+DH_Constant.lnAddUserID);
if(DH_Constant.AddedContactsList_obj.response.get(position).ImportedContactsID == DH_Constant.lnAddUserID)
{
nAddUserPosition = position;
AddUserView = convertView;
}
}
return convertView;
}
class ViewHolder
{
TextView tvName;
Button btnRemove;
}
}