Something that puzzles me since my first steps with Android is the optimized use of a custom adapter with different layouts for different orientations. Currently I do end up in cascades of NULL checks and I would like to know if there's a better way.
Consider two different layouts for vertical orientation and horizontal orientation that do contain a different count of widg开发者_JAVA技巧ets.
Here's the vertical layout with one widget located in layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal" >
<TextView
style="@style/TextViewDefault"
android:id="@+id/name"
android:layout_weight="1" />
</LinearLayout>
Here's the horizontal layout with two widgets located in layout-land:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal" >
<TextView
style="@style/TextViewDefault"
android:id="@+id/name"
android:layout_weight="1" />
<TextView
style="@style/TextViewDefault"
android:id="@+id/description"
android:layout_weight="1" />
<TextView
style="@style/TextViewDefault"
android:id="@+id/date"
android:layout_weight="1" />
</LinearLayout>
And here's the custom adapter - an extended SimpleCursorAdapter. Depending on the orientation the description and date objects are null or non-null. In my Android projetcs I do have row layouts with 10ths of objects and these null-checks become really nasty in the code. Is there a better solution? How do you guys work with custom adapters and different layouts for different orientations or sizes?
public class TestAdapter extends SimpleCursorAdapter {
private static class ViewHolder {
private TextView date;
private TextView description;
private TextView name;
}
private Context context;
private Cursor cursor;
private int layout;
private SQLiteDatabase sqliteDatabase;
private MySQLiteOpenHelper sqliteOpenHelper;
public Test(final Context context, final int layout, final Cursor cursor, final String[] from, final int[] to) {
super(context, layout, cursor, from, to);
this.context = context;
this.cursor = cursor;
this.layout = layout;
if (sqliteOpenHelper == null) {
sqliteOpenHelper = new MySQLiteOpenHelper(context);
}
if (sqliteOpenHelper != null) {
if (sqliteDatabase == null) {
sqliteDatabase = sqliteOpenHelper.getWritableDatabase();
}
}
}
@Override
public View getView(final int position, final View contentView, final ViewGroup viewGroup) {
View view = null;
ViewHolder viewHolder = null;
if (contentView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(layout, null);
if (view != null) {
viewHolder = new ViewHolder();
viewHolder.date = (TextView) view.findViewById(R.id.date);
viewHolder.description = (TextView) view.findViewById(R.id.description);
viewHolder.name = (TextView) view.findViewById(R.id.name);
view.setTag(viewHolder);
}
} else {
view = contentView;
viewHolder = (ViewHolder) contentView.getTag();
}
if (viewHolder != null) {
cursor.moveToPosition(position);
String date = cursor.getString(cursor.getColumnIndex(Test.DATE));
String description = cursor.getString(cursor.getColumnIndex(Test.DESCRIPTION));
String name = cursor.getString(cursor.getColumnIndex(Test.NAME));
if (viewHolder.date != null) {
if (!StringUtils.isEmpty(date) && date.length() == 10) {
viewHolder.date.setText(Tools.formatDate(date.substring(0, 10)));
} else {
viewHolder.date.setText("");
}
}
if (viewHolder.description != null) viewHolder.description.setText(description);
if (viewHolder.name != null) viewHolder.name.setText(name);
}
return view;
}
}
Since you have different widget count you have to deal with the fact setting different data.
But you can get rid of following null checks:
// wouldn't you expect to have a working layout here?
View != null
// when view != null removed this is obsolete
viewHolder != null
// the same thing here, you would expect a name here
viewHolder.name != null
// put that under viewHolder.date != null since both are available when one of them is
viewHolder.description.setText(description);
精彩评论