2011-02-24 59 views
1

我已經使用自定義數組適配器來填充我的列表視圖。我面臨的問題是分隔符在第四個和第五個列表項之間缺失。Android中的列表項之間缺少分隔符

下面的代碼:

public class Clubs extends ListActivity{ 

    private static class EfficientAdapter extends BaseAdapter { 
     private LayoutInflater mInflater; 
     private Bitmap mIcon1; 
     private Bitmap mIcon2; 

     public EfficientAdapter(Context context) { 
      // Cache the LayoutInflate to avoid asking for a new one each time. 
      mInflater = LayoutInflater.from(context); 

      // Icons bound to the rows. 
      mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.yellow_offline); 
      mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.green_online); 
     } 

     /** 
     * The number of items in the list is determined by the number of speeches 
     * in our array. 
     * 
     * @see android.widget.ListAdapter#getCount() 
     */ 
     public int getCount() { 
      return DATA.length; 
     } 

     /** 
     * Since the data comes from an array, just returning the index is 
     * sufficent to get at the data. If we were using a more complex data 
     * structure, we would return whatever object represents one row in the 
     * list. 
     * 
     * @see android.widget.ListAdapter#getItem(int) 
     */ 
     public Object getItem(int position) { 
      return position; 
     } 

     /** 
     * Use the array index as a unique id. 
     * 
     * @see android.widget.ListAdapter#getItemId(int) 
     */ 
     public long getItemId(int position) { 
      return position; 
     } 

     /** 
     * Make a view to hold each row. 
     * 
     * @see android.widget.ListAdapter#getView(int, android.view.View, 
     *  android.view.ViewGroup) 
     */ 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // A ViewHolder keeps references to children views to avoid unneccessary calls 
      // to findViewById() on each row. 
      ViewHolder holder; 

      // When convertView is not null, we can reuse it directly, there is no need 
      // to reinflate it. We only inflate a new View when the convertView supplied 
      // by ListView is null. 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.people_list_item, null); 

       // Creates a ViewHolder and store references to the two children views 
       // we want to bind data to. 
       holder = new ViewHolder(); 
       holder.text = (TextView) convertView.findViewById(R.id.textpeople); 
       holder.icon = (ImageView) convertView.findViewById(R.id.image); 

       convertView.setTag(holder); 
      } else { 
       // Get the ViewHolder back to get fast access to the TextView 
       // and the ImageView. 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      // Bind the data efficiently with the holder. 
      holder.text.setText(DATA[position]); 
      holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 

      return convertView; 
     } 

     static class ViewHolder { 
      TextView text; 
      ImageView icon; 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new EfficientAdapter(this)); 
    } 

    private static final String[] DATA = { 
      "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", 
      "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", 
      }; 
} 

這裏的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:id="@+id/widget31" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 

<LinearLayout 
android:id="@+id/widget321" 
android:layout_width="479dip" 
android:layout_height="54dip" 
android:layout_marginTop="16dip" 
> 

<ImageView 
android:id="@+id/image" 
android:layout_width="24dip" 
android:layout_height="24dip" 
android:layout_marginLeft="14dip" 
android:layout_marginTop="5dip" 
android:src="@drawable/green_online"> 
</ImageView> 

<TextView 
android:id="@+id/textpeople" 
android:layout_marginLeft="10dip" 
android:textColor="#FFF5EE" 
android:layout_height="29dip" 
android:textSize="16sp" 
android:layout_width="400dip" 
android:layout_marginTop="7dip" 
> 
</TextView> 
</LinearLayout> 
</LinearLayout> 

下面是屏幕截圖listseparator

有人可以告訴我在哪裏出了問題? 感謝

編輯
解決方案
下面的代碼添加到搞不定

ListView lv=getListView(); 
lv.setDividerHeight(2); 
+0

你正在測試哪個設備我在HTC tatoo遇到類似的問題,並發現它的唯一設備專用 – ingsaurabh 2011-02-24 12:38:59

+0

我正在模擬器中測試它。 – rogerstone 2011-02-24 12:45:18

回答

4

簡單的方法是調用setDividerHeight(2)onCreate方法。以更復雜的方式,您需要確保您的資源正確放置在layout-hdpi/layout中,因爲發生在那裏的操作系統接近1dpi到0dpi,因爲應用程序以兼容模式運行。同時檢查你的清單中的supports-screens屬性(嘗試將所有內容設置爲true)

+0

你能告訴我在哪裏完全放置setDividerHeight()完全syntax.I嘗試把它放在oncreate.It是扔我編譯錯誤。 – rogerstone 2011-02-24 12:54:37

+0

哇!。得到它working.Thanks很多。 – rogerstone 2011-02-24 13:03:18

+0

你也可以在xml中嘗試:android:dividerHeight =「3dip」 – 2011-02-24 13:04:48