我將onItemClickListener添加到自定義列表視圖。但是這個onItemClickListener只有在我滾動了列表視圖後才起作用。Android - Listview僅在滾動後才起作用
PostListAdapter adapter = new PostListAdapter(getActivity(), bps);
listView = (ListView) v.findViewById(R.id.board_listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(),"List Item Clicked:" + position, Toast.LENGTH_LONG).show();
}
});
自定義行的xml僅包含視圖,textViews和imageView。 我在做什麼錯?
編輯: PostListAdapter:
Context context;
BoardPostStruct bps[];
public PostListAdapter(Context context, BoardPostStruct[] bps){
this.context=context;
this.bps=bps;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return bps.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return bps[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
PostHolder holder = null;
if(convertView==null){
LayoutInflater inflater = ((FragmentActivity)context).getLayoutInflater();
row = inflater.inflate(R.layout.club_board_post, parent, false);
holder = new PostHolder();
holder.userImage = (ImageView)row.findViewById(R.id.club_board_post_user_image);
holder.userName = (TextView)row.findViewById(R.id.club_board_post_userName);
holder.postTime= (TextView)row.findViewById(R.id.club_board_post_postTime);
holder.postContent= (TextView)row.findViewById(R.id.club_board_post_textpost);
holder.commentCount=(TextView)row.findViewById(R.id.club_board_post_comment_count);
row.setTag(holder);
}else{
holder = (PostHolder)row.getTag();
}
BoardPostStruct data = bps[position];
holder.userImage = data.userImage;
holder.userName.setText(data.userName);
holder.postContent.setText(data.postContent);
holder.postTime.setText(data.postTime);
if(!data.commentCount.isEmpty()){
holder.commentCount.setText(data.commentCount);
holder.commentCount.setVisibility(View.VISIBLE);
}else{
holder.commentCount.setVisibility(View.GONE);
}
return row;
}
static class PostHolder{
ImageView userImage;
//TextView userName;
TextView postTime;
TextView postContent;
TextView commentCount;
TextView userName;
}
你如何填充列表視圖? – 2014-11-24 18:03:47