简体   繁体   English

ImageView 不会每 x 秒更新一次

[英]ImageView doesn't update every x seconds

I have a ImageView which will be updated every "x" seconds, obtained from a server.我有一个 ImageView,它将每“x”秒更新一次,从服务器获取。

The first updating the image changes correctly.第一次更新图像正确更改。

But in the next image has not updated anymore.但在下一个图像已不再更新。

Does the first image is stored in memory, I would like to remove?第一张图片是否存储在内存中,我想删除?

code:代码:

 public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
        if(convertView ==null)
        {
            convertView = mInflater.inflate(R.layout.onlinelist,parent,false);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.textView1);
            holderProfile = (ImageView) convertView.findViewById(R.id.usernameProfile);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(usersInRange.get(position));
    String[] split = usersInRange.get(position).split(" ");
    final String firstSubString = split[0];
    AsyncHttpClient client = new AsyncHttpClient();
    client.get("xxxxxxxxx.com/uploads/" + firstSubString + ".jpeg", null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    final Bitmap image = BitmapFactory.decodeByteArray(responseBody, 0, responseBody.length);
                    holderProfile.setImageBitmap(image);
                    holderProfile.invalidate();


            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }

    });

    return convertView;
}

You are doing the Update on a Thread with Background Priority....您正在对具有后台优先级的线程进行更新....

You will have to do run it on main Thread您必须在主线程上运行它

Change this line:改变这一行:

holderProfile.setImageBitmap(image);

To this:对此:

runOnUiThread(new Runnable() {
      @Override public void run() {
            holderProfile.setImageBitmap(image);
      }
});

设置图像后尝试使 imageView 失效()。

Use notifyDataSetChanged();使用 notifyDataSetChanged(); After updating ur image in adapter.在适配器中更新您的图像后。

Remove holderProfile.invalidate();删除holderProfile.invalidate(); from client.get(...) and put before theclient.get(...)并放在

AsyncHttpClient client = new AsyncHttpClient();

or you can try this one ImageView not refreshing/reflecting changes或者你可以试试这个ImageView 不刷新/反映变化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM