1

我用ListActivity那个SpecialAdapter显示一些日志信息。我不使用后台线程,但滚动列表时出现错误:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class ru.FoxGSM.ui.DebugActivity$SpecialAdapter)] 

请帮我。

FoxLog是获取日志信息的静态类,可能来自另一个线程。但在列表中需要显示FoxLog快照)

public class DebugActivity extends ListActivity {

private class SpecialAdapter extends BaseAdapter    {
    private LayoutInflater mInflater;

    public SpecialAdapter(Context context)  { 
        super();
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);
    }

    public int getCount()   {
        return  FoxLog.count();
    }

    public long getItemId(int index)    {
        return index;
    }

    public Object getItem(int index)    {
        return  FoxLog.get(FoxLog.count()-index-1);
    }

    // Get the type of View
    public View getView(int position, View convertView, ViewGroup parent)   {
        TextView text;

        if (convertView == null)    {
            convertView = mInflater.inflate(R.layout.debug_list, null);
            text = (TextView)convertView.findViewById(R.id.text);
            convertView.setTag(text);
        }   else
            text = (TextView) convertView.getTag();

        String s = (String) getItem(position);
        if (s==null)
            return convertView; 

        text.setText(s);

        boolean isError = false;
        if (s!=null && s.length()>0)    {
            String prefix = s.substring(0, 1);
            if (prefix.equals("E") || prefix.equals("W"))
                isError = true;
        }
        if (isError)
            text.setBackgroundResource(R.color.red);
        else
            text.setBackgroundDrawable(null);
        return convertView;    

    }
}

private void RefreshData()  {
    FoxLog.ReInit(this);        
    SpecialAdapter adapter = (SpecialAdapter)this.getListAdapter();
    adapter.notifyDataSetChanged();
}

@Override
public void onCreate(Bundle savedInstanceState) {           
    super.onCreate(savedInstanceState);
    setContentView(R.layout.debug);

    FoxLog.ReInit(this);        
    SpecialAdapter adapter = new SpecialAdapter(this);                
    setListAdapter(adapter);        

    Button mExit = (Button)findViewById(R.id.exit);
    mExit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            finish();
        }          
    });

  }

}
4

0 回答 0