CursorAdapter有两个行布局

时间:2014-11-10 18:57:41

标签: android android-cursoradapter

好吧,因为我在搜索时没有找到一致的方法,所以我决定将我的代码放在别人看来可能有帮助。

我正在创建一个使用卡片的游戏。这些卡可以根据不同因素锁定或解锁。重要的是,我想检查sqlite db是否被锁定,然后为两种可能的结果显示不同的行布局。

这是我的代码:

public class AllCardsAdapter extends CursorAdapter {

LockedHolder viewLocked;
UnlockedHolder viewUnlocked;
private LayoutInflater mInflater;
public static final int LOCKED = 0;
public static final int UNLOCKED = 1;

public AllCardsAdapter(Context context, Cursor cursor) {
    super(context, cursor);
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    final int type;

    int viewType = this.getItemViewType(cursor.getInt(cursor.getColumnIndex("unlocked")));

    if (viewType == 1){
        type = UNLOCKED;
    } else {
        type = LOCKED;
    }

    if (type == LOCKED){
        viewLocked = (LockedHolder) view.getTag();
        viewLocked.nameHolder.setText(cursor.getString(cursor.getColumnIndex("name")));
        viewLocked.awardedAtHolder.setText("Awarded at level:");
        viewLocked.reqLvlHolder.setText(cursor.getString(cursor.getColumnIndex("reqlvl")));

        String imagePath = cursor.getString(cursor.getColumnIndex("image"));

        if (imagePath.equals("card_obj_plus_1")){
            Picasso.with(context).load(R.drawable.card_obj_plus_1).placeholder(R.drawable.card_placeholder).into(viewLocked.imageHolder);
        }
        if (imagePath.equals("card_obj_plus_2")){
            Picasso.with(context).load(R.drawable.card_obj_plus_2).placeholder(R.drawable.card_placeholder).into(viewLocked.imageHolder);
        }
        if (imagePath.equals("card_obj_plus_3")){
            Picasso.with(context).load(R.drawable.card_obj_plus_3).placeholder(R.drawable.card_placeholder).into(viewLocked.imageHolder);
        }
        if (imagePath.equals("card_slowdown")){
            Picasso.with(context).load(R.drawable.card_slowdown).placeholder(R.drawable.card_placeholder).into(viewLocked.imageHolder);
        }
        if (imagePath.equals("card_speed_up")){
            Picasso.with(context).load(R.drawable.card_speed_up).placeholder(R.drawable.card_placeholder).into(viewLocked.imageHolder);
        }

    } else {
        viewUnlocked = (UnlockedHolder) view.getTag();
        viewUnlocked.nameHolder.setText(cursor.getString(cursor.getColumnIndex("name")));

        String imagePath = cursor.getString(cursor.getColumnIndex("image"));

        if (imagePath.equals("card_obj_plus_1")){
            Picasso.with(context).load(R.drawable.card_obj_plus_1).placeholder(R.drawable.card_placeholder).into(viewUnlocked.imageHolder);
        }
        if (imagePath.equals("card_obj_plus_2")){
            Picasso.with(context).load(R.drawable.card_obj_plus_2).placeholder(R.drawable.card_placeholder).into(viewUnlocked.imageHolder);
        }
        if (imagePath.equals("card_obj_plus_3")){
            Picasso.with(context).load(R.drawable.card_obj_plus_3).placeholder(R.drawable.card_placeholder).into(viewUnlocked.imageHolder);
        }
        if (imagePath.equals("card_slowdown")){
            Picasso.with(context).load(R.drawable.card_slowdown).placeholder(R.drawable.card_placeholder).into(viewUnlocked.imageHolder);
        }
        if (imagePath.equals("card_speed_up")){
            Picasso.with(context).load(R.drawable.card_speed_up).placeholder(R.drawable.card_placeholder).into(viewUnlocked.imageHolder);
        }

    }

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    viewLocked = new LockedHolder();
    viewUnlocked = new UnlockedHolder();
    final int type;

    int viewType = this.getItemViewType(cursor.getInt(cursor.getColumnIndex("unlocked")));

    if (viewType == 1){
        type = UNLOCKED;
    } else {
        type = LOCKED;
    }
    System.out.println(viewType);
    System.out.println(type);

    if (type == LOCKED){
        View lockedView;
        lockedView = mInflater.inflate(R.layout.card_list_row_disabled, parent, false);
        viewLocked.nameHolder = (TextView) lockedView.findViewById(R.id.txtTitle);
        viewLocked.imageHolder = (ImageView) lockedView.findViewById(R.id.imgThumbnail);
        viewLocked.reqLvlHolder = (TextView) lockedView.findViewById(R.id.tvLevelNr);
        viewLocked.awardedAtHolder = (TextView) lockedView.findViewById(R.id.tvAwardedAt);
        lockedView.setTag(viewLocked);

        return lockedView;
    } else {
        View unlockedView;
        unlockedView = mInflater.inflate(R.layout.card_list_row, parent, false);
        viewUnlocked.nameHolder = (TextView) unlockedView.findViewById(R.id.txtTitle);
        viewUnlocked.imageHolder = (ImageView) unlockedView.findViewById(R.id.imgThumbnail);
        unlockedView.setTag(viewUnlocked);

        return unlockedView;
    }

}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return position % 2;
}

public class LockedHolder {
    public TextView nameHolder;
    public ImageView imageHolder;
    public TextView awardedAtHolder;
    public TextView reqLvlHolder;
}

public class UnlockedHolder {
    public TextView nameHolder;
    public ImageView imageHolder;
}

}

我得到的错误是在行“viewLocked =(LockedHolder)view.getTag();”

AllCardsAdapter $ UnlockedHolder无法强制转换为AllCardsAdapter $ LockedHolder

当我有一个包含锁定和解锁卡片的列表时,错误才会显示。

看到我知道它想要做什么,我只是不知道为什么。另外,如果我没有充分理由过度复杂化,或者遗漏了一些明显的东西,如果你发现它我会非常感激..

1 个答案:

答案 0 :(得分:1)

getItemViewType 实现看起来不正确(类型不依赖于项目位置)它应该类似于以下内容

private int getItemViewType(Cursor cursor) {
    return cursor.getInt(cursor.getColumnIndex("unlocked")) % 2;
}

@Override
public int getItemViewType(int position) {
    Cursor cursor = (Cursor) getItem(position);
    return getItemViewType(cursor);
}

您还需要更新 bindView newView

int viewType = this.getItemViewType(cursor);