Beginning Android programming, coming from html/php/css, I have searched the web for a simple way to separate my code from my style - for now I need to display data from a database in a list- or table view.
Simply put, I get a cursor from the database, iterate through it, creating each list-item dynamically in code as TextViews. Then I would like to apply a style from an external xml layout file to each item.
Pseudo-code:
style.xml:
//mystyle: bold, 12pt
//yourstyle: italic, 11pt
Activity:
for (each cursor-entry)
{
tv1 = new TextView();
applyStyle(tv, mystyle);
tv2 = new TextView();
applyStyle(tv, yourstyle);
//Apply content to textviews from the cursor...
}
mainLayout.setView(tv1);
mainLayout.setView(tv2);
The code examples I've found around the net, uses multiple lines of code, or multiple xml files (using inflate, or cursorAdapters), and IMO quickly become bloated. I just want a nice neat way to apply a style to a dynamically created code. Is this possible?
If you are using ListView, it is so simple to have an XML file for rows. The only thing you need is an XML file and a Adapter class. Take a look at this simple example:
To read data from database, create a helper class like this:
public class MessagingDatabaseAdapter {
protected SQLiteDatabase database;
public MessagingDatabaseAdapter(Context context) {
MessagingDatabaseHelper databaseHelper = new MessagingDatabaseHelper(context, "message_history_db");
database = databaseHelper.getWritableDatabase();
}
public void close() {
database.close();
}
public void Entity[] getAllEntities() {
Entity[] values = null;
String query = "select * from TABLE_NAME";
Cursor cursor = null;
try {
cursor = database.rawQuery(query, null);
if( cursor.moveToFirst() ) {
int s = cursor.getCount();
values = new Entity[s];
do {
Entity entity = new Entity();
entity.setSomeProperty(cursor.getInt(cursor.getColumnIndex(SOME_PROPERTY_COLUMN)));
values[i++] = entity;
} while( cursor.moveToNext() );
}
} catch(Exception ex) {
} finally {
if( cursor != null ) {
cursor.close();
}
return values;
}
}
protected class MessagingDatabaseHelper extends SQLiteOpenHelper {
public MessagingDatabaseHelper(Context context, String name) {
super(context, name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Your SQL to create Tables");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
In your Activity class:
MessagingDatabaseAdapter db = new MessagingDatabaseAdapter();
values = db.getAllEntities();
db.close();
list_view = (ListView) findViewById(R.id.list_view);
ListAdapter adapter = new ListAdapter(this, values);
list_view.setAdapter(adapter);
And ListAdapter class:
public class ListAdapter extends ArrayAdapter<Entity> {
final Context context;
final Entity[] values;
public ListAdapter(Context context, Entity[] values) {
super(context, R.layout.list_screen, values);
this.values = values;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_view_item, parent, false);
TextView datetimeTextView = (TextView) rowView.findViewById(R.id.list_view_datetime_text_view);
datetimeTextView.setTypeface(someTypeFace);
return rowView;
}
}
And row layout XML file (list_view_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/BorderedFrame" >
<TextView
android:id="@+id/inbox_list_view_datetime_text_view"
style="@style/MediumText"
android:layout_width="wrap_content" >
</TextView>
</RelativeLayout>