0

我有两个清单(List1, List2)。我为此实现了拖放功能(从 List1 到 List2)。对于 List1,我this(setOnItemSelectedListener)对选定项目使用了侦听器。如果您不滚动,它工作正常意味着它的返回列表项位置。

假设如果您正在滚动然后尝试选择该项目然后它返回可见项目的列表项目位置。(例如,您的列表在滚动后从 1 到 10 项目 4 项目是您列表的第一项,然后用户假设选择 7 项目列表然后它返回位置为4。)如何获取列表的确切位置。这个监听器也一样setOnItemReceiverListener

code
/**
 * Set selected Listener to know what item must be drag
 */
lvStudies.setOnItemSelectedListener(mOnItemSelectedListener);

/**
 * Listener to know on what position the new item must be insert
 */
 lvAddedContacts.setOnItemReceiverListener(listenerReceivePicture);    

/**
 * Save selected item
 */
private AdapterView.OnItemSelectedListener mOnItemSelectedListener = new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
            long arg3) {

        /**
         * retrieve selected item from adapterview
         */
        nStudiesMovePosition = position;
        System.out.println("position"+position);            
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub          
    }
};

 private OnItemClickListener listenerReceivePicture = new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
    {       
        System.out.println("Position:"+position);
    }
    };

//Adapter for studies list
private class StudiesListViewAdapter extends BaseAdapter 
{
    private LayoutInflater mInflater;        

    public StudiesListViewAdapter(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.StudiesList_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.studieslist_item, null);
        holder = new ViewHolder();

        //getting the id's
        holder.tvPatientName = (TextView) convertView.findViewById(R.id.SLI_PatientName_tv);
        holder.tvStudy = (TextView) convertView.findViewById(R.id.SLI_Study_tv);           

        //Patient Name            
        String strTotal = DH_Constant.StudiesList_obj.response.get(position).PatientName;
        String strPName = strTotal.substring(0, strTotal.indexOf("(Dt:"));
        holder.tvPatientName.setText(strPName);

        //Study
        String strStudy = strTotal.substring(strTotal.indexOf("(Dt:"));
        strStudy = strStudy.replace("Dt: ", "");
        strStudy = strStudy.replace("Study: ", "");           
        holder.tvStudy.setText(strStudy);

        return convertView;
    }   
    class ViewHolder 
    {
       TextView tvPatientName,tvStudy;                  
    } 
}   

//Adapter for added contacts
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;          

//           if (convertView == null) 
//           {
             convertView = mInflater.inflate(R.layout.added_contacts_list, null);
             holder = new ViewHolder();
//                 convertView.setTag(holder);
//           } 
//           else 
//           {
//               holder = (ViewHolder) convertView.getTag();
//           }

        //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 strUserName = DH_Constant.AddedContactsList_obj.response.get(position).Email;
        System.out.println("strUserName:"+strUserName);

        //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));   
            String strrr =strUserName.substring(0, strUserName.indexOf('-'));
            strUserName = DH_Constant.AddedContactsList_obj.response.get(position).Name +"("+ strrr+")";             
        }
        else
        {
            holder.tvName.setTextColor(Color.BLUE);             
        }

        holder.tvName.setText(strUserName);

        //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);
            }
        });                                             
        return convertView;
    }   

    class ViewHolder 
    {
       TextView tvName;
       Button btnRemove;        
    } 
}
4

1 回答 1

0

我不知道这是否是错误,但是当您滚动 ListView 时,位置会变得混乱。
在您的情况下,如果您可以使用 id( long arg3 in your source code),请使用它。
滚动时有很多关于位置的工作,但它可能很混乱。

于 2012-11-02T07:02:54.183 回答