自从我使用Android的第一步以来,让我感到困惑的是优化使用具有不同方向的不同布局的自定义适配器。目前我最终会进行NULL检查,我想知道是否有更好的方法。
考虑垂直方向和水平方向的两种不同布局,它们包含不同数量的小部件。
这是垂直布局,其中一个小部件位于布局中:
<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>
这是水平布局,其中两个小部件位于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>
这是自定义适配器 - 扩展的SimpleCursorAdapter。根据方向,描述和日期对象为null或非null。在我的Android projetcs中,我确实有行布局和10个对象,这些空检查在代码中变得非常讨厌。有更好的解决方案吗?你们如何使用不同方向或尺寸的自定义适配器和不同布局?
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;
}
}
答案 0 :(得分:0)
由于你有不同的小部件数,你必须处理设置不同数据的事实 但是你可以摆脱以下空检查:
// 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);