android - 如何在Listview中的某个位置删除视图

时间:2012-05-25 13:41:47

标签: android android-listview

我想知道如何在列表视图中的某个位置删除子视图,并在列表顶部添加相同的视图。我尝试使用以下方法但是会生成不受支持的异常。怎么可以请任何人帮助我。

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;        
    } 
}

1 个答案:

答案 0 :(得分:1)

请勿自行删除适配器支持的视图,因为这可能会导致令人厌烦的行为!您对BaseAdapter的实现对我来说也很奇怪。即:

 public Object getItem(int position)
{
    return position;
}

public long getItemId(int position) {
    return position;
}

似乎没有任何意义!

您应该使用ArrayAdapter将模型传递给它,并相应地实现getView(int, View, ViewGroup)。如果你想在Top里面移动一个Item,你所要做的只是:

ArrayAdapter adapter = //initialize with your Model Objects and set it as ListAdapter
Object someItemInsideList = //some Item
adapter.remove(someItemInsideList);
adapter.insert(someItemInsideList, 0);
adapter.notifyDataSetChanged();