CityBeans.java 這是我在豆cityArrayList
Android的微調使用ArrayList的工作不
public class CityBeans implements Serializable {
private int ;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
使用Home.java
public class Home extends BaseActivity {
private ProgressDialog dialog;
private SectionsPagerAdapter mSectionsPagerAdapter;
private TextView tap1, tap2;
private String cityUrl ="http://arabnewtech.org/zoodre/public/api/locations/city/3";
private Spinner citySpinner;
private ArrayList<CityBeans> cityBeansList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (WrapContentViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Create Tabs Handelation
tabsHandel();
// Initialize Views
initViews();
// City Spinner
getCityData();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client2 = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void getCityData() {
dialog = new ProgressDialog(Home.this);
dialog.setMessage("Waiting ....");
dialog.show();
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
client.get(cityUrl, params, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//Log.d("---Error---", responseString);
dialog.dismiss();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
dialog.dismiss();
Log.d("---result---", responseString);
try {
JSONObject jo = new JSONObject(responseString);
JSONArray jo_result = jo.getJSONArray("result");
cityBeansList = new ArrayList<CityBeans>();
for (int i = 0; i < jo_result.length(); i++) {
CityBeans cityBean = new CityBeans();
JSONObject my_jo = jo_result.getJSONObject(i);
cityBean.setId(my_jo.getInt("id"));
cityBean.setName(my_jo.getString("name"));
cityBeansList.add(cityBean);
}
ArrayAdapter<CityBeans> adapter;
adapter = new ArrayAdapter<CityBeans>(Home.this, android.R.layout.simple_spinner_item, cityBeansList);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
citySpinner.setAdapter(adapter);
} catch (Exception e) {
Log.i("---Result Error","Here We found error");
e.printStackTrace();
}
}
});
}
}
fragment.xml之
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.arabnewtech.a22.zoodrealstate.Home$PlaceholderFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/spinner1">
<Spinner
android:id="@+id/cityID"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"></Spinner>
<Spinner
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_height="wrap_content"></Spinner>
</LinearLayout>
<LinearLayout
android:id="@+id/spinner2"
android:layout_below="@id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"></Spinner>
<Spinner
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_height="wrap_content"></Spinner>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorYellow"
android:layout_below="@+id/spinner2"
android:text="بحث"/>
</RelativeLayout>
其中是您的活動中的onCreate()方法 –
您必須爲其創建自定義適配器。 –
您必須創建自定義適配器,因爲您正在從Web服務獲取微調器值 –