调用 onCreate() 时 ListView 不显示任何项目
ListView doesn't show any Items when onCreate() is called
ListView 在调用 onCreate() 时不显示任何项目,但在我锁定和解锁屏幕以及 onResume() 被调用。
"Title" 是一个异步任务
谁能帮帮我
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;
}
}
"Title" is an Async Task
这意味着在 UI 线程上执行 updateUI();
时它很可能仍然很忙。你必须确保
adapter.notifyDataSetChanged();
不会太早执行,所以把语句放在AsyncTask
.
的onPostExecute()
方法里面
有可能在 new Title().execute() 之前调用了 updateUI()
returns 有数据吗?因此更新 UI 没有数据也没有显示任何东西??
ListView 在调用 onCreate() 时不显示任何项目,但在我锁定和解锁屏幕以及 onResume() 被调用。
"Title" 是一个异步任务
谁能帮帮我
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;
}
}
"Title" is an Async Task
这意味着在 UI 线程上执行 updateUI();
时它很可能仍然很忙。你必须确保
adapter.notifyDataSetChanged();
不会太早执行,所以把语句放在AsyncTask
.
onPostExecute()
方法里面
有可能在 new Title().execute() 之前调用了 updateUI() returns 有数据吗?因此更新 UI 没有数据也没有显示任何东西??