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 の実装も私には奇妙に見えます。すなわち:

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

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

意味がないようです!

モデルを渡すArrayAdapterを使用し、それに応じて実装する必要がありますgetView(int, View, ViewGroup)。次に、アイテムを上に移動したい場合は、次のことを行うだけです。

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 に答える