编辑:解决...主要。调用LOD.g(...)在getView()中仍然没有出现在LogCat中,但似乎一切正常。下面的代码已更新为工作版本。Android 2.3自定义ArrayAdapter getView方法不被称为
我一个ArrayAdapter类:
public class PostAdapter extends ArrayAdapter<Post> {
//fields
private LayoutInflater inflater;
private ArrayList<Post> posts;
//constructor
public PostAdapter(Context context, ArrayList<Post> posts) {
super(context, R.layout.post_layout, R.id.title, posts);
this.inflater = LayoutInflater.from(context);
this.posts = posts;
}
//methods
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//get data item for this position
Post post = (Post)getItem(position);
//check if existing view is being reused, otherwise inflate the view
if(convertView == null) {
convertView = inflater.inflate(R.layout.post_layout, parent, false);
}
//Populate views with data
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(post.getTitle());
TextView subTitle = (TextView) convertView.findViewById(R.id.subTitle);
subTitle.setText(post.getDescription());
//return completed view to render on screen
return convertView;
}
@Override
public int getCount() {
return posts.size();
}
}
和代码,我创建一个ArrayAdapter:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
//Initializing feed variables
posts = new ArrayList<>();
postAdapter = new PostAdapter(this, posts);
list = (ListView) findViewById(R.id.list);
//attaching Adapter to ListView
list.setAdapter(postAdapter);
DatabaseReference p = database.getReference("posts");
p.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//get reference to post
HashMap<Object, Object> entry = (HashMap<Object, Object>) dataSnapshot.getValue();
//convert to Post.class datatype
Post post = new Post();
if (entry.get("category") != null) { post.setCategory(entry.get("category").toString()); }
if (entry.get("title") != null) { post.setTitle(entry.get("title").toString()); }
if (entry.get("description") != null) { post.setDescription(entry.get("description").toString()); }
if (entry.get("hasImg") != null) { post.setImg((boolean)entry.get("hasImg")); }
if (entry.get("postKey") != null) { post.setPostKey(entry.get("postKey").toString()); }
if (entry.get("posterID") != null) { post.setPosterID(entry.get("posterID").toString()); }
if (entry.get("schoolID") != null) { post.setSchoolID(entry.get("schoolID").toString()); }
if (entry.get("flagged") != null) { post.setFlagged((long)entry.get("flagged")); }
if (entry.get("time") != null) { post.setTime((long)entry.get("time")); };
//add to ArrayList
posts.add(post);
postAdapter.notifyDataSetChanged();
Log.d("LIST", "Post Added: Posts: " + postAdapter.getCount());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我很好奇为什么每次添加子项时都要调用setAdapter()。我认为这是一个一劳永逸的事情。我还删除了对postAdapter.add(post)的调用,因为它引发了空指针异常,并且还在postAdatper中创建了重复条目。然而,真正的问题仍然没有解决 - getView()仍然没有被调用。 – Hayden
postAdapter.notifyDataSetChanged(); //这个函数用于在数据集发生变化时通知列表视图。所以不需要在childAdded监听器中调用setAdapter。 我有同样的问题,这是通过创建数据集时调用setAdapter来解决的,如果它适用于您,那么它很好。 –