我有一个愚蠢的问题,这是我的LinearLayout的LevelsAdapter类。我用它来显示我的活动中的水平。在getView方法中,我使用“if(level.getLevel()== 1)”来获取级别ID,我想询问是否可以创建一个循环来执行此操作?如果要检查100个级别或更多级别,那么我的实现会很糟糕。
private class LevelsAdapter extends ArrayAdapter<Level> {
private ArrayList<Level> levelsList;
public LevelsAdapter(Context context, int textViewResourceId,ArrayList<Level> levelsList) {
super(context, textViewResourceId, levelsList);
this.levelsList = new ArrayList<Level>();
this.levelsList.addAll(levelsList);
}
private class ViewHolder {
TextView level;
TextView levelQuestionsCount;
TextView lockedLevelContainer;
Button start;
RelativeLayout levelLayout;
LinearLayout levelLayoutInner;
LinearLayout unlockedLevelContainer;
ImageView locklevel;
}
private void unlock(ViewHolder holder){
holder.lockedLevelContainer.setVisibility(View.GONE);
holder.locklevel.setVisibility(View.GONE);
}
private void lock(ViewHolder holder, int toUnlock){
holder.unlockedLevelContainer.setVisibility(View.GONE);
holder.lockedLevelContainer.setVisibility(View.VISIBLE);
holder.lockedLevelContainer.setText("Level is locked. Answer "+ (toUnlock - countSolved) +" questions to unclock.");
holder.locklevel.setVisibility(View.VISIBLE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.level1, null);
holder = new ViewHolder();
holder.level = (TextView) convertView.findViewById(R.id.level);
holder.levelQuestionsCount = (TextView) convertView.findViewById(R.id.levelQuestionsCount);
holder.lockedLevelContainer = (TextView) convertView.findViewById(R.id.lockedLevelContainer);
holder.start = (Button) convertView.findViewById(R.id.buttonLevel);
holder.levelLayout = (RelativeLayout) convertView.findViewById(R.id.levelLayout);
holder.levelLayoutInner = (LinearLayout) convertView.findViewById(R.id.levelLayoutInner);
holder.unlockedLevelContainer = (LinearLayout) convertView.findViewById(R.id.unlockedLevelContainer);
holder.locklevel = (ImageView) convertView.findViewById(R.id.locklevel);
convertView.setTag(holder);
holder.start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
D.sh("Clicked");
Button start = (Button) v;
Level level = (Level) start.getTag();
Intent dialogIntent = new Intent(MainActivity.this, LevelActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.putExtra("level", level.getLevel());
MainActivity.this.startActivity(dialogIntent);
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Level level = levelsList.get(position);
holder.levelLayoutInner.setBackgroundResource(R.drawable.bg_level_repeat);
if(level.getLevel() == 1) {
holder.levelLayout.setBackgroundResource(R.drawable.level1_button);
if(countSolved >= Level.L1_TO_UNLOCK){
unlock(holder);
} else {
lock(holder, Level.L1_TO_UNLOCK);
}
}
if(level.getLevel() == 2) {
holder.levelLayout.setBackgroundResource(R.drawable.level2_button);
if(countSolved >= Level.L2_TO_UNLOCK){
unlock(holder);
} else {
lock(holder, Level.L2_TO_UNLOCK);
}
}
if(level.getLevel() == 3) {
holder.levelLayout.setBackgroundResource(R.drawable.level3_button);
if(countSolved >= Level.L3_TO_UNLOCK){
unlock(holder);
} else {
lock(holder, Level.L3_TO_UNLOCK);
}
}
if(level.getLevel() == 4) {
holder.levelLayout.setBackgroundResource(R.drawable.level4_button);
if(countSolved >= Level.L4_TO_UNLOCK){
unlock(holder);
} else {
lock(holder, Level.L4_TO_UNLOCK);
}
}
if(level.getLevel() == 5) {
holder.levelLayout.setBackgroundResource(R.drawable.level5_button);
if(countSolved >= Level.L5_TO_UNLOCK){
unlock(holder);
} else {
lock(holder, Level.L5_TO_UNLOCK);
}
}
holder.level.setText("Level "+level.getLevel());
holder.levelQuestionsCount.setText(level.getSolved() + "/" + level.getTotal());
holder.level.setTag(level);
holder.start.setTag(level);
return convertView;
}
}
答案 0 :(得分:0)
njzk2,我用过你的想法。
for (int i=1;i<=5;i++) {
if(level.getLevel() == i) {
int resID = getResources().getIdentifier("level" + i + "_button" , "drawable", getPackageName());
holder.levelLayout.setBackgroundResource(resID);
if(countSolved >= level.getToUnlock()){
unlock(holder);
} else {
lock(holder, level.getToUnlock());
}
}
}