How to get last ListView item position

19,931

Solution 1

Hold a boolean to say whether user has himself selected anything. Let the boolean be userSelected = false;

Now you could do in getView():

if(position==getCount()-1 && userSelected==false)
    holder.radioBtn.setChecked(true);
else
    holder.radioBtn.setChecked(false);

In onItemClick, when the user has made a selection himself, just set userSelected=true;

Solution 2

Your last position is , int lastpostion = getcount() -1; where getcount() is a method in the adpater class

Share:
19,931
androidcodehunter
Author by

androidcodehunter

Working at VocaPower as an android developer. My daily activity is to implement new feature, improve efficiency, fix bug and checkout different modern architecture for android. The app I am currently working is VocaPower – Mnemonic Dictionary for Vocabulary Your feedback will be accepted promptly and implemented in my app.

Updated on June 05, 2022

Comments

  • androidcodehunter
    androidcodehunter almost 2 years

    I created a listview with radiobutton. The listview is like the image below: enter image description here

    And my getView is:

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        PaymentData rowItem = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(
                    com.android.paypal.homeactivity.R.layout.list_item, null);
            holder = new ViewHolder();
            holder.radioBtn = (RadioButton) convertView
                    .findViewById(com.android.paypal.homeactivity.R.id.rdb_payment_method);
            holder.btPaymentMethod = (Button) convertView
                    .findViewById(com.android.paypal.homeactivity.R.id.bt_item_event);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
    
        holder.radioBtn.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                if ((position != mSelectedPosition) && (mSelectedRB != null)) {
                    mSelectedRB.setChecked(false);
                }
    
                mSelectedPosition = position;
                mSelectedRB = (RadioButton) v;
            }
        });
    
        if (mSelectedPosition != position) {
            holder.radioBtn.setChecked(false);
        } else {
            holder.radioBtn.setChecked(true);
            if (mSelectedRB != null && holder.radioBtn != mSelectedRB) {
                mSelectedRB = holder.radioBtn;
            }
        }
    
        holder.radioBtn.setText(rowItem.getRdbText());
        holder.btPaymentMethod.setText(rowItem.getBtText());
        return convertView;
    
    }
    

    I want to auto checked the last radiobutton when the screen comes first. How can I achieve this.

  • androidcodehunter
    androidcodehunter about 11 years
    At first I want to check the last position. Is this is last position then I want radion button setChecked(true).
  • androidcodehunter
    androidcodehunter about 11 years
    Exactly what I want. Thank you Aswin Kumar.
  • Sagar Vasoya
    Sagar Vasoya over 6 years
    it's Correct Answer