gpt4 book ai didi

android - 具有两行布局的 CursorAdapter

转载 作者:行者123 更新时间:2023-11-30 02:34:07 26 4
gpt4 key购买 nike

好吧,因为我在搜索时没有找到执行此操作的一致方法,所以我决定将我的代码放在其他人的眼睛上可能会有所帮助。

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

这是我的代码:

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

仅当我有一个包含已锁定和未锁定卡的列表时才会出现此错误。

我知道它要做什么,只是不知道为什么。另外,如果我无缘无故地使事情过于复杂,或者遗漏了一些明显的东西,如果您能找到它,我将不胜感激..

最佳答案

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);
}

您还需要更新bindViewnewView

int viewType = this.getItemViewType(cursor);

关于android - 具有两行布局的 CursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26850917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com