在Android BaseAdapter中设置编程图像

时间:2013-12-06 11:57:04

标签: java android

拜托,我有一个问题,我正在编写我的应用程序,我想使用动态设置drawable图像。

表示我想使用:flag.setImageResource(R.drawable.+my_variable+); 这是我的代码请在这方面帮助我。

public class AlphabetAdapter extends BaseAdapter {

  private Context context;
  private final String[] alphabets;

public AlphabetAdapter(Context context, String[] alphabets) {

   this.context = context;
   this.alphabets = alphabets;

}

public View getView(int position, View convertView, ViewGroup parent) {

  LayoutInflater inflater = (LayoutInflater)   context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;
        if (convertView == null) {

                gridView = new View(context);
                gridView = inflater.inflate(R.layout.custom_six_alphabets, null);
                TextView textView = (TextView) gridView.findViewById(R.id.label);
        textView.setText(alphabets[position]);
        ImageView flag = (ImageView) gridView .findViewById(R.id.flag);
        String mobile = alphabets[position];

                if (mobile.equals("apple")) {
        flag.setImageResource(R.drawable.apple);

            } else if (mobile.equals("ant")) {
                flag.setImageResource(R.drawable.ant);
            } else if (mobile.equals("aeroplane")) {
                flag.setImageResource(R.drawable.airplane);
            } else if (mobile.equals("alligator")) {
                flag.setImageResource(R.drawable.alligator);
            } else if (mobile.equals("arrow")) {
                flag.setImageResource(R.drawable.arrow);
            } else {
                flag.setImageResource(R.drawable.arm);
            }

        } else {
            gridView = (View) convertView;
        }

        return gridView;

}

3 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

int i = context.getResources().
             getIdentifier(mobile, "drawable", this.getPackageName());
flag.setImageResource(i);

上面的代码:将首先从drawable中获取移动设备的id,这相当于R.drawable。{mobile}例如,如果mobile是“ant”,则代码将返回drawable ant的id,即R.drawable.ant和第2个我们将使用此ID并设置Image Resource ..

答案 1 :(得分:0)

您可以尝试更改int类型的数组。 替换这个:

String[] alphabets;

为此:

int[] alphabets;

在您的代码中,您可以使用以下行:

rh.imageviewLeft.setBackgroundResource(alphabets[position]);

注意:在您发送的数组中,例如填写它:

int [] img = new int [] { R.drawable.apple, R.drawable.ant }

并在您的适配器中添加:

@Override
public int getCount()
{
    return alphabets.length;
}

@Override
public Object getItem(int position)
{
    return alphabets[position];
}

@Override
public long getItemId(int position)
{
    return position;
}

我希望得到帮助,抱歉我的英语不好。

答案 2 :(得分:0)

试试这个.. 在你的活动中

public static HashMap<String, Integer> ThemeId = new HashMap<String, Integer>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
.....
        ThemeId.clear();
        ThemeId.put("apple", R.drawable.apple);
        ThemeId.put("ant", R.drawable.ant);
        ThemeId.put("aeroplane", R.drawable.aeroplane);
        ThemeId.put("alligator", R.drawable.alligator);
        ThemeId.put("arrow", R.drawable.arrow);
        ThemeId.put("arm", R.drawable.arm);

}

//在基本适配器

 import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class AlphabetAdapter extends BaseAdapter {

        private Context context;
        private final String[] alphabets;

        public AlphabetAdapter(Context context, String[] alphabets) {

            this.context = context;
            this.alphabets = alphabets;

        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View gridView;
            if (convertView == null) {

                gridView = new View(context);
                gridView = inflater.inflate(R.layout.custom_six_alphabets, null);
                TextView textView = (TextView) gridView.findViewById(R.id.label);
                textView.setText(alphabets[position]);
                ImageView flag = (ImageView) gridView.findViewById(R.id.flag);
                String mobile = alphabets[position];


                    flag.setImageResource(YourActivity.ThemeId.get(mobile));


            } else {
                gridView = (View) convertView;
            }

            return gridView;

        }

        @Override
        public int getCount() {
            return alphabets.length;
        }

        @Override
        public String getItem(int position) {
            return alphabets[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }