java - 以正确的方式编码。 Android onCreate 和初始化

标签 java android oop

我正在开发一个应用程序,该应用程序处理从服务器获取 JSON 数据、解析它并设置适配器以在 ListView 中查看数据。我真正想知道的是我是否以正确的方式开始 Activity 。我在为不同的微调器设置适配器以及设置它们的 onClick 监听器时有一些重复的代码。我将如何整合这段代码,这样它就不会陷入困惑。我发现我很难继续编码我的其他 Activity ,因为我遇到了这样的问题。感谢您提供的任何帮助。

package com.bde.dgcr;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class CourseSearch extends Activity {

private String citySearch;
private String stateSearch;
private String countrySearch;
private String mode;

public void onCreate(Bundle savedInstanceState) {
    // call Activity onCreate method to take care of all initial setup before we do
    // any other customizing of the method
    super.onCreate(savedInstanceState);
    //setting the current view to the mail xml file.
    setContentView(R.layout.main);
    // here we are creating new spinners and connecting their adapters to them. This needs to be
    // made into its own function. Any code that you are repeating should be made into its own function.
    Spinner countriesSpinner = (Spinner) findViewById(R.id.country);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.countries_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    countriesSpinner.setAdapter(adapter);
    countriesSpinner.setOnItemSelectedListener(new onCountrySelectedListener());
    //state spinner
    Spinner stateSpinner = (Spinner) findViewById(R.id.state);
    ArrayAdapter<CharSequence> state_adapter = ArrayAdapter.createFromResource(
            this, R.array.states_array, android.R.layout.simple_spinner_item);
    state_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    stateSpinner.setAdapter(state_adapter);
    stateSpinner.setOnItemSelectedListener(new onStateSelectedListener());
}


public void searchByLoc(View search) {
    final EditText cityField = (EditText) findViewById(R.id.cityName);
    citySearch = cityField.getText().toString();

    if (citySearch != null)
        setMode("findloc");
    Intent grabberintent = new Intent(CourseSearch.this, JsonGrabber.class);
    grabberintent.putExtra("city", citySearch);
    grabberintent.putExtra("state", stateSearch);
    grabberintent.putExtra("country", countrySearch);
    grabberintent.putExtra("mode", mode);
    this.startActivity(grabberintent);
}

public String getCitySearch() {
    return citySearch;
}

public String getStateSearch() {
    return stateSearch;
}

public String getCountrySearch() {
    return countrySearch;
}

public void setMode(String m) {
    mode = m;
}

public String getMode() {
    return mode;
}

class onCountrySelectedListener implements AdapterView.OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
                               View view, int pos, long id) {
        Toast.makeText(parent.getContext(), "The Country is " +
                parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        countrySearch = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

class onStateSelectedListener implements AdapterView.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent,
                               View view, int pos, long id) {
        Toast.makeText(parent.getContext(), "The State is " +
                parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        stateSearch = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

}

这是xml代码

    >

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="48dp"

        android:background="@drawable/header_bg_hdpi"
        android:tileMode="repeat"
        android:gravity="center_vertical"

        >
    <TextView
            android:text="@string/title_search"
            style="@style/TitleText"
            android:gravity="left"
            android:layout_weight="1"
            >
    </TextView>


    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/dgcr_logo"
            android:gravity="right"

            android:layout_weight="1"


            />

</LinearLayout>



<TextView
        android:id="@+id/searchTitle"
        android:text="Search for Course By Location"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"/>
<EditText
        android:id="@+id/cityName"
        android:autoText="true"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:inputType="textAutoComplete"
        android:hint="City Name">


</EditText>

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/states_prompt"
        />
<Spinner
        android:id="@+id/state"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:prompt="@string/states_prompt"

        >
</Spinner>
<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/countries_prompt"
        />
<Spinner
        android:id="@+id/country"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:prompt="@string/countries_prompt"

        >
</Spinner>
/>
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <Button
            android:text="Search Address"
            android:id="@+id/search"
            android:background="@drawable/orange_gradient_button"
            style="@style/ButtonText"
            android:onClick="searchByLoc"
        android:layout_weight="1">
    </Button>

    <Button
            android:text="Search Map"
            android:id="@+id/button2"
            android:background="@drawable/orange_gradient_button"
            style="@style/ButtonText"
        android:layout_weight="1">
    </Button>
</LinearLayout>
<ImageView
        android:gravity=""
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:src="@drawable/footer_hdpi">
</ImageView>



</LinearLayout>

最佳答案

您正在做的事情是正确的,但这不是最简单的方法。您应该考虑设置一个布局文件(在 res/layout/.xml 中)来完成 onCreate() 正在执行的大部分操作。它还允许您定义触发操作时要调用的方法。请参阅http://developer.android.com/guide/topics/ui/declaring-layout.html例如。

关于java - 以正确的方式编码。 Android onCreate 和初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9693675/

相关文章:

java : ClassLoaders

java - 使用时区java将字符串转换为适当的日期

c# - 如何使用 Intent 和 Activity 在 Xamarin Android 中插入城市、街道、邮政编码、国家/地区的联系人?

c++ - OOP C++、虚函数和新运算符

oop - 业务规则分为两类

java - 静态无效类

java - Android ArrayAdapter NullPointerException getID

android - File.separatorChar 是基于操作系统、区域设置还是其他什么?

java - 无法解析符号 CoordinatorLayout

集群系统上的 Java 并行处理(集群计算)