カスタム ListView アイテムを含む ListView があります。
写真 --> http://imageshack.us/photo/my-images/155/08pictureslistcopy.png/
リストビューに正しい画像を表示するには、リストビューの正しい順序でpicture_IDを持つArrayListを持っています。
BaseAdapter を拡張する AttachmentAdapter というクラスも作成しました。getView メソッドで、ArrayList から正しい picture_Id を取得し、アイテムを表示します。
ArrayListからpicture_IDを取得するプロセスをデバッグしましたが、これは正しく機能します:-)
私の問題は、リストビューに 6 つ以上のエントリがあると、getView メソッドがアイテムの間違った位置を表示し始めることです。そのため、リストの間違った位置に間違った画像が表示されます。
BaseAdapters getView メソッドが偽の位置を渡す理由を誰か教えてもらえますか?
BaseAdapter の私のコードは次のとおりです。
private class AttachmentAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<Data_Attachment> mData = new ArrayList<Data_Attachment>();
public AttachmentAdapter(Context fragment) {
// super(fragment, textViewResourceId, items);
mInflater = LayoutInflater.from(fragment);
}
public void setData(ArrayList<Data_Attachment> data)
{
mData = data;
}
private Data_Attachment getAttachmentfromPosition(int pos)
{
return DataResources.getAttachment(position_list.get(pos));
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Data_Attachment data = getAttachmentfromPosition(position);
ViewHolder holder = null;
Log.e("TEST","getView " + position + ", Attachment: #" + data.getID() + " --> " + data.getBriefDescr());
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listpictures_item, null);
holder = new ViewHolder();
holder.name= (TextView) convertView.findViewById(R.id.idPictureName);
holder.date= (TextView) convertView.findViewById(R.id.idPictureDate);
holder.uploader= (TextView) convertView.findViewById(R.id.idPictureUploader);
holder.icon = (ImageView) convertView.findViewById(R.id.idListPicture_Preview);
try{
Bitmap thumbnail = data.getThumbnail(getApplicationContext());
if(thumbnail != null)
{
holder.icon.setImageBitmap(thumbnail);
}
else
{
holder.icon.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.ic_notavailable));
}
}
catch(Exception e)
{
Log.e("TEST","PicturesIncident_Fragment.AttachmentData.getView, Error while showing picture: " + e.toString());
holder.icon.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.ic_notavailable));
}
String name = data.getBriefDescr();
if(name.length() == 0)
{
holder.name.setVisibility(View.GONE);
holder.uploader.setTextSize(14);
holder.uploader.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
//Kürzen nach 40 Strings
if(name .length() >= _Settings.MAX_ATTACHMENT_NAME_SIZE)
{
char[] buf = new char[_Settings.MAX_ATTACHMENT_NAME_SIZE];
name.getChars(0, _Settings.MAX_ATTACHMENT_NAME_SIZE-1, buf, 0);
name = String.valueOf(buf) +"...";
}
holder.name.setText(name);
}
//Set uploader and Date
Data_Worker data_worker = DataResources.getWorkerData_fromID(data.getuploaderID());
String uploader = data_worker.getName();
String date = data.getModified_str();
holder.uploader.setText(uploader);
//TODO change to view the date
// holder.date.setText(date);
holder.date.setText("pos: "+ position + " , #" + data.getID());
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
class ViewHolder {
TextView name;
TextView uploader;
TextView date;
ImageView icon;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Data_Attachment getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
テスト目的で、コードと下のスクリーンショットでわかるように、リストビューでのこのアイテムの現在の位置を表示しました。
写真 --> http://imageshack.us/photo/my-images/805/device20121010155051.png/
ここでは、位置 0 がリストの一番上にないことがわかります。
さらに、少し上下にスクロールすると位置が変わりますが、位置の変化の背後にあるロジックがわかりません。
PS: デバイスでのみテスト済み (Samsung Galaxy S2 - Android 4.0.4)