Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:10:54+00:00 2026-06-17T22:10:54+00:00

I have a listview that uses a custom arrayadapter that loads a layout with

  • 0

I have a listview that uses a custom arrayadapter that loads a layout with text + a radiobutton. The user should only be able to select a single item in the list at a time – so basically only one radio button should be ‘checked’ at any time. This works perfectly except when the activity is recreated, like when the screen is rotated. I can’t figure out why it’s doing this, so maybe you all can think of something?

Here’s the code for the activity that has the listview:

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.widget.AdapterView;

public class TimeForm extends Activity implements AdapterView.OnItemClickListener {

    private MyArrayAdapter maa;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_form);
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);

        ListView lv = (ListView) findViewById(R.id.listView1);
        lv.setItemsCanFocus(false);

        String listitems[] = new String[16];

        listitems[0] = "Other";

        for(int i = 1; i < 16; i++)
        {
            listitems[i] = "Job " + i;
        }

        maa = new MyArrayAdapter(this, R.layout.layout_list, listitems);

        if(savedInstanceState != null)
            maa.selIndex = savedInstanceState.getInt("selIndex");

        lv.setAdapter(maa);
        lv.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        EditText et = (EditText) this.findViewById(R.id.editText1);

        if(position == 0)
        {
            et.setVisibility(View.VISIBLE);
        } else {
            et.setVisibility(View.GONE);
        }

        if(maa.selItem != null)
        {
            RadioButton rOld = (RadioButton) maa.selItem.findViewById(R.id.radioButton1);
            rOld.setChecked(false);
        }

        RadioButton r = (RadioButton) view.findViewById(R.id.radioButton1);
        r.setChecked(true);

        maa.selIndex = position;
        maa.selItem = view;
    }

    public void onSaveInstanceState(Bundle b)
    {
        b.putInt("selIndex", maa.selIndex);

        super.onSaveInstanceState(b);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_time_form, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Code for MyArrayAdapter class:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<String> {

    private Context context;
    private String[] strings;
    private int layoutID;

    public int selIndex;
    public View selItem;

    public MyArrayAdapter(Context c, int id, String[] values)
    {
        super(c, id, values);

        selIndex = -1;
        selItem = null;

        this.strings = values;
        this.context = c;
        layoutID = id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rView = inflater.inflate(layoutID, parent, false);

        TextView tv = (TextView) rView.findViewById(R.id.textView1);
        tv.setText(strings[position]);

        if(position == selIndex)
        {
            RadioButton r = (RadioButton) rView.findViewById(R.id.radioButton1);
            r.setChecked(true);
            selItem = rView;
        }


        return rView;
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:10:54+00:00Added an answer on June 17, 2026 at 10:10 pm

    I figured it out. I’m not sure it’s the best possible solution, but it works and doesn’t cause any noticeable performance loss. ^_^

    In the TimeForm activity’s onItemClick() method, i added this at the bottom:

    ListView lv = (ListView) findViewById(R.id.listView1);
    lv.invalidateViews();
    

    This forces the listview to redraw all the child views. Thanks to everyone who took the time to help me figure this out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom ListView that uses a custom ArrayAdapter (which basically just overrides
I have a custom ListView adapter that uses multiple layout types. One is the
I have a ListView that uses a custom layout that has 5 child View
I have a ListView that uses a custom adapter. The custom adapter's getView uses
I have an activity with a listView, which uses a custom row layout defined
I have a problem with my Android listview. My list uses a custom arrayadapter,
I have a ListView that uses an ArrayAdapter. The items in the ArrayAdapter are
I have a custom dialog that uses an xml file to setup the layout,
I have ListView that uses a GridView to display several columns of data. Two
I have a ListView that uses Linkify to create a link to another activity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.