How to get last ListView item position
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
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, 2022Comments
-
androidcodehunter almost 2 years
I created a listview with radiobutton. The listview is like the image below:
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 about 11 yearsAt first I want to check the last position. Is this is last position then I want radion button setChecked(true).
-
androidcodehunter about 11 yearsExactly what I want. Thank you Aswin Kumar.
-
Sagar Vasoya over 6 yearsit's Correct Answer