현재 구현은 이름 데이터 객체의 텍스트 데이터를 텍스트 뷰에 바인딩해야합니다. 이미지보기가 올바른 이미지를 표시하지 않는 이유는 어댑터가 이미지보기와 통신 할 수있는 방법이 없기 때문입니다. 배열 어댑터의 기본 구현은 list.xml 뷰를 팽창시키고 그 list.xml에서 R.id.label이 나타내는 뷰를 꺼냅니다. 해당 뷰가 텍스트 뷰인지 또는 이미지 뷰인지 확인합니다. 둘 다 같지 않으면 예외가 발생합니다. 그런 다음 이름 데이터 객체의 데이터를 텍스트 뷰에 바인딩합니다.
사용자 지정 어댑터를 지정하고 데이터 모델에 이름의 문자열과 이미지 리소스에 대한 참조가 들어 있는지 확인해야합니다.이 경우 정수가됩니다. 의 getView,하는 getPosition, getCount를 BaseAdapter을하는 클래스를 작성하고 확장하여 시작
public class Name
{
private String _name;
private int _imageResourceId;
public void Name(String name, int resId)
{
_name = name;
_imageResourceId = resId;
}
public void setImageResId(int resId)
{
_imageResrouceId = resId;
}
public void setName(String name)
{
_name = name;
}
public int getImageResId()
{
return _imageResourceId;
}
public String getName()
{
return _name;
}
}
을하고 구현 등
다음은 샘플 어댑터입니다 : 여기
당신을 위해 작동 할 수있는 샘플 이름 객체의 그것은 당신을 위해 잘 작동 될 수 있습니다
public class NameAdapter extends BaseAdapter
{
private Name[] _data;
private LayoutInflater _inflater;
public NameAdapter(Context context, Name[] data)
{
_data = data;
_inflater = LayoutInflater.from(context);
}
public int getCount()
{
if(_data != null)
{
return _data.length;
}
else
{
return 0;
}
}
public Object getItem(int position)
{
if(_data != null)
{
return _data[position];
}
else
{
return null;
}
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView == null)
{
convertView = _inflater.inflate(R.layout.list, parent);
holder = new ViewHolder();
holder._image = (ImageView) convertView.findViewById(R.id.icon);
holder._label = (TextView) convertView.findViewById(R.id.label);
convertView.setTag(holder);
}
if(holder == null)
{
holder = (ViewHolder) convertView.getTag();
}
holder._image.setImageResource(_data[position].getImageResourceId());
holder._label.setText(_data[position].getName());
}
static class ViewHolder
{
TextView _label;
ImageView _image;
}
}
* 참고 : 그래서 당신이 약간 조정할 필요가 수도를 테스트하지 않았습니다. 또한
는 최적화로 나는 당신의 list.xml를 변경 제안 : 당신이 "setCompoundDrawable"또는 "setCompoundDrawablesWithIntrinsicBounds"를 사용하고 정말 레이아웃을 최적화 할 수있는 텍스트 뷰와
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/label"
android:textSize="16sp" />
합니다. 복합 드로어 블을 사용하면 이미지를 텍스트보기의 왼쪽 또는 오른쪽 또는 위 또는 아래에 배치 할 수 있습니다. 추가 구현에 대한 자세한 내용은 API 문서를 살펴 보시기 바랍니다.
희망 하시겠습니까?
carefacerz라고 생각하면 ArrayAdapter를 확장 한 사용자 정의 클래스를 사용하고 getView 메소드에서 다른 행에 다른 이미지를 지정할 수 있습니다. –
귀하의 질문과 관련이 없지만 치수를 정의 할 때 픽셀을 사용하지 않는 것이 좋습니다. 화면 밀도가 다른 여러 장치에서보기가 잘 조정되지 않습니다. 그런 이유로 항상 텍스트에 sp를 사용하고 다른 모든 것에는 dip을 사용하십시오. – jagsaund
ok @jagsaund 내가 SP를 사용해야하는 이유는 무엇입니까? DP? – carefacerz