当调用onCreate()时,ListView不显示任何项目

时间:2016-03-21 15:53:36

标签: java android listview oncreate onresume

当调用 onCreate()时,ListView不会显示任何项目,但是当我锁定并解锁屏幕并调用 onResume()时,它会显示。

"名称"是一个异步任务

任何人都可以帮助我

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;
}

}

2 个答案:

答案 0 :(得分:2)

  

&#34;名称&#34;是一个异步任务

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

adapter.notifyDataSetChanged();

未执行得太早,因此请将语句放在onPostExecute()的{​​{1}}方法中。

答案 1 :(得分:2)

有可能在new Title()之前调用updateUI()。execute() 返回任何数据?因此更新UI没有数据,没有显示任何东西?