Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Android Custom Adapter
Options
-
10-11-2012 7:51pmI'm storing course module data in a database and retrieving them to show them in a listview.
I want multiple database cells to be displayed in a single listitem, like this:*********************** * -----MyModules----- * * --------------------* * Code Time Day * * --------------------* * Code Time Day * * --------------------* * Code Time Day * * --------------------* * * * |Button| |Button| * ***********************
I'm not getting any errors. All that is appearing is Code - not Time or Day, for example.
Here is my Activity:
[PHP]public class ListModule extends ListActivity implements OnClickListener {
ListView listview;
ModuleDatabase mDB = new ModuleDatabase(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_module);
Module[] mods = mDB.loadModules();
listview = (ListView) findViewById(android.R.id.list);
Module [] arr = new Module[] {
new Module(1,"COMP23321", "Android", "Lecture", "Monday", "12.20", "B0.06", "This is a **** class"),
new Module(2,"COMP36365", "OS", "Practical", "Thursday", "15.00", "B0.16", "This is a great practical"),
new Module(3,"COMP16556", "Java", "Lecture", "Friday", "09.30", "B0.01", "This is a dog")
};
ModuleAdapter adapter = new ModuleAdapter(this, R.layout.module_list_row, mods);
// Assign adapter to ListView
adapter.notifyDataSetChanged();
View header = getLayoutInflater().inflate(R.layout.module_list_header, null);
View footer = getLayoutInflater().inflate(R.layout.module_list_footer, null);
listview.addHeaderView(header);
listview.addFooterView(footer);
listview.setAdapter(adapter);
}[/php]
This is the adapter class:
[PHP]package com.mcnulty.mytimetable;
public class ModuleAdapter extends ArrayAdapter<Module> {
Context context;
int listViewResourceId;
Module courses[] = null;
public ModuleAdapter(Context context, int listViewResourceId, Module[] courses) {
super(context, listViewResourceId, courses);
// TODO Auto-generated constructor stub
this.listViewResourceId = listViewResourceId;
this.context = context;
this.courses = courses;
}
public static class ModuleHolder
{
public TextView txtCode;
// TextView txtName;
public TextView txtType;
public TextView txtDay;
public TextView txtTime;
public TextView txtLocation;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ModuleHolder holder;
if(row == null)
{
LayoutInflater inflater = ((ListActivity)context).getLayoutInflater();
row = inflater.inflate(listViewResourceId, parent, false);
holder = new ModuleHolder();
holder.txtCode = (TextView)row.findViewById(R.id.txtCode);
// holder.txtName = (TextView)row.findViewById(R.id.txtName);
holder.txtType = (TextView)row.findViewById(R.id.txtType);
holder.txtDay = (TextView)row.findViewById(R.id.txtDay);
holder.txtTime = (TextView)row.findViewById(R.id.txtTime);
holder.txtLocation = (TextView)row.findViewById(R.id.txtLocation);
row.setTag(holder);
}
else
{
holder = (ModuleHolder)row.getTag();
}
Module module = courses[position];
holder.txtType.setText(module.getCode());
holder.txtCode.setText(module.getType().substring(0, 1));
holder.txtDay.setText(module.getDay().substring(0, 2));
holder.txtTime.setText(module.getTime().toString());
holder.txtLocation.setText(module.getLocation());
return row;
}
}
[/PHP]
This is the listview row:
[HTML]<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp">
<TextView android:id="@+id/txtCode"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="10dp"
android:textColor="#ff0000"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"/>
<TextView android:id="@+id/txtType"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_toRightOf="@id/txtCode"
android:textStyle="bold"
android:textSize="10dp"
android:textColor="#000000"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:text="@string/class_type"/>
<TextView android:id="@+id/txtDay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_toRightOf="@id/txtType"
android:textStyle="bold"
android:textSize="10dp"
android:textColor="#000000"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:text="@string/day"/>
<TextView android:id="@+id/txtTime"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_toRightOf="@id/txtDay"
android:textStyle="bold"
android:textSize="10dp"
android:textColor="#ffff00"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" />
<TextView android:id="@+id/txtLocation"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_toRightOf="@id/txtTime"
android:textStyle="bold"
android:textSize="10dp"
android:textColor="#000000"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"/>
</RelativeLayout>[/HTML]
Let me know if I'm missing anything.
Can anyone help with this?Tagged:0
Comments
-
I figured it out.
the layout heights and widths of the TextViews were fill_parent and should be wrap_content. The first TextView was taking up all the parent space leaving none for the others0 -
Advertisement