ListView在调用onCreate()时不显示任何项目,但是当我锁定和解锁屏幕并调用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;
}
}
"标题"是一个异步任务
这意味着在 UI 线程上执行updateUI();
时,它很可能仍然繁忙。你必须确保
adapter.notifyDataSetChanged();
不会太早执行,所以把语句放在AsyncTask
的onPostExecute()
方法里。
updateUI() 有可能在 new Title().execute() 之前调用返回任何数据?因此更新了没有数据且不显示任何内容的 UI?