我正在开发一个应用程序处理从解析它的服务器获取JSON数据并设置适配器以查看列表视图中的数据。我真正想要的是知道我是否以正确的方式开始活动。我在为不同的微调器设置适配器时也有一些重复的代码,并设置了他们的onClick监听器。我将如何整合这些代码,以便它不会陷入一团糟。我发现我很难继续编写我的其他活动,因为我遇到了这样的问题。感谢您提供的任何帮助。
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>
答案 0 :(得分:0)
你所做的是正确的,但这不是最简单的方法。您应该考虑设置一个布局文件(在res / layout / .xml中)来完成onCreate()正在做的大部分工作。它还允许您定义触发操作时要调用的方法。有关示例,请参阅http://developer.android.com/guide/topics/ui/declaring-layout.html。