如何使这个工作,数组图像?

时间:2015-03-28 08:11:27

标签: android

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Image[] facultati ={R.drawable.faculate_1, R.drawable.faculatate_2, R.drawable.facultate_3, R.drawable.facultate_4};
        ArrayAdapter<Image> faculati_adapter = new ArrayAdapter<Image>(this,android.R.layout.simple_list_item_1,facultati);
        ListView myList = (ListView) findViewById(R.id.list_facultati);
        myList.setAdapter(faculati_adapter);
    }
}

我想制作一个图像列表,但我无法使其工作,我不知道如何制作阵列。

3 个答案:

答案 0 :(得分:0)

R文件夹中的资源是整数类型。 您应该声明类型为Integer的数组 您应该使用自定义适配器来显示图像或cardView

答案 1 :(得分:0)

使用以下代码:

  Integer[] facultati ={R.drawable.faculate_1, R.drawable.faculatate_2, R.drawable.facultate_3, R.drawable.facultate_4};

ArrayAdapter <Integer> dataAdapter = new ArrayAdapter<Integer>( this,android.R.layout.simple_spinner_item,facultati );
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   list.setAdapter(dataAdapter);

答案 2 :(得分:0)

为此创建自定义适配器,如下面的代码

public class UsersAdapter extends ArrayAdapter<Integer>{
 public class ViewHolder
{
    ImageView image;
}

public UsersAdapter(Context context, ArrayList<Integer> users) {
   super(context, R.layout.item_user, users);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Integer user = getItem(position);    
   ViewHolder viewHolder; // view lookup cache stored in tag
   if (convertView == null) {
      viewHolder = new ViewHolder();
      LayoutInflater inflater = LayoutInflater.from(getContext());
      convertView = inflater.inflate(R.layout.item_user, parent, false);
      viewHolder.image = (ImageView) convertView.findViewById(R.id.imageview);
      convertView.setTag(viewHolder);
   } else {
       viewHolder = (ViewHolder) convertView.getTag();
   }
   viewHolder.image.setDrawable(user);
  return convertView;

} }