Android Open Source - Android.Parse View Friends Fragment






From Project

Back to project page Android.Parse.

License

The source code is released under:

MIT License

If you think the Android project Android.Parse listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iamnbty.android.parse.fragment;
/* ww w .  j a  v a  2  s  . c om*/
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iamnbty.android.parse.R;
import com.iamnbty.android.parse.parse.FriendParse;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.List;

public class ViewFriendsFragment extends Fragment {

    private ListView mListView;
    private FriendListAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_fragment_view_friends, container, false);
    }

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

        mListView = (ListView) view.findViewById(R.id.view_friends_listview);
        mListView.setEmptyView(view.findViewById(R.id.view_friends_empty_textview));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mAdapter = new FriendListAdapter(new ArrayList<FriendParse>());
        mListView.setAdapter(mAdapter);

        queryFriendsFromParse();
    }

    private class FriendListAdapter extends BaseAdapter {

        private int v, h;

        private ArrayList<FriendParse> friendParseEntries;

        private FriendListAdapter(ArrayList<FriendParse> friendParseEntries) {
            this.v = (int) getActivity().getResources().getDimension(R.dimen.activity_vertical_margin);
            this.h = (int) getActivity().getResources().getDimension(R.dimen.activity_horizontal_margin);
            this.friendParseEntries = friendParseEntries;
        }

        public void setFriendParseEntries(ArrayList<FriendParse> friendParseEntries) {
            this.friendParseEntries = friendParseEntries;
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            FriendParse friendParse = friendParseEntries.get(position);

            TextView textView = new TextView(getActivity());
            textView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
            textView.setPadding(v, h, v, h);
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
            textView.setCompoundDrawablePadding(h);
            textView.setText(friendParse.name + " (" + friendParse.nickName + ")");

            return textView;
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();

            if (friendParseEntries.size() == 0) {
                Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.info_no_friends), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void queryFriendsFromParse() {
        Toast.makeText(getActivity(), "Loading friends...", Toast.LENGTH_SHORT).show();

        ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery(FriendParse.PARSE_CLASS_NAME);
        parseQuery.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> parseObjects, ParseException exception) {
                if (exception == null) {
                    Log.i("Load Friends", "Success: " + parseObjects.size() + " object(s).");

                    // success, no error.

                    // create an empty collection of FriendParse
                    ArrayList<FriendParse> friendParseEntries = new ArrayList<FriendParse>();

                    // loop over Parse Objects result
                    for (ParseObject parseObject : parseObjects) {
                        // convert Parse Object into FriendParse
                        friendParseEntries.add(FriendParse.fromParseObject(parseObject));
                    }

                    // update list adapter to update list view
                    mAdapter.setFriendParseEntries(friendParseEntries);
                    mAdapter.notifyDataSetChanged();

                } else {
                    // fail, error occurred.
                    Log.e("Load Friends", "Failed: " + exception.getMessage());

                }
            }
        });
    }

}




Java Source Code List

com.iamnbty.android.parse.ApplicationTest.java
com.iamnbty.android.parse.BaseApplication.java
com.iamnbty.android.parse.activity.MainActivity.java
com.iamnbty.android.parse.fragment.AddFriendFragment.java
com.iamnbty.android.parse.fragment.ViewFriendsFragment.java
com.iamnbty.android.parse.parse.FriendParse.java