Daniel Andujar Daniel Andujar - 24 days ago 6
Android Question

ListView with CustomArrayAdapter showing everything empty

I want to use a custom layout for the ListView Items, I created my XML, have my custom object and created the custom ArrayAdapter, passed the adapter but everything shows empty

no clue of errors on LogCat

I believe the problem is in the

ArrayAdapter
Class, here's my code

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;
}
}


So this is where I set the adapter to the 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;
}


If I comment the ImageView, I can see the sample image that I have set, but if I uncomment it, nothing is shown.

Here's the XML layout for the list item

<?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>


Here's the layout of the fragment where I show this list item (where I'm getting everything empty

<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>


Any ideas?

Answer

After two weeks strugling with this problem, i was able to find a real solution,

the problem is, that the main LinearLayout was set to VERTICAL, so the texts where displaying below the imageView!

Thanks to everyone that had helped!!