2016-03-21 76 views
0

ListView控件不显示任何项目时的onCreate()被调用,但它确实,当我锁定和解锁屏幕和的onResume()是调用。ListView控件不显示任何项目时的onCreate()被调用

“标题” 是一个异步任务

任何人可以帮助我

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final ListView lvView = (ListView) findViewById(R.id.resList); 

    adapter = new CustomAdapter(this, listItems); 

    lvView.setAdapter(adapter); 

    lvView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent myIntent = new Intent(view.getContext(), Detailed.class); 
      Nachricht temp = (Nachricht) lvView.getItemAtPosition(position); 
      String tempTitel = temp.titel; 
      String tempBeschreibung = temp.beschreibung; 
      String tempLink = temp.link; 

      myIntent.putExtra("Titel", tempTitel); 
      myIntent.putExtra("Beschreibung", tempBeschreibung); 
      myIntent.putExtra("Link", tempLink); 
      view.getContext().startActivity(myIntent); 
     } 
    }); 

    new Title().execute(); 

    updateUI(); 

} 

public void updateUI(){ 
    adapter.notifyDataSetChanged(); 
} 

这是我的自定义适配器

public class CustomAdapter extends ArrayAdapter<Nachricht> { 
public CustomAdapter(Context context, ArrayList<Nachricht> users) { 
    super(context, 0, users); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Get the data item for this position 
    Nachricht user = getItem(position); 
    // Check if an existing view is being reused, otherwise inflate the view 
    if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.lv_item, parent, false); 
    } 
    // Lookup view for data population 
    TextView tvName = (TextView) convertView.findViewById(R.id.lvItem_title); 
    TextView tvHome = (TextView) convertView.findViewById(R.id.lvItem_desc); 
    TextView links = (TextView) convertView.findViewById(R.id.link); 
    ImageView imgV = (ImageView) convertView.findViewById(R.id.lvItem_pic); 
    // Populate the data into the template view using the data object 
    tvName.setText(user.titel); 
    tvHome.setText(user.beschreibung); 
    links.setText(user.link); 
    imgV.setImageBitmap(user.bmp); 
    // Return the completed view to render on screen 
    return convertView; 
} 

}

+1

你的代码在哪里? –

+0

您是否在适配器内部创建了一些东西? –

+0

是我做的: 我添加自定义类的listItems中ArrayList的 就像我说的,锁定和解锁屏幕时,它的工作原理... 我没有覆盖的onResume() – thegesuser

回答

2

“标题” 是异步任务

这意味着当在UI线程上执行updateUI();时,它很可能仍然很忙。你必须确保

adapter.notifyDataSetChanged(); 

是不是太早执行,这样就把语句AsyncTaskonPostExecute()方法中。

+0

这是解决方案。谢谢;) 没想过 – thegesuser

2

有可能在new Title()。execute() 之前调用updateUI()get返回任何数据?并因此更新了没有数据的UI并且没有显示任何内容?