作为我的第一个 Android 项目,我正在创建一个应用程序,允许用户显示来自 Magic The Gathering 集换式卡牌游戏的卡片。
我在我的 ArrayAdapter 中遇到了一个问题,它在列表视图中显示了一组卡片(根据版本)。
我正在读取一个看起来像这样的字符串,例如:{2}{B}{B} 并为每个字符创建 ImageViews.. 我将这些 ImageViews 添加到列表中的 LinearLayout 显示中。
问题是每次调用 getView() 时都会调用该过程,因此只要我向下滚动列表,ImageView 的数量就会不断增加。我希望一劳永逸地调用该程序。
任何帮助都会非常友好。如果您需要更多信息,请不要犹豫。
谢谢!鲁兹
package rudy.jaumain.mtgdb;
import java.util.ArrayList;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.content.Context;
import android.widget.TextView;
import android.graphics.Color;
public class MTGSetListAdapter extends ArrayAdapter<MTGCard> {
private ArrayList<MTGCard> set;
public MTGSetListAdapter(Context c, int resource, int textViewResourceId, ArrayList<MTGCard> set){
super(c, resource, textViewResourceId, set);
this.set = set;
}
static class ViewHolder{
TextView name;
ImageView rarity;
LinearLayout manacost;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = super.getView(position, convertView, parent);
System.out.println("CONVERTVIEW IS NULL");
holder = new ViewHolder();
holder.name = (TextView)convertView.findViewById(R.id.editionlist_item_name);
holder.rarity = (ImageView)convertView.findViewById(R.id.editionlist_item_rarity);
holder.manacost = (LinearLayout)convertView.findViewById(R.id.editionlist_item_manacost);
convertView.setTag(holder);
}
else{
System.out.println("CONVERTVIEW IS NOT NULL");
holder = (ViewHolder)convertView.getTag();
}
MTGCard currentCard = this.set.get(position);
if(currentCard != null){
holder.name.setText(currentCard.getName());
holder.name.setTextColor(Color.BLACK);
//Manacost example : {2}{B}{B}
String manacostStr = currentCard.getManacost();
int i=0;
String currentMana = null;
while(i < manacostStr.length()){
char currentChar = manacostStr.charAt(i);
if(currentChar == '{'){
currentMana = null;
}
else
{
if(currentChar == '}'){
ImageView iv = new ImageView(getContext());
int idMana = getContext().getResources().getIdentifier("mana_"+currentMana.toLowerCase(), "drawable", getContext().getPackageName());
iv.setImageResource(idMana);
iv.setAdjustViewBounds(true);
holder.manacost.addView(iv);
currentMana = null;
}
else{
if(currentMana == null){
currentMana = String.valueOf(currentChar);
}
else{
currentMana += String.valueOf(currentChar);
}
}
}
i++;
}
String rarityStr = currentCard.getRarity().toLowerCase();
int id = this.getContext().getResources().getIdentifier("rarity_"+rarityStr, "drawable", this.getContext().getPackageName());
holder.rarity.setImageResource(id);
/* COLOR SYMBOLS TO HANDLE LISTVIEW_ITEM BACKGROUNDCOLOR
*
* Colored artifacts are not handled for now as : AG --> O
*
* L : Land
* B : Black
* R : Red
* W : White
* G : Green
* U : Blue
* A : Artifact
* O : Gold (2 colors or more)
* C : DoubleSided Cards Back
*/
boolean posIsPair = (position %2 == 0);
int c;
char color = '\0';
if(currentCard.getColor().length()>1){
if(currentCard.getColor().charAt(0) == 'A'){
color = currentCard.getColor().charAt(1);
}
else color = 'O';
}
else{
color = currentCard.getColor().charAt(0);
}
switch(color){
case 'L' :
if(posIsPair)c = R.color.L1;
else c = R.color.L2;
break;
case 'B' :
if(posIsPair)c = R.color.B1;
else c = R.color.B2;
break;
case 'R' :
if(posIsPair)c = R.color.R1;
else c = R.color.R2;
break;
case 'W' :
if(posIsPair)c = R.color.W1;
else c = R.color.W2;
break;
case 'G' :
if(posIsPair)c = R.color.G1;
else c = R.color.G2;
break;
case 'U' :
if(posIsPair)c = R.color.U1;
else c = R.color.U2;
break;
case 'A' :
if(posIsPair)c = R.color.A1;
else c = R.color.A2;
break;
case 'O' :
if(posIsPair)c = R.color.O1;
else c = R.color.O2;
break;
case 'C' :
c = Color.TRANSPARENT;
break;
default :
c = Color.TRANSPARENT;
break;
}
convertView.setBackgroundColor(this.getContext().getResources().getColor(c));
}
return convertView;
}
@Override
public int getCount() {
return set.size();
}
@Override
public MTGCard getItem(int arg0) {
return set.get(arg0);
}
}