0

这里是安卓新手。我打算制作一个具有 ListView 的应用程序,其中单选按钮设置为单选模式。在用户为相应的 ListView 项目选择一个单选按钮后,并单击一个Button输入后,它应该将用户带到另一个活动。

因此,我在解决如何定义 RadioGroup 的问题时遇到了麻烦,该 RadioGroup 从 strings.xml 文件中动态获取 RadioButton 的值。

这是我目前与此相关的相关工作;

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" />

字符串.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>

目前我有这个布局

布局

对于我的应用程序并单击“查找”按钮,它应该从该活动跳转到下一个活动。考虑到这一点,我应该遵循什么方法?谢谢。

4

1 回答 1

0

试试下面的代码。

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;


            }
        });

}

并获得 MapActivity 的意图,

Intent intent= getIntent();
intent.getStringExtra("placeType");
intent.getStringExtra("placeTypeName");
于 2015-04-20T22:38:51.383 回答