2016-09-28 56 views
0

CityBeans.java 這是我在豆cityArrayListAndroid的微調使用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> 
+0

其中是您的活動中的onCreate()方法 –

+0

您必須爲其創建自定義適配器。 –

+0

您必須創建自定義適配器,因爲您正在從Web服務獲取微調器值 –

回答

0

下面是一個例子。 爲微調器設計創建此佈局。我存儲了ID,並且名字像你一樣。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/txtApplyID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     /> 

    <TextView 
     android:id="@+id/txtApplyName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/colorBlack" 
     android:textSize="18sp" 
     android:layout_gravity="center_horizontal" 
     /> 

</LinearLayout> 

像你的城市豆。

public class Apply 
{ 
     private String _id; 
     private String _name; 

     public Apply(String id, String name) 
     { 
      this._id = id; 
      this._name = name; 
     } 

     public String GetID() 
     { 
      return this._id; 
     } 

     public String GetName() 
     { 
      return this._name; 
     } 
} 

的微調

public class ApplyAdapter extends BaseAdapter { 

     private LayoutInflater layoutInflater; 
     private List<Apply> applyList; 

     public ApplyAdapter(Activity activity, List<Apply> applyList) 
     { 
      this.layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      this.applyList = applyList; 
     } 

     @Override 
     public int getCount() { 
      return this.applyList.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return this.applyList.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View viewerino; 
      viewerino = this.layoutInflater.inflate(R.layout.spinner_apply_item, null); 
    //my layout file spinner_apply_item you have to create. 
      TextView id = (TextView)viewerino.findViewById(R.id.txtApplyID); 
      TextView name = (TextView)viewerino.findViewById(R.id.txtApplyName); 
      Apply apply = this.applyList.get(position); 
      id.setText(apply.GetID()); 
      name.setText(apply.GetName()); 
      return viewerino; 
     } 
    } 

,並調用它在你的活動定製ApplyAdapter

ApplyAdapter applyAdapter = new ApplyAdapter(MainActivity.this,this.applyList); 
this.spnApply.setAdapter(applyAdapter); 

有一部分我的活動佈局。

<?xml version="1.0" encoding="utf-8"?> 
    <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="match_parent" 
     tools:context="com.example.volkan.dkbilisim.MainActivity"> 

     <RelativeLayout 
      android:id="@+id/mainRltHeader" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/relative_design" 
      android:layout_marginTop="10dp" 
      > 

      <Spinner 
       android:id="@+id/mainSpinner" 
       android:layout_width="wrap_content" 
       android:layout_height="30dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginTop="8dp" 
       android:gravity="center" 
       android:layout_alignParentRight="true" 
       ></Spinner> 

</RelativeLayout> 

    </RelativeLayout> 
+0

爲什麼不使用Spinner在你的XML? –

+0

這不是微調。等待我的編輯。微調必須在活動佈局。 – Kayra

+0

您必須爲微調器創建一個佈局文件,您無法爲CityBeans類使用它的數組列表。您必須創建自定義適配器。試試這個會有用。 – Kayra