为什么这个方法叫做双倍时间?

时间:2015-08-19 03:08:18

标签: android

我想要的是隐藏ImageView中特定项目的ListView,因此我将此代码放到适配器的getView方法中:

for (int i = 0; i < mImageModelList.size(); i++) {
    if (((SNSPostModel) mComparableObjectList.get(position)).getAccountId().equals(mImageModelList.get(i).getSnsPostId())) {
        Log.e("PATH", mImageModelList.get(i).getPath());
        Log.e("ID", mImageModelList.get(i).getSnsPostId());
        if (mImageModelList.get(i).getPath().equals(null) || mImageModelList.get(i).getPath().equals("null")) {
                //Log.e("REMOVE", "REMOVE");
                //mPostHolder.mPostedImage.setVisibility(View.GONE);
        } else {
                mPostHolder.mPostedImage.setVisibility(View.VISIBLE);
                Bitmap bmp = BitmapFactory.decodeFile(mImageModelList.get(i).getPath());
                mPostHolder.mPostedImage.setImageBitmap(bmp);

            }
        }
    }
}

这是Logcat:

08-19 02:36:18.725: E/ID(13070): 929043207145744_951230201593711
08-19 02:36:18.729: E/PATH(13070): null
08-19 02:36:18.729: E/ID(13070): 929043207145744_951230201593711
08-19 02:36:18.737: E/PATH(13070): /mnt/sdcard/lifelog/929043207145744_951223071594424.png
08-19 02:36:18.737: E/ID(13070): 929043207145744_951223071594424
08-19 02:36:18.737: E/PATH(13070): /mnt/sdcard/lifelog/929043207145744_951223071594424.png
08-19 02:36:18.737: E/ID(13070): 929043207145744_951223071594424

正如您所看到的,我不明白为什么ImageModelList中的对象被调用了两次。请告诉我如何解决这个问题。谢谢!

2 个答案:

答案 0 :(得分:0)

方法getView()不是您想要的地方。这是Adapter占据ListView中每个位置并决定如何填充它以及是否需要为此地点生成新View的地方。

相反,你可以使用`ListView.getChaildAt(int position)来为你提供你想要的视图,或者如果你不知道你可以使用的位置:

    YourCustomAdapter yourCustomAdapter = ((YourCustomAdapter)listView.getAdapter());
    for (int i = 0; i < listView.getChildCount(); i++){
        View v = listView.getChildAt(i);
        //put your logic check here, use the yourCustomAdapter reference to read it data like mImageModelList. You need that all data will define as public 
    }

答案 1 :(得分:0)

这是你在getView方法中必须做的。 你永远不会在getView()中运行for循环。

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        mPostHolder= new ViewHolder();
        view = inflater.inflate(R.layout.snapshort_item_view, null);
        // Locate the ImageView in listview_item.xml
        mPostHolder.mPostedImage= (ImageView) view
                .findViewById(R.id.snapshortImage);
        view.setTag(mPostHolder);
    } else {
        mPostHolder= (ViewHolder) view.getTag();
    }
     // Here you need to check your condition for hiding the imageview or not. Replace "i" with "position" which is a parameter fot getView() method.
    if (mImageModelList.get(position).getPath().equals(null)
            || mImageModelList.get(position).getPath().equals("null")) {
        // Log.e("REMOVE", "REMOVE");
        // mPostHolder.mPostedImage.setVisibility(View.GONE);
    } else {
        mPostHolder.mPostedImage.setVisibility(View.VISIBLE);
        Bitmap bmp = BitmapFactory.decodeFile(mImageModelList.get(i)
                .getPath());
        mPostHolder.mPostedImage.setImageBitmap(bmp);

    }

    return view;
}
相关问题