2012-05-25 24 views
0

我想知道如何删除列表视图中某个位置的子视图,并将其添加到列表顶部。我试着使用下面的方法,但会产生不受支持的异常。请问这有什么可以帮助我的人。android-如何在列表视图中的某个位置删除视图

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

请勿自行删除由适配器支持的视图,因为这可能会导致更坏的行为! BaseAdapter的实现对我来说也很陌生。即:

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

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

似乎没有任何意义!

您应该使用ArrayAdapter将模型传递给它,并相应地实现getView(int, View, ViewGroup)。如果你想在上面移动一个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(); 
+0

在listview中,每个项目都是一个自定义视图。我使用来自Web服务的数据设置自定义视图。这里我没有使用arrayadapter – naresh

相关问题