由于ListRecycling,我有一点问题, 问题是我有一个列表项,填充了几个textview和2个按钮。 一个按钮总是可见的(总是文本),但另一个按钮是空的,所以也不应该显示。
我尝试了一些事情,尝试调整自定义列表中复选框的实现,但没有运气。
public class PokeDexListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater LInflator;
ArrayList<PokeDexListItems> pokeDexList;
public static final String ROW_ID = "row_id"; // Intent extra key
public PokeDexListAdapter(Context context, ArrayList<PokeDexListItems> list) {
ctx = context;
pokeDexList = list;
LInflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return pokeDexList.size();
}
@Override
public Object getItem(int position) {
return pokeDexList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PokeDexListItems pokeDexListItems = pokeDexList.get(position);
if (convertView == null) {
convertView = LInflator.inflate(R.layout.nat_dex_list_item, null);
}
TextView pokeTv = (TextView) convertView.findViewById(R.id.nameDexTv);
TextView natDexTv = (TextView) convertView.findViewById(R.id.natDexTv);
Button type1 = (Button) convertView.findViewById(R.id.type1Btn);
Button type2 = (Button) convertView.findViewById(R.id.type2Btn);
ImageView pokeSprite = (ImageView) convertView
.findViewById(R.id.pokeSpriteIV);
pokeTv.setText(pokeDexListItems.getpokeName());
natDexTv.setText(pokeDexListItems.getNatDex());
type1.setText(pokeDexListItems.getType1());
type2.setText(pokeDexListItems.getType2());
AssetManager assetManager = ctx.getAssets();
try {
InputStream ims = assetManager.open("pokes/"
+ pokeDexListItems.getImageIndex() + ".gif");
Drawable d = Drawable.createFromStream(ims, null);
pokeSprite.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
if (type1.getText().toString().contains("Bug")) {
type1.setBackgroundResource(R.drawable.bug_button_border);
} else if (type1.getText().toString().contains("Dark")) {
type1.setBackgroundResource(R.drawable.dark_button_border);
} else if (type1.getText().toString().contains("Dragon")) {
type1.setBackgroundResource(R.drawable.dragon_button_border);
} else if (type1.getText().toString().contains("Electric")) {
type1.setBackgroundResource(R.drawable.electric_button_border);
} else if (type1.getText().toString().contains("Fairy")) {
type1.setBackgroundResource(R.drawable.fairy_button_border);
} else if (type1.getText().toString().contains("Fighting")) {
type1.setBackgroundResource(R.drawable.fight_button_border);
} else if (type1.getText().toString().contains("Fire")) {
type1.setBackgroundResource(R.drawable.fire_button_border);
} else if (type1.getText().toString().contains("Flying")) {
type1.setBackgroundResource(R.drawable.flying_button_border);
} else if (type1.getText().toString().contains("Ghost")) {
type1.setBackgroundResource(R.drawable.ghost_button_border);
} else if (type1.getText().toString().contains("Grass")) {
type1.setBackgroundResource(R.drawable.grass_button_border);
} else if (type1.getText().toString().contains("Ground")) {
type1.setBackgroundResource(R.drawable.ground_button_border);
} else if (type1.getText().toString().contains("Ice")) {
type1.setBackgroundResource(R.drawable.ice_button_border);
} else if (type1.getText().toString().contains("Normal")) {
type1.setBackgroundResource(R.drawable.normal_button_border);
} else if (type1.getText().toString().contains("Poison")) {
type1.setBackgroundResource(R.drawable.poison_button_border);
} else if (type1.getText().toString().contains("Psychic")) {
type1.setBackgroundResource(R.drawable.psychic_button_border);
} else if (type1.getText().toString().contains("Rock")) {
type1.setBackgroundResource(R.drawable.rock_button_border);
} else if (type1.getText().toString().contains("Steel")) {
type1.setBackgroundResource(R.drawable.steel_button_border);
} else if (type1.getText().toString().contains("Water")) {
type1.setBackgroundResource(R.drawable.water_button_border);
}
if (type2.getText().toString().contains("Bug")) {
type2.setBackgroundResource(R.drawable.bug_button_border);
} else if (type2.getText().toString().contains("Dark")) {
type2.setBackgroundResource(R.drawable.dark_button_border);
} else if (type2.getText().toString().contains("Dragon")) {
type2.setBackgroundResource(R.drawable.dragon_button_border);
} else if (type2.getText().toString().contains("Electric")) {
type2.setBackgroundResource(R.drawable.electric_button_border);
} else if (type2.getText().toString().contains("Fairy")) {
type2.setBackgroundResource(R.drawable.fairy_button_border);
} else if (type2.getText().toString().contains("Fighting")) {
type2.setBackgroundResource(R.drawable.fight_button_border);
} else if (type2.getText().toString().contains("Fire")) {
type2.setBackgroundResource(R.drawable.fire_button_border);
} else if (type2.getText().toString().contains("Flying")) {
type2.setBackgroundResource(R.drawable.flying_button_border);
} else if (type2.getText().toString().contains("Ghost")) {
type2.setBackgroundResource(R.drawable.ghost_button_border);
} else if (type2.getText().toString().contains("Grass")) {
type2.setBackgroundResource(R.drawable.grass_button_border);
} else if (type2.getText().toString().contains("Ground")) {
type2.setBackgroundResource(R.drawable.ground_button_border);
} else if (type2.getText().toString().contains("Ice")) {
type2.setBackgroundResource(R.drawable.ice_button_border);
} else if (type2.getText().toString().contains("Normal")) {
type2.setBackgroundResource(R.drawable.normal_button_border);
} else if (type2.getText().toString().contains("Poison")) {
type2.setBackgroundResource(R.drawable.poison_button_border);
} else if (type2.getText().toString().contains("Psychic")) {
type2.setBackgroundResource(R.drawable.psychic_button_border);
} else if (type2.getText().toString().contains("Rock")) {
type2.setBackgroundResource(R.drawable.rock_button_border);
} else if (type2.getText().toString().contains("Steel")) {
type2.setBackgroundResource(R.drawable.steel_button_border);
} else if (type2.getText().toString().contains("Water")) {
type2.setBackgroundResource(R.drawable.water_button_border);
} else {
type2.setVisibility(View.GONE);
}
final int tag = pokeDexListItems.getRowIdTag();
pokeTv.setTag(tag);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent viewPoke = new Intent(ctx, PokedexEntry.class);
// pass the selected contact's row ID as an extra with the
// Intent
viewPoke.putExtra(ROW_ID, getItemId(tag));
ctx.startActivity(viewPoke); // start the ViewBook Activity
} catch (Exception e) {
Toast.makeText(ctx, "This entry does not exist anymore!!",
Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
}
我现在得到的代码是,正确的观点, 但是当我开始滚动所有的秒按钮变得不可见的那一刻...... 不是我真正需要的xD
任何帮助表示赞赏, 提前谢谢你。
答案 0 :(得分:0)
我找到了我的问题的解决方案,经过几次试验和错误我试图将Visibility(View.VISIBLE)添加到其他“if语句”, 看到我的答案,以便更清楚地理解^^
public class PokeDexListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater LInflator;
ArrayList<PokeDexListItems> pokeDexList;
public static final String ROW_ID = "row_id"; // Intent extra key
public PokeDexListAdapter(Context context, ArrayList<PokeDexListItems> list) {
ctx = context;
pokeDexList = list;
LInflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return pokeDexList.size();
}
@Override
public Object getItem(int position) {
return pokeDexList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
ImageView pokeSprite;
TextView pokeTv;
TextView natDexTv;
Button type1;
Button type2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
PokeDexListItems pokeDexListItems = pokeDexList.get(position);
if (convertView == null) {
convertView = LInflator.inflate(R.layout.nat_dex_list_item, null);
holder = new ViewHolder();
holder.pokeTv = (TextView) convertView.findViewById(R.id.nameDexTv);
holder.natDexTv = (TextView) convertView
.findViewById(R.id.natDexTv);
holder.type1 = (Button) convertView.findViewById(R.id.type1Btn);
holder.type2 = (Button) convertView.findViewById(R.id.type2Btn);
holder.pokeSprite = (ImageView) convertView
.findViewById(R.id.pokeSpriteIV);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.pokeTv.setText(pokeDexListItems.getpokeName());
holder.natDexTv.setText(pokeDexListItems.getNatDex());
holder.type1.setText(pokeDexListItems.getType1());
holder.type2.setText(pokeDexListItems.getType2());
AssetManager assetManager = ctx.getAssets();
try {
InputStream ims = assetManager.open("pokes/"
+ pokeDexListItems.getImageIndex() + ".gif");
Drawable d = Drawable.createFromStream(ims, null);
holder.pokeSprite.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
if (holder.type1.getText().toString().contains("Bug")) {
holder.type1.setBackgroundResource(R.drawable.bug_button_border);
} else if (holder.type1.getText().toString().contains("Dark")) {
holder.type1.setBackgroundResource(R.drawable.dark_button_border);
} else if (holder.type1.getText().toString().contains("Dragon")) {
holder.type1.setBackgroundResource(R.drawable.dragon_button_border);
} else if (holder.type1.getText().toString().contains("Electric")) {
holder.type1
.setBackgroundResource(R.drawable.electric_button_border);
} else if (holder.type1.getText().toString().contains("Fairy")) {
holder.type1.setBackgroundResource(R.drawable.fairy_button_border);
} else if (holder.type1.getText().toString().contains("Fight")) {
holder.type1.setBackgroundResource(R.drawable.fight_button_border);
} else if (holder.type1.getText().toString().contains("Fire")) {
holder.type1.setBackgroundResource(R.drawable.fire_button_border);
} else if (holder.type1.getText().toString().contains("Flying")) {
holder.type1.setBackgroundResource(R.drawable.flying_button_border);
} else if (holder.type1.getText().toString().contains("Ghost")) {
holder.type1.setBackgroundResource(R.drawable.ghost_button_border);
} else if (holder.type1.getText().toString().contains("Grass")) {
holder.type1.setBackgroundResource(R.drawable.grass_button_border);
} else if (holder.type1.getText().toString().contains("Ground")) {
holder.type1.setBackgroundResource(R.drawable.ground_button_border);
} else if (holder.type1.getText().toString().contains("Ice")) {
holder.type1.setBackgroundResource(R.drawable.ice_button_border);
} else if (holder.type1.getText().toString().contains("Normal")) {
holder.type1.setBackgroundResource(R.drawable.normal_button_border);
} else if (holder.type1.getText().toString().contains("Poison")) {
holder.type1.setBackgroundResource(R.drawable.poison_button_border);
} else if (holder.type1.getText().toString().contains("Psychic")) {
holder.type1
.setBackgroundResource(R.drawable.psychic_button_border);
} else if (holder.type1.getText().toString().contains("Rock")) {
holder.type1.setBackgroundResource(R.drawable.rock_button_border);
} else if (holder.type1.getText().toString().contains("Steel")) {
holder.type1.setBackgroundResource(R.drawable.steel_button_border);
} else if (holder.type1.getText().toString().contains("Water")) {
holder.type1.setBackgroundResource(R.drawable.water_button_border);
}
if (holder.type2.getText().toString().contains("Bug")) {
holder.type2.setBackgroundResource(R.drawable.bug_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Dark")) {
holder.type2.setBackgroundResource(R.drawable.dark_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Dragon")) {
holder.type2.setBackgroundResource(R.drawable.dragon_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Electric")) {
holder.type2
.setBackgroundResource(R.drawable.electric_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Fairy")) {
holder.type2.setBackgroundResource(R.drawable.fairy_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Fight")) {
holder.type2.setBackgroundResource(R.drawable.fight_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Fire")) {
holder.type2.setBackgroundResource(R.drawable.fire_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Flying")) {
holder.type2.setBackgroundResource(R.drawable.flying_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Ghost")) {
holder.type2.setBackgroundResource(R.drawable.ghost_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Grass")) {
holder.type2.setBackgroundResource(R.drawable.grass_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Ground")) {
holder.type2.setBackgroundResource(R.drawable.ground_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Ice")) {
holder.type2.setBackgroundResource(R.drawable.ice_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Normal")) {
holder.type2.setBackgroundResource(R.drawable.normal_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Poison")) {
holder.type2.setBackgroundResource(R.drawable.poison_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Psychic")) {
holder.type2
.setBackgroundResource(R.drawable.psychic_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Rock")) {
holder.type2.setBackgroundResource(R.drawable.rock_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Steel")) {
holder.type2.setBackgroundResource(R.drawable.steel_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else if (holder.type2.getText().toString().contains("Water")) {
holder.type2.setBackgroundResource(R.drawable.water_button_border);
holder.type2.setVisibility(View.VISIBLE);
} else {
holder.type2.setVisibility(View.INVISIBLE);
}
final int tag = pokeDexListItems.getRowIdTag();
holder.pokeTv.setTag(tag);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent viewPoke = new Intent(ctx, PokedexEntry.class);
// pass the selected contact's row ID as an extra with the
// Intent
viewPoke.putExtra(ROW_ID, getItemId(tag));
ctx.startActivity(viewPoke); // start the ViewBook Activity
} catch (Exception e) {
Toast.makeText(ctx, "This entry does not exist anymore!!",
Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
}