Android:自定义适配器多次显示数据

时间:2014-12-22 14:41:12

标签: java android kinvey

我正在使用kinvey构建一个测试应用程序作为后端。我从后端下载kinvey实体并将它们添加到我的适配器。适配器行为错误,第一个数据实体被访问三次。

适配器的代码在

之下
public class ImageAdapter extends BaseAdapter {
private static final String TAG = ImageAdapter.class.getSimpleName();
private static ImageFetcher mImageFetcher;
private ArrayList<ImageEntity> mImageEntities;
private Context mContext;
private LayoutInflater mLayoutInflater;

public ImageAdapter(Context context, ArrayList<ImageEntity> imageEntities) {
    mContext = context;
    mImageEntities = imageEntities;
    mLayoutInflater = LayoutInflater.from(context);
    mImageFetcher = new ImageFetcher(context);
}

@Override
public int getCount() {
    return mImageEntities.size();
}

@Override
public Object getItem(int position) {
    return mImageEntities.get(position);
}

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


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

    ViewHolder viewHolder = null;

    if (convertView != null) {
        viewHolder = (ViewHolder) convertView.getTag();
    } else {
        convertView = mLayoutInflater.inflate(R.layout.grid_item_layout, parent, false);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    }
    ImageEntity imageEntity = mImageEntities.get(position);
    Log.d(TAG, imageEntity.getId());
    viewHolder.textView.setText(imageEntity.getId());
    viewHolder.imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
    mImageFetcher.loadImage(viewHolder.imageView, imageEntity.getId());
    return convertView;
}


static class ViewHolder {
    ImageView imageView;
    TextView textView;

    public ViewHolder(View v) {
        imageView = (ImageView) v.findViewById(R.id.id_simple_image);
        textView = (TextView) v.findViewById(R.id.textview);

    }

}

}

活动的代码是

public class ImageActivity extends ActionBarActivity {
private static final int CAPTURE_IMAGE_REQUEST_CODE = 11;
private static final int MEDiA_TYPE_IMAGE = 1;
private static final int PICK_IMAGE = 2;
private static final String TAG = ImageActivity.class.getSimpleName();

private Client mKinveyClient;
private EntitySource mEntitySource;
private Uri mFileUri;
private GridView mGridView;
private ImageAdapter mImageAdapter;
private ArrayList<ImageEntity> mImageEntities;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
    mEntitySource = EntitySource.getEntitySource(this);
    mKinveyClient = TestApplication.getClientInstance();
    mGridView = (GridView) findViewById(R.id.gridview);
    mImageEntities = new ArrayList<>();
    if (!mKinveyClient.user().isUserLoggedIn()) {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
    mImageAdapter = new ImageAdapter(this, mImageEntities);
    mGridView.setAdapter(mImageAdapter);
}

private void updateData() {

    AsyncAppData<ImageEntity> events = mKinveyClient.appData("events", ImageEntity.class);
    events.get(new KinveyListCallback<ImageEntity>() {
        @Override
        public void onSuccess(ImageEntity[] imageEntities) {

            mImageEntities.addAll(Arrays.asList(imageEntities));
            for (ImageEntity imageEntity : imageEntities) {
                System.out.println(imageEntity.getId());
            }
            mImageAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFailure(Throwable throwable) {
            Log.d(TAG, "Fail " + throwable.getMessage());
        }
    });

}

日志:显示三个实体已下载但在适配器中第一个实体被访问三次。请帮助

12-22 18:43:39.742: V/Kinvey - Client(16995): adding new instance of AppData, new collection name
12-22 18:43:40.628: I/System.out(16995): 5497cdc4b76664a0030358a2
12-22 18:43:40.628: I/System.out(16995): 5497cedfe21a6f05030016b8
12-22 18:43:40.629: I/System.out(16995): 5498062bc0f22386510111b6
12-22 18:43:40.635: D/ImageAdapter(16995): 5497cdc4b76664a0030358a2
12-22 18:43:40.640: D/ImageAdapter(16995): 5497cdc4b76664a0030358a2
12-22 18:43:40.641: D/ImageAdapter(16995): 5497cdc4b76664a0030358a2
12-22 18:43:40.643: D/ImageAdapter(16995): 5497cedfe21a6f05030016b8

1 个答案:

答案 0 :(得分:1)

每次在updateData中返回结果时,都会添加到数组中。您需要每次都重置集合,因此请先调用清除:

        mImageEntities.clear();
        mImageEntities.addAll(Arrays.asList(imageEntities));