2013-11-15 143 views
0

我想根據字符串值設置我的imageview。但圖像從未設置。請指導我,如果我在這裏做錯了。由於Android - 無法在自定義適配器中設置我的imageView

這裏是我的代碼:

public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    getExpense expense = objects.get(position); 
    EventController controller = new EventController(ctx); 
    View view = convertView; 
    if (view == null) { 
     view = lInflater.inflate(R.layout.view_expense_entry, parent, false); 
    } 
    TextView expenseId = (TextView)view.findViewById(R.id.expenseId); 
    expenseId.setText(expense.getExpenseId()); 
    TextView expenseName = (TextView)view.findViewById(R.id.expenseName); 
    expenseName.setText(expense.getExpenseName()); 
    TextView expenseQuantity = (TextView)view.findViewById(R.id.expenseQuantity); 
    expenseQuantity.setText("Quantity : x" + expense.getExpenseQuantity()); 
    ImageView expenseImage = (ImageView)view.findViewById(R.id.ivtype); 
    String expenseType = controller.getExpenseType(expenseId.getText().toString()); 
    if (expenseType.equals("Food")){ 
     expenseImage.setImageResource(R.drawable.food); 
    } 
    else if (expenseType.equals("Beverage")){ 
     expenseImage.setImageResource(R.drawable.drink); 
    } 
    else if (expenseType.equals("Dessert")){ 
     expenseImage.setImageDrawable(ctx.getResources().getDrawable(R.drawable.dessert)); 
    } 
    else if (expenseType.equals("Set")){ 
     expenseImage.setImageDrawable(ctx.getResources().getDrawable(R.drawable.set)); 
    } 

    return view; 
} 
+1

您確定您是否達到條件?您是在調試器中完成的,還是可以添加日誌語句?即:Log.i(「DEBUG」,費用類型)。 – wyoskibum

+0

你可以發佈錯誤日誌嗎? – jasonflaherty

+0

@jasonflaherty找不到錯誤。 – AndroidWorld12

回答

0

除了我的關於確保IF conditons得到滿足上述評論。你正在使用需要一個整數的setImageResource,然後你使用的是getDrawable。所以你的代碼不一致。

試試這個:

int resourceId = 0; 

if (expenseType.equals("Food")){ 
    resourceId = ctx.getResources().getIdentifier("Food","drawable",ctx.getPackageName()); 
} 
else if (expenseType.equals("Beverage")){ 
    resourceId = ctx.getResources().getIdentifier("drink","drawable",ctx.getPackageName()); 
} 
else if (expenseType.equals("Dessert")){ 
    resourceId = ctx.getResources().getIdentifier("dessert","drawable",ctx.getPackageName()); 
} 
else if (expenseType.equals("Set")){ 
    resourceId = ctx.getResources().getIdentifier("set","drawable",ctx.getPackageName()); 
} 
expenseImage.setImageResource(resourceId); 
相關問題