我是Android的新手,我想给我的列表活动的每个项目一个不同的图标,所以我该怎么做...这是我的列表活动与不同的项目
public class MenuAct extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] {
"Mes Missions",
"Mes Points",
"Importer Geotiff",
"Importer Point KML",
"Importer Zone KML",
"Exporter" };
// use your custom layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.menu_activity, R.id.label, values);
setListAdapter(adapter);
}
答案 0 :(得分:0)
您想创建自己的ListAdapter,然后修改它以设置图像。
查看http://www.javacodegeeks.com/2013/06/android-listview-custom-adapter-with-imageview.html以获取示例。
答案 1 :(得分:0)
适配器负责创建与每个项目关联的视图。在这种情况下,您将使用ArrayAdapter
为每个项目返回一个视图,该视图包含(或多或少)单个TextView,并且每行都对应一个字符串。
您需要做的是创建一个自定义适配器(派生自BaseAdapter),返回更复杂的视图(特别是包括您想要的ImageView)。
这是一个很好的示例:http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown_custom
答案 2 :(得分:0)
您必须创建自定义适配器。在你将覆盖的适配器的getView()方法中,你可以轻松地在每个项目上放置你想要的任何图标。
你可以这样做:
someListView.setAdapter(new ImageAdapter(mContext));
然后,如果你将数据保存在ArrayList或类似的(我称之为我的pathListThumbs),请像这样定义ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return pathListThumbs.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, final ViewGroup parent) {
查看itemView = convertView; if(gridView == null){//如果它没有被回收,则初始化一些属性 //从xml获取布局 LayoutInflater inflater =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); itemView = inflater.inflate(R.layout.mylistviewitem,null); }
final GridProductHolder holder;
if (gridView.getTag() != null) {
holder = (GridProductHolder) gridView.getTag();
} else {
holder = new GridProductHolder();
gridView.setTag(holder);
holder.thumbNail = (ImageView) gridView.findViewById(R.id.thumbnail);
}
Bitmap bitmap = pathListThumbs.get(position), wScale, hScale);
if (bitmap != null) {
holder.thumbNail.setImageBitmap(bitmap);
}
}
}
答案 3 :(得分:0)
好的,这是我用来将相机图像尺寸调整到所需x / y值的片段:
if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) {
_bitmapPreScale = gradeBookDbAdapter.getStudentPhoto(studentName);
}
if (_bitmapPreScale != null) {
int oldWidth = _bitmapPreScale.getWidth();
int oldHeight = _bitmapPreScale.getHeight();
int newWidth = 200; // whatever your desired width and height are
int newHeight = 200;
// calculate the scale
float scaleWidth = ((float) newWidth) / oldWidth;
float scaleHeight = ((float) newHeight) / oldHeight;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0, oldWidth, oldHeight, matrix, true);
// write the image back out so it's always small
try {
FileOutputStream out = new FileOutputStream(imgFile);
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 50, out);
out.flush();
out.close();
} catch (Exception e) {
Toast.makeText(mContext,"ERROR writing new image. Please try again",Toast.LENGTH_SHORT).show();
Log.e("STUDENT", "ERROR WRITING");
e.printStackTrace();
}
}
else {
Toast.makeText(mContext,"ERROR reading image. Please try again",Toast.LENGTH_SHORT).show();
Log.e("STUDENT","ERROR READING");
}
答案 4 :(得分:0)
扩展BaseAdapter,编写自己的adpter
覆盖getView,因此您也可以设置图像
适配器代码的getView应该是(未经测试)
@Override
public View getView(int index, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = mInflater.inflate(R.layout.menu_activity, parent, false);
TextView label = convertView.findViewById(R.id.label);
label.setText(values[index]);
Drawable icon = // obtain your image
label.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
return convertView;
}