在写一个 SlidingDrawer的Demo时,发现一个自己原来没有注意的问题。

首先贴上源代码。

 

 
  
  1. package com.example.slidingdrawerproject;  
  2.   
  3. import android.R.anim;  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.app.Dialog;  
  7. import android.content.DialogInterface;  
  8. import android.content.res.Resources;  
  9. import android.os.Bundle;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.ArrayAdapter;  
  14. import android.widget.LinearLayout;  
  15. import android.widget.ListView;  
  16. import android.widget.SlidingDrawer;  
  17. import android.widget.TextView;  
  18. import android.widget.Toast;  
  19. import android.widget.ZoomControls;  
  20.   
  21. public class SlidingDrawerActivity extends Activity {  
  22.   
  23.     private SlidingDrawer drawer = null;  
  24.     private ListView listView = null;  
  25.     private ArrayAdapter<CharSequence> adapter = null;  
  26.     private TextView textView = null;  
  27.     private ZoomControls controls = null;  
  28.     private float curFontSize = 0;  
  29.     private String[] data = null;  
  30.       
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         super.setContentView(R.layout.activity_sliding_drawer);  
  35.         drawer = (SlidingDrawer) super.findViewById(R.id.slidingdrawer);  
  36.   
  37.         LinearLayout layout = (LinearLayout) super.findViewById(R.id.content);  
  38.         listView = new ListView(this);  
  39. adapter = ArrayAdapter.createFromResource(this, R.array.citys, android.R.layout.simple_list_item_1); // Resources resources = getResources(); // data = resources.getStringArray(R.array.citys); // adapter = new ArrayAdapter<CharSequence>(this, // android.R.layout.simple_list_item_1, data); 
  40.         listView.setAdapter(adapter);  
  41.         layout.addView(listView, 2);  
  42.          
  43.  
  44.         drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {  
  45.   
  46.             @Override  
  47.             public void onDrawerClosed() {  
  48.                 Toast.makeText(SlidingDrawerActivity.this"关闭组件",  
  49.                         Toast.LENGTH_SHORT).show();  
  50.             }  
  51.         });  
  52.         drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {  
  53.   
  54.             @Override  
  55.             public void onDrawerOpened() {  
  56.                 Toast.makeText(SlidingDrawerActivity.this"打开组件",  
  57.                         Toast.LENGTH_SHORT).show();  
  58.             }  
  59.         });  
  60.   
  61.         drawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {  
  62.   
  63.             @Override  
  64.             public void onScrollStarted() {  
  65.                 Toast.makeText(SlidingDrawerActivity.this"滑动组件ing....",  
  66.                         Toast.LENGTH_SHORT).show();  
  67.             }  
  68.   
  69.             @Override  
  70.             public void onScrollEnded() {  
  71.                 Toast.makeText(SlidingDrawerActivity.this"滑动组件end",  
  72.                         Toast.LENGTH_SHORT).show();  
  73.   
  74.             }  
  75.         });  
  76.   
  77.         listView.setOnItemClickListener(new ListView.OnItemClickListener() {  
  78.   
  79.             @Override  
  80.             public void onItemClick(AdapterView<?> parent, View view,  
  81.                     int position, long id) {  
  82.                 Object obj = parent.getAdapter().getItem(position);  
  83.                 if (obj instanceof String) {  
  84.                     String info = (String) obj;  
  85.                     Dialog dialog = new AlertDialog.Builder(  
  86.                             SlidingDrawerActivity.this).setTitle("info")  
  87.                             .setMessage(info).setIcon(R.drawable.ic_launcher)  
  88.                             .create();  
  89.   
  90.                     dialog.show();  
  91.                 }  
  92.             }  
  93.         });  
  94.     }  
  95.   
  96.     @Override  
  97.     public boolean onCreateOptionsMenu(Menu menu) {  
  98.         getMenuInflater().inflate(R.menu.activity_sliding_drawer, menu);  
  99.         return true;  
  100.     }  
  101.   
  102. }  

 

 
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string-array name="citys"> 
  4.         <item>北京</item> 
  5.         <item>南昌</item> 
  6.         <item>昆明</item> 
  7.         <item>九江</item> 
  8.         <!-- (云南过桥米线发源地) --> 
  9.         <item>蒙自</item> 
  10.         <item>深证</item> 
  11.         <item>东莞</item> 
  12.         <item>香港</item> 
  13.     </string-array> 
  14. </resources> 
 
  
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="match_parent" 
  5.     android:layout_height="match_parent" > 
  6.      
  7.     <SlidingDrawer 
  8.         android:id="@+id/slidingdrawer" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="fill_parent" 
  11.         android:orientation="vertical" 
  12.         android:handle="@+id/handle" 
  13.         android:content="@+id/content" 
  14.         > 
  15.         <ImageView 
  16.             android:id="@+id/handle" 
  17.             android:src="@android:drawable/ic_lock_idle_alarm" 
  18.             android:layout_width="wrap_content" 
  19.             android:layout_height="wrap_content"  
  20.             /> 
  21.  
  22.         <LinearLayout 
  23.             android:id="@+id/content" 
  24.             android:layout_width="fill_parent" 
  25.             android:layout_height="fill_parent" 
  26.             android:orientation="vertical" > 
  27.  
  28.             <TextView 
  29.                 android:id="@+id/slid_txt" 
  30.                 android:text="明天放假" 
  31.                 android:layout_width="fill_parent" 
  32.                 android:layout_height="wrap_content" 
  33.                 /> 
  34.             <ZoomControls 
  35.                 android:id="@+id/slid_zoomCon" 
  36.                 android:layout_width="wrap_content" 
  37.                 android:layout_height="wrap_content" 
  38.                 /> 
  39.                  
  40.         </LinearLayout>     
  41.     </SlidingDrawer> 
  42.      
  43. </LinearLayout> 

效果图:

 

这是可以出现效果的。

在ArrayAdapter中的源代码中:是

 
  
  1. adapter = ArrayAdapter.createFromResource(this, R.array.citys, 
  2.     android.R.layout.simple_list_item_1); 

这个是没有问题的,查API文档,

 

public static ArrayAdapter<CharSequencecreateFromResource (Context context, int textArrayResId, int textViewResId)
Since:  API Level 1

Creates a new ArrayAdapter from external resources. The content of the array is obtained through getTextArray(int).

Parameters
contextThe application's environment.
textArrayResIdThe identifier of the array to use as the data source.//xml中array的id
textViewResIdThe identifier of the layout used to create views.//xml中layout的id
Returns
  • An ArrayAdapter.

 

可是由于自己的粗心,把代码写成

 
  
  1. adapter = new ArrayAdapter<CharSequence>(this,  
  2.                 android.R.layout.simple_list_item_1,R.array.citys); 

而且由于类型匹配,所以系统没有报错。

后来查过API

public ArrayAdapter (Context context, int resource, int textViewResourceId)
Since:  API Level 1

Constructor

Parameters
contextThe current context.
resourceThe resource ID for a layout file containing a layout to use when instantiating views.
textViewResourceIdThe id of the TextView within the layout resource to be populated//TextView在layout中的id

所以自己错写的方法是不匹配的。

这里还有两种方法写这个adapter:首先看API文档

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)
Since:  API Level 1

Constructor

Parameters
contextThe current context.
textViewResourceIdThe resource ID for a layout file containing a TextView to use when instantiating views.
objectsThe objects to represent in the ListView.

将数据在代码中写成数组形式,

 
  
  1. private String[] pro=new String[]{"北京","上海","广东","江西","云南","福建","海南","河南"};  
  2.   
  3. adapter = new ArrayAdapter<CharSequence>(this,   
  4.                 android.R.layout.simple_list_item_1, data);  

②将xml中的资源通过resources的getStringArray()方法转化为数组。

 
  
  1. Resources resources = getResources(); 
  2.         data = resources.getStringArray(R.array.citys); 
  3.         adapter = new ArrayAdapter<CharSequence>(this,  
  4.                 android.R.layout.simple_list_item_1, data);