0
 public class Inventory extends ListActivity  {

private ArrayList<Item> inv = new ArrayList<Item>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inventory);
    inv.add(new Item("Snake", 0, 50));
    inv.add(new Item("Snowball", 1, 200));
    inv.add(new Item("Stone", 4, 1000));

    setListAdapter(new ColorAdapter(this, R.layout.row, inv));
}


private class ColorAdapter extends ArrayAdapter<Item> {

    public ColorAdapter(Context context, int textViewResourceId, ArrayList<Item> inv) {
        super(context, textViewResourceId, inv);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        Item item = inv.get(position);

        TextView label = (TextView) view.findViewById(R.id.invrow);

        switch(position){
        case 0:
            label.setTextColor(Color.GRAY);
        case 1:
            label.setTextColor(Color.BLACK);
        default:
            label.setTextColor(Color.BLUE);
        }

        return view;
    }
}

私は Android を学んでいて、さまざまな色の要素でリストを作成しようとしています。私はすでにこのカスタム アダプターを作成しました。それが機能することを確認するために、最初の 2 つの項目を別の色にしたかったのですが、テキストは常に青に設定されています。

4

1 に答える 1