图像未从viewHolder的ImageView中的web加载

时间:2016-07-19 13:59:00

标签: android photoswipe tinder

我正在努力制作可以刷卡的应用程序。我使用了这个sample

当我从Drawable资源获取图像时工作正常,但是当我从web设置图像时,viewHolder.cardImage没有设置。

viewHolder.cardImage根本没有出现,虽然flingContainer的事件是在从onButton click函数调用时调用的。

在我出错的地方没有任何提示。

以下是代码:

public class MatchesFragmentPlay extends Fragment implements FlingCardListener.ActionDownInterface {

private static final String TAG = "MatchesFragmentPlay";

public static final String[] NAMES = new String[] { "Emily Rose", "Cherry", "Anderson", "Laius" };
public static final int[] AGES = new int[] {20, 22, 25, 18 };
public static final int[] RATINGS = new int[] {8, 7, 7, 6 };
public static final Integer[] IMAGES = { R.drawable.lady, R.drawable.lady2, R.drawable.lady3, R.drawable.lady4 };
public static final Integer[] LIKES = { 0, 0, 0, 0 };
public static final Integer[] DOTS = { R.id.rating_dot_one, R.id.rating_dot_two, R.id.rating_dot_three, R.id.rating_dot_four,
        R.id.rating_dot_five, R.id.rating_dot_six, R.id.rating_dot_seven, R.id.rating_dot_eight,
        R.id.rating_dot_nine, R.id.rating_dot_ten};

private CallBack networkCallback;
private static String responseObj;
private Bitmap bitmap = null;

// NEW
public static MyAppAdapter myAppAdapter;
public static ViewHolder viewHolder;
private ArrayList<UserMatchesPlay> al;
private SwipeFlingAdapterView flingContainer;

public static UserMatchesPlay obj;

public ImageView matchesPlayBackground;
public TextView matchesPlayUserName, matchesPlayUpgradeToPremium;
CircleImageView matchesPlayDisLike, matchesPlayLike;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view  = inflater.inflate(R.layout.fragment_matches_play, container, false);

    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Bundle inBundle = getActivity().getIntent().getExtras();
    String fb_id = inBundle.get("fb_id").toString();
    Log.d(TAG, "fb_id: "+fb_id);
    String name = inBundle.get("name").toString();
    String surname = inBundle.get("surname").toString();
    String imageUrl = inBundle.get("imageUrl").toString();

    flingContainer = (SwipeFlingAdapterView) view.findViewById(R.id.matches_play_frame);
    matchesPlayBackground = (ImageView)view.findViewById(R.id.matches_play_background);
    matchesPlayUserName = (TextView) view.findViewById(R.id.matches_play_user_name);
    matchesPlayUpgradeToPremium = (TextView) view.findViewById(R.id.matches_play_upgradeToPremium);
    matchesPlayDisLike = (CircleImageView)view.findViewById(R.id.matches_play_dislike);
    matchesPlayLike = (CircleImageView)view.findViewById(R.id.matches_play_like);

    String premiumText="For Rating Upgrade to Premium";
    SpannableString content = new SpannableString(premiumText);
    content.setSpan(new UnderlineSpan(), 0, premiumText.length(), 0);
    matchesPlayUpgradeToPremium.setText(content);

    matchesPlayDisLike.setImageResource(R.drawable.dislike_enable);
    matchesPlayDisLike.setBorderColor(Color.parseColor("#FFD1D5D9"));
    matchesPlayDisLike.setBorderWidth(5);
    matchesPlayLike.setImageResource(R.drawable.like_enable);
    matchesPlayLike.setBorderColor(Color.parseColor("#FFD1D5D9"));
    matchesPlayLike.setBorderWidth(5);

    matchesPlayDisLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            left();
        }
    });

    matchesPlayLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            right();
        }
    });

    al = new ArrayList<>();

    networkCallback = new CallBack() {
        @Override
        public void onProgress() {}

        @Override
        public void onResult(String result) {
            responseObj = result;
            Log.i(TAG, "responseData: "+responseObj);
            JsonParser jsonParser = new JsonParser();
            JsonElement element = jsonParser.parse(responseObj);
            JSONObject usersObj, userObj;
            JSONArray usersArray;
            try {
                usersObj = new JSONObject(element.toString());
                usersArray = usersObj.getJSONArray("users");

                for (int i=0; i<usersArray.length(); i++){

                    userObj = usersArray.getJSONObject(i);

                    String [] _images = {userObj.getString("img_path_1"), userObj.getString("img_path_2"), userObj.getString("img_path_3"), userObj.getString("img_path_4"), userObj.getString("img_path_5"), userObj.getString("img_path_6")};
                    al.add(new UserMatchesPlay(userObj.getString("uid"), userObj.getString("name"), userObj.getString("age"), userObj.getString("rate"), userObj.getString("img_path_1"), "0"));
                }
                Log.i(TAG, "arrayList (Size): "+al.size());

                myAppAdapter = new MyAppAdapter(al, getActivity());
                flingContainer.setAdapter(myAppAdapter);
                myAppAdapter.notifyDataSetChanged();
                flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
                    @Override
                    public void removeFirstObjectInAdapter() {

                    }

                    @Override
                    public void onLeftCardExit(Object dataObject) {
                        al.remove(0);
                        myAppAdapter.notifyDataSetChanged();
                        Toast.makeText(getActivity(), "Left Swipe ", Toast.LENGTH_SHORT).show();
                        //Do something on the left!
                        //You also have access to the original object.
                        //If you want to use it just cast it (String) dataObject

                    }

                    @Override
                    public void onRightCardExit(Object dataObject) {

                        al.remove(0);
                        myAppAdapter.notifyDataSetChanged();
                        Toast.makeText(getActivity(), "Right Swipe ", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAdapterAboutToEmpty(int itemsInAdapter) {

                    }

                    @Override
                    public void onScroll(float scrollProgressPercent) {

                        View view = flingContainer.getSelectedView();

                    }
                });

                flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClicked(int itemPosition, Object dataObject) {

                        View view = flingContainer.getSelectedView();
                        Toast.makeText(getActivity(), "Clicked: "+itemPosition+" "+String.valueOf(dataObject),Toast.LENGTH_SHORT).show();

                        UserMatchesPlay userMatchesPlay = obj;
                        Intent intent = new Intent(getActivity(), ProfileActivity.class);
                        intent.putExtra("user_matches_play_obj", userMatchesPlay);
                        startActivity(intent);

                        myAppAdapter.notifyDataSetChanged();
                    }
                });

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onCancel() {
            // TODO Auto-generated method stub

        }
    };

    HTTPAsyncTask asyncTask = new HTTPAsyncTask(getActivity(), networkCallback, null, null, "GET");
    asyncTask.execute(Config.GET_USERS_BY_LAT_LNG+fb_id);

}


@Override
public void onActionDownPerform() {
    Log.e("action", "bingo");
}

public void right() {
    flingContainer.getTopCardListener().selectRight();
}

public void left() {
    flingContainer.getTopCardListener().selectLeft();
}

public static class ViewHolder {

    public ImageView cardImage;
    public FrameLayout background_frame;
}

public class MyAppAdapter extends BaseAdapter {

    public List<UserMatchesPlay> parkingList;
    public Context context;

    private MyAppAdapter(List<UserMatchesPlay> apps, Context context) {
        this.parkingList = apps;
        this.context = context;
    }


    @Override
    public int getCount() {
        return parkingList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        if (rowView == null) {
            System.out.println("rootView is null");
            LayoutInflater inflater = getActivity().getLayoutInflater();
            rowView = inflater.inflate(R.layout.match_play_image_item, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.background_frame = (FrameLayout) rowView.findViewById(R.id.matches_play_item_frame);
            viewHolder.cardImage = (ImageView) rowView.findViewById(R.id.cardImage);
            rowView.setTag(viewHolder);

        } else {
            System.out.println("rootView is not null");
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String text = parkingList.get(0).getName()+" ("+parkingList.get(0).getAge()+" years)";
        System.out.println("Text: "+text);
        matchesPlayUserName.setText(text);

        Log.d(TAG, (viewHolder.cardImage == null)+ " IMG_PATH: "+Config.IMG_LINK_1+parkingList.get(position).getImage());

        new DownloadImage(viewHolder.cardImage).execute(Config.IMG_LINK_1+parkingList.get(position).getImage());

        Glide.with(getActivity()).load(Config.IMG_LINK_1+parkingList.get(position).getImage()).into(viewHolder.cardImage);

        return rowView;
    }
}}

0 个答案:

没有答案