2014-11-24 52 views
2

我將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; 
} 
+0

你如何填充列表視圖? – 2014-11-24 18:03:47

回答

0

我已經看到了有關getLayoutInflater一些問題......所以你getView方法我建議你改變你的LayoutInflater建設:

//(...) 
if(convertView==null){ 
     LayoutInflater inflater = LayoutInflater.from(context); 
     row = inflater.inflate(R.layout.club_board_post, parent, false); 
//(...) 

希望對你有所幫助。

+0

感謝您的回答。不幸的是它不起作用。 – mkersche17 2014-11-24 19:05:39

+0

你是什麼意思「不起作用」?應用程序崩潰還是像以前一樣? – PedroHawk 2014-11-25 09:18:22

+0

對於遲到的回覆感到抱歉。我和以前一樣,但是我發現它可以在真實設備上工作,只有在仿真器上它不起作用。但我只嘗試過一種設備,所以我不能說它是否適用於所有設備。 – mkersche17 2014-11-30 16:16:08

相關問題