清空listview黑色闪光灯

时间:2013-12-12 13:40:57

标签: android listview

我已经定义了一个ListView,从服务中获取数据,它也有一个空视图:

public class MyActivity extends Activity {

MyObject myObject;
ListView myListView;
MyAdapter myAdapter;
ArrayList<MyListElement> myElements;

Handler h = new Handler(){
   public void handleMessage (Message msg){
      if(msg.what == 0){
          init((ArrayList<MyListElement> ))
      }
   }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            setContentView(R.layout.my_layout);
    myListView = (ListView) findViewById(R.id.my_listview);

    myObject = (myObject) getIntent().getExtras().get("myObj");
    setTitle(getString(R.string.title_activity_chat) + myObject.getDisplayedName());

    myElements = new ArrayList<MyListElement>();
    LayoutInflater inflater = getLayoutInflater();
    myAdapter = new MyAdapter(this, this, R.layout.my_activity, myElements);
    myListView.setEmptyView(findViewById(R.id.text1));
    myListView.setAdapter(myAdapter);
    Intent i = new Intent(MyActivity.this, MyService.class);
            ...
    startService(i);
}

protected void init(List<MyListElement> pHistory) {
    myAdapter.addAll(pHistory);
    myAdapter.notifyDataSetChanged()
}
...
}

我有一个自己的ArrayAdapter(MyAdapter)。

我的问题是,当我将数据恢复到初始化(...)后,当我启动活动并调用服务时,UI会以黑色闪光刷新自己。

有人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:0)

我已使用此代码永久解决了问题:

myAdapter.add(first);
myListView.post(new Runnable() {
    @Override
    public void run() {
        while (myListView.getWidth() == 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        myAdapter.remove(first);
    }
});