我想为ListView项使用自定义布局,我创建了我的XML,拥有自定义对象并创建了自定义ArrayAdapter,传递了适配器,但一切都显示为空
没有关于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布局
<?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>
这里是我显示此列表项的片段的布局(我将所有内容都清空
<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 :(得分:0)
问题不在于自定义ArrayAdapter
。
通过观察您的代码,您可以在同一个片段中包含tab和listview,对吧? 不要将tab和listview合二为一。
将代码banksTab.setContent(R.id.Banks);
修改为banksTab.setContent(new Intent(this, activityClass));
。
在活动类中执行listview逻辑。
答案 1 :(得分:-1)
经过两周的努力,我找到了一个真正的解决方案,
问题是,主要的LinearLayout设置为VERTICAL,因此显示在imageView下方的文本!
感谢所有帮助过的人!