• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Custom ListAdaptor Issues

mm2345

Lurker
Hey everyone, I'm a little stuck right now. i'm getting some funky errors when I try run my project.

Basically what it's doing is pulling a number of entries from a DB and displaying them in a custom ListAdaptor.

I'm getting "ArrayAdapter requires the resource ID to be a TextView" as a error

Here's where the Listview is populated

______________________________
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.status_container_list_entry, null);
}
JobListAdaptor o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView mt = (TextView) v.findViewById(R.id.middletext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
//TextView st = (TextView) v.findViewById(R.id.sidetext);

if (tt != null) {
tt.setText("Name: "+ o.getName());
}
if(mt != null){
mt.setText("Provider: "+ o.getProvider());
}
if(bt != null){
bt.setText("Most Recent Feed Date: "+ o.getMostRecentFeedDate());
}
//if(st != null){
// st.setText("Status: "+ o.getStatus());
//}

}
return v;
______________________________


Here's the XML display

______________________________
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/middletext"
android:singleLine="true"
android:ellipsize="marquee"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/sidetext"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
</LinearLayout>
______________________________



And where's its called in my Home Activity

______________________________
private void showStatusItems(List<StatusContainer> list)
{
_loginLayout.setVisibility(View.INVISIBLE);
_mainLayout.setVisibility(View.VISIBLE);


ArrayAdapter<JobListAdaptor> listAdapter = new ArrayAdapter<JobListAdaptor>(this, R.layout.status_container_list_entry, StatusContainer.getStatusItemList());

_statusContainerListView.clearChoices();
_statusContainerListView.setAdapter(listAdapter);

}
______________________________


Any help or tips would be greatly appreciated..
 
You should extend BaseAdapter and not use an ArrayAdapter.

Everything else looks ok though.

From: ArrayAdapter | Android Developers

ArrayAdapter
A ListAdapter that manages a ListView backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.
You could also look into a CursorAdapter. There are lots of tutorials online about both too. An ArrayAdapter really isnt what you need for what it looks like you are trying to do.

Minor nitpick: JobListAdaptor should be named something like "JobListObject" or "JobObj" as it's the object in the adapter, not an adapter itself. That might help you keep things straight in your head. If you're familiar with the MVC design pattern it's helpful to think of the adapter as an adapter to the MODEL.
 
Back
Top Bottom