2014-09-19 17 views
-1

我想用一個自定義佈局爲ListView項目,我創建了XML,有我的自定義對象和創建的自定義ArrayAdapter,通過適配器,但一切都顯示爲空的ListView與CustomArrayAdapter顯示一切空

沒有線索在logcat的錯誤

我相信這個問題是在ArrayAdapter類,這裏是我的代碼

public class EntityListAdapter extends ArrayAdapter<EntityInfo> { 

    Context context; 
    List<EntityInfo> _entities; 
    int layoutResID; 

    public EntityListAdapter(Context context, int layoutResourceID, 
          List<EntityInfo> listItems) { 
     super(context, layoutResourceID, listItems); 
     this.context = context; 
     this._entities = listItems; 
     this.layoutResID = layoutResourceID; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     EntityHolder entity; 
     View view = convertView; 

     if (view == null) { 
      view = LayoutInflater.from(getContext()).inflate(layoutResID, parent, false); 
      //LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      //view = inflater.inflate(layoutResID, parent,false); 
     }// else { 
     // entity = (EntityHolder) view.getTag(); 
     //} 
     entity = new EntityHolder(); 
     entity.mEntityName = (TextView) view.findViewById(R.id.txt_entity_name); 
     entity.mEntityType = (TextView) view.findViewById(R.id.txt_entity_type); 
     entity.mImage = (ImageView) view.findViewById(R.id.list_icon); 

     DatabaseHelper db = new DatabaseHelper(context); 

     EntityInfo _entity = (EntityInfo) this._entities.get(position); 

     entity.mEntityName.setText(_entity.getEntityName()); 
     EntityTypeInfo _entityType = db.getEntityType(_entity.getEntityTypeId()); 
     entity.mEntityType.setText(_entityType.getEntityTypeName()); 
     //Uri uri = Uri.parse(_entity.getImageUri()); 
     //entity.mImage.setImageURI(uri); 
     return view; 
    } 
    @Override 
    public int getCount() { 
     return _entities.size(); 
    } 

    private static class EntityHolder { 
     TextView mEntityName; 
     TextView mEntityType; 
     ImageView mImage; 
    } 
} 

因此,這是我的適配器設置到ListView

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_entities, container, false); 

     TabHost tabs = (TabHost) rootView.findViewById(R.id.tabHost); 
     tabs.setup(); 

     TabHost.TabSpec peopleTab = tabs.newTabSpec(getString(R.string.entities_tab_people)); 
     peopleTab.setContent(R.id.People); 
     peopleTab.setIndicator(getString(R.string.entities_tab_people)); 
     tabs.addTab(peopleTab); 

     // Home 
     TabHost.TabSpec banksTab = tabs.newTabSpec(getString(R.string.entities_tab_banks)); 
     banksTab.setContent(R.id.Banks); 
     banksTab.setIndicator(getString(R.string.entities_tab_banks)); 
     tabs.addTab(banksTab); 

     // Home 
     TabHost.TabSpec governmentTab = tabs.newTabSpec(getString(R.string.entities_tab_gov)); 
     governmentTab.setContent(R.id.Taxes); 
     governmentTab.setIndicator(getString(R.string.entities_tab_gov)); 
     tabs.addTab(governmentTab); 

     tabs.setOnTabChangedListener(new AnimatedTabHostListener(tabs)); 

     TabWidget widget = tabs.getTabWidget(); 
     for (int i = 0; i < widget.getChildCount(); i++) { 
      View v = widget.getChildAt(i); 

      // Look for the title view to ensure this is an indicator and not a divider. 
      TextView tv = (TextView) v.findViewById(android.R.id.title); 
      if (tv == null) { 
       continue; 
      } 
      v.setBackgroundResource(R.drawable.tab_indicator_ab_money); 
     } 

     DatabaseHelper db = new DatabaseHelper(getActivity()); 
     List<EntityInfo> _entities = db.getAllEntities(); 
     List<EntityInfo> _banks = new ArrayList<EntityInfo>(); 
     List<EntityInfo> _people = new ArrayList<EntityInfo>(); 
     List<EntityInfo> _gov = new ArrayList<EntityInfo>(); 

     for (EntityInfo _entity : _entities) { 
      if (_entity.getEntityTypeId() == 2) { 
       _people.add(_entity); 
      } else if (_entity.getEntityTypeId() == 3) { 
       _banks.add(_entity); 
      } else if (_entity.getEntityTypeId() == 4) { 
       _gov.add(_entity); 
      } 
     } 

     if (_people.size() > 0) { 
      ListView _listPeople = (ListView) rootView.findViewById(R.id.list_person_entities); 
      EntityListAdapter _peopleAdapter = new EntityListAdapter(getActivity(), R.layout.entity_list_item, _people); 
      _listPeople.setAdapter(_peopleAdapter); 
     } 
     if (_banks.size()>0) { 
      ListView _listBanks = (ListView) rootView.findViewById(R.id.list_bank_entities); 
      EntityListAdapter _banksAdapter = new EntityListAdapter(getActivity(), R.layout.entity_list_item, _banks); 
      _listBanks.setAdapter(_banksAdapter); 
     } 
     if (_gov.size() > 0) { 
      ListView _listGov = (ListView) rootView.findViewById(R.id.list_gov_entities); 
      EntityListAdapter _govAdapter = new EntityListAdapter(getActivity(), R.layout.entity_list_item, _gov); 
      _listGov.setAdapter(_govAdapter); 
     } 

     return rootView; 
    } 

如果我評論ImageView,我可以看到我設置的示例圖像,但是如果我取消註釋,則不顯示任何內容。

這裏的列表項

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="75dp" 
     android:orientation="vertical"> 
     <RelativeLayout 
      android:layout_width="75dp" 
      android:layout_height="fill_parent"> 
      <ImageView android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       android:id="@+id/list_icon" 
       android:src="@drawable/ic_action_picture" 
       android:layout_centerInParent="true" /> 
     </RelativeLayout> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_entity_name" 
       android:layout_gravity="center" 
       android:textColor="@color/dark_gray" 
       android:textStyle="bold" 
       android:textAppearance="?android:attr/textAppearanceMedium"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:textColor="@color/dark_gray" 
       android:textStyle="bold" 
       android:id="@+id/txt_entity_type"/> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

這裏的XML佈局就是我展示這個列表項(其中我得到的一切空

<FrameLayout 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.andujardev.money.EntitiesFragment"> 

    <TabHost 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/tabHost" 
     android:layout_gravity="center"> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"></TabWidget> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

       <LinearLayout 
        android:id="@+id/People" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:background="@color/lighter_gray"> 

        <ListView 
         android:id="@+id/list_person_entities" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:divider="@color/light_gray" 
         android:dividerHeight="1dp" 
         android:choiceMode="none" 
         /> 

        </LinearLayout> 

       <LinearLayout 
        android:id="@+id/Banks" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="horizontal" 
        android:background="@color/lighter_gray"> 
        <ListView 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/list_bank_entities" 
         android:divider="@color/light_gray" 
         android:dividerHeight="1dp" 
         android:choiceMode="none"> 

        </ListView> 
        </LinearLayout> 

       <LinearLayout 
        android:id="@+id/Taxes" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:background="@color/lighter_gray"> 
        <ListView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/list_gov_entities" 
         android:divider="@color/light_gray" 
         android:dividerHeight="1dp" 
         android:choiceMode="none"> 

        </ListView> 
        </LinearLayout> 
      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</FrameLayout> 

任何想法片段的佈局?

+0

http://www.android-ios-tutorials.com/770/create-custom-listview-in-android/ – Houcine 2014-09-19 23:06:36

+0

不知道你的問題是什麼,因爲沒有太多的信息,但'ListView's'height '不應該是'wrap_content'。它應該是'match_parent'或者指定的高度 – codeMagic 2014-09-19 23:52:33

回答

-1

兩週後這個問題strugling,我能夠找到一個真正的解決方案,

的問題是,主要的LinearLayout設置爲垂直,所以文本在ImageView的下方顯示!

感謝大家的幫助!

0

問題不在於自定義ArrayAdapter

通過觀察你的代碼,你在同一個片段中包含tab和listview,對嗎? 不要在其中包含選項卡和列表視圖。

修改代碼banksTab.setContent(R.id.Banks);banksTab.setContent(new Intent(this, activityClass));

在activity類中做你的listview邏輯。