0

New to android here. I intend to make an app that has a ListView with Radio buttons set in single choice mode. After the user selects a radio button for the corresponding ListView item, and after clicking a Button input, it should take the user to another activity.

As such I am having trouble as to how I should approach the problem of how to define a RadioGroup which fetches the values of RadioButton dynamically from the strings.xml file.

This is the related work that I have at the moment pertaining to this;

Related Code in MainActivity.java

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Array of place types
    mPlaceType = getResources().getStringArray(R.array.place_type);

    // Array of place type names
    mPlaceTypeName = getResources().getStringArray(R.array.place_type_name);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, mPlaceTypeName); //ArrayAdapter<String> means each element in the array would be a string


    mListPlaceType = (ListView) findViewById(R.id.list_place_type);

    mListPlaceType.setAdapter(adapter);

    mListPlaceType.setItemsCanFocus(false);

    mListPlaceType.setChoiceMode(ListView.CHOICE_MODE_SINGLE);




    Intent mIntent = getIntent();
    main_radius = mIntent.getIntExtra("radius", 0);

    //TextView
    TextView tV = (TextView)findViewById(R.id.textView_main);
    tV.setText("Current Radius is: " +Integer.toString(main_radius));

    Button btnFind;

    // Getting reference to Find Button
    btnFind = ( Button ) findViewById(R.id.btn_find);

    // Setting click event listener for the find button
    btnFind.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            StringBuilder sb1 = new StringBuilder(sbMethod());

            Intent mapIntent = new Intent(getApplicationContext(), MapActivity.class);
            Bundle b = new Bundle();
            b.putString("sb1",sb1.toString());
            mapIntent.putExtras(b);
            startActivity(mapIntent);

            if(mListPlaceType.getSelectedItemPosition() < 0 )
            {
                Toast.makeText(getApplicationContext(), "No Item Selected from List", Toast.LENGTH_LONG).show();
            }

        }
    });

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
style="@style/LinearLayout">

<Button
    android:id="@+id/btn_find"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:text="@string/str_btn_find"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="30dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ListView
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/list_place_type"
    android:layout_below="@+id/btn_find"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView_main"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

strings.xml

<resources>

<string name="app_name">GeoGuideB</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="str_btn_find">Find</string>
<string name="change_radius">Change Radius</string>
<string name="save_radius">Save Radius</string>


<string-array name="place_type">
    <item>airport</item>
    <item>atm</item>
    <item>bank</item>
    <item>bus_station</item>
    <item>church</item>
    <item>doctor</item>
    <item>hospital</item>
    <item>movie_theater</item>
    <item>restaurant</item>
</string-array>

<string-array name="place_type_name">
    <item>Airport</item>
    <item>ATM</item>
    <item>Bank</item>
    <item>Bus Station</item>
    <item>Church</item>
    <item>Doctor</item>
    <item>Hospital</item>
    <item>Movie Theater</item>
    <item>Restaurant</item>
</string-array>

At the moment I have this layout:

layout

for my app and on clicking the 'Find' button it should jump from this activity to the next one. Considering this, what should be the approach I should follow? Thanks.

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
  • if users are only allowed to select one item and you are going into an activity anyway why do you need radio buttons? why not just say what would you like to find and launch the activity as soon as they click the listView row? – Tomer Shemesh Apr 20 '15 at 19:29
  • I am going to add another button to my activity (say "Find in a List") so the current button that I have would be "Find on map". These buttons would let the user make a choice between whether they want to find the listed service on a Google map fragment or in the form of a ListView. – user3583395 Apr 20 '15 at 19:35
  • ah ok. well take a look here. http://stackoverflow.com/questions/21347661/android-radio-button-in-custom-list-view – Tomer Shemesh Apr 20 '15 at 19:45

1 Answers1

0

try the code below.

int selectedPosition=-1;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Array of place types
    mPlaceType = getResources().getStringArray(R.array.place_type);

    // Array of place type names
    mPlaceTypeName = getResources().getStringArray(R.array.place_type_name);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, mPlaceTypeName); //ArrayAdapter<String> means each element in the array would be a string


    mListPlaceType = (ListView) findViewById(R.id.list_place_type);

    mListPlaceType.setAdapter(adapter);

    mListPlaceType.setItemsCanFocus(false);

    mListPlaceType.setChoiceMode(ListView.CHOICE_MODE_SINGLE);




    Intent mIntent = getIntent();
    main_radius = mIntent.getIntExtra("radius", 0);

    //TextView
    TextView tV = (TextView)findViewById(R.id.textView_main);
    tV.setText("Current Radius is: " +Integer.toString(main_radius));

    Button btnFind;

    // Getting reference to Find Button
    btnFind = ( Button ) findViewById(R.id.btn_find);

    // Setting click event listener for the find button
    btnFind.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        if(mListPlaceType.getSelectedItemPosition() < 0 )
            {
                Toast.makeText(getApplicationContext(), "No Item Selected from List", Toast.LENGTH_LONG).show();
            }else{
            Intent mapIntent = new Intent(getApplicationContext(), MapActivity.class);
            mapIntent.putExtra("placeType", mPlaceType[selectedPosition]);
            mapIntent.putExtra("placeTypeName", mPlaceTypeName[selectedPosition]);
            startActivity(mapIntent);
        }


        }
    });


    mListPlaceType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                selectedPosition = position;


            }
        });

}

and to get the intent on the MapActivity,

Intent intent= getIntent();
intent.getStringExtra("placeType");
intent.getStringExtra("placeTypeName");
Genevieve
  • 450
  • 3
  • 9