Search code examples
javaandroidlistviewoncreateonresume

ListView doesn't show any Items when onCreate() is called


ListView doesn't show any Items when onCreate() is called, but it does, when I lock and unlock the screen and onResume() is called.

"Title" is an Async Task

Can anybody help me

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

This is my Custom Adapter

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

}


Solution

  • "Title" is an Async Task

    This means it will most likely still be busy when updateUI(); is executed on the UI Thread. You have to make sure that

    adapter.notifyDataSetChanged();
    

    is not executed too early, so put the statement inside the onPostExecute() method of the AsyncTask.