0

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

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

1 回答 1

1

不要自己删除由适配器支持的视图,因为它可能会导致奇怪的行为!您对 BaseAdapter 的实现对我来说也很奇怪。IE:

 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();
于 2012-05-25T13:54:39.240 回答