Android:如何让listview在设置之前等待变量填充?

时间:2015-09-29 13:15:14

标签: android facebook listview android-listview parse-platform

所以我正在制作一部电影琐事游戏。我遇到的问题是在我设置3个列表视图的主用户配置文件页面上。一个用于轮到你的游戏,一个用于等待你的对手的游戏,一个用于完成的游戏。这些列表视图调用Parse.com来获取游戏对象。为了从Parse获取此信息,我需要使用Facebook用户ID提供我的查询。我的代码调用Facebook获取ID,但listview适配器运行大约5毫秒,然后调用Facebook返回ID。当用户刷新页面时,列表正确加载,但是当第一次运行应用程序时,它们是空白的。以下是用户个人资料活动代码:

public class UserProfileActivity extends AppCompatActivity {

    ParseLogic mPl;
    private WfoAdapter wfoAdapter;
    private WoyAdapter woyAdapter;
    private CompletedGameAdapter completedGameAdapter;
    private TicketSystem mTicketsSytem;



    String mMyFbId;
    String mMyFbName;

    int mTickets;

    ListView mWfoListView;
    ListView mWoyListView;
    ListView mCompletedGameListView;
    TextView mmTicketNumber;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mTicketsSytem = new TicketSystem();
        mTickets = mTicketsSytem.getTickets();

        setContentView(R.layout.activity_user_profile);
        new getFbDeets().execute(); //This is where the async task is called


        mPl = new ParseLogic();
        mWoyListView = (ListView)findViewById(R.id.waitingForYouListView);
        mWfoListView = (ListView)findViewById(R.id.waitingForOpponentListView);
        mCompletedGameListView = (ListView)findViewById(R.id.completedGameListView);
        mmTicketNumber = (TextView)findViewById(R.id.numberOfTickets);

        mmTicketNumber.setText(String.valueOf(mTickets));




        FaceBookFriends.getFaceBookFriends();



         //Testing start game remove when building rest of page
        Button button = (Button)findViewById(R.id.startNewGame);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(UserProfileActivity.this, GameStart.class);
                startActivity(intent);


            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();



        // Logs 'install' and 'app activate' App Events.
        AppEventsLogger.activateApp(this);

        mTickets = mTicketsSytem.getTickets();
        mmTicketNumber.setText(String.valueOf(mTickets));
        populateWoyListView();
        populateWfoListView();
        populateCompleteListView();


    }

    @Override
    public void onBackPressed(){}


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        //create the logout button in action bar
        switch (item.getItemId()){
            case R.id.logoutButton:
                ParseUser.logOut();
                ParseUser currentUser = ParseUser.getCurrentUser(); // this will now be null
                Intent intent = new Intent(UserProfileActivity.this, WelcomeActivity.class);
                startActivity(intent);
        }

        return super.onOptionsItemSelected(item);
    }

    public void populateUI() {
        populateWoyListView();
        populateWfoListView();
        populateCompleteListView();

        mWoyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String goTV = ((TextView) view.findViewById(R.id.gameObjectHiddenTextView)).getText().toString();
                String score = ((TextView) view.findViewById(R.id.woyScoreNumber)).getText().toString();
                Log.v("Clicked Object Id ", goTV);
                Intent intent = new Intent(UserProfileActivity.this, AreYouReady.class);
                intent.putExtra("Score", score);
                intent.putExtra("Object Id", goTV);

                startActivity(intent);
            }
        });
    }

    private void populateWoyListView() {
      //mMfbId is the variable set by the facebook call
        woyAdapter = new WoyAdapter(this ,mMyFbId);  
        woyAdapter.loadObjects();
        mWoyListView.setAdapter(woyAdapter);
    }

    private void populateWfoListView() {
        wfoAdapter = new WfoAdapter(this);
        wfoAdapter.loadObjects();
        mWfoListView.setAdapter(wfoAdapter);
    }

    private void populateCompleteListView() {
        completedGameAdapter = new CompletedGameAdapter(this, mMyFbId);
        completedGameAdapter.loadObjects();
        mCompletedGameListView.setAdapter(completedGameAdapter);
    }


    protected class getFbDeets extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void... params) {

            mMyFbId = FaceBookFriends.getMyFbId();
            mMyFbName = FaceBookFriends.getMyFbName();
            Log.v("getFbDeets() ", "Is Running");
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            populateUI();
        }
    }





}

这是已完成的适配器:

/**
 * Created by Tom Schinler on 9/23/2015.
 */
public class CompletedGameAdapter extends ParseQueryAdapter<Game> {

    String mWinnerScore;
    String mWinnerName;
    String mLoserScore;
    String mLoserName;
    String mFbName;


    public CompletedGameAdapter(Context context, final String myFbId) {
        super(context, new ParseQueryAdapter.QueryFactory<Game>() {
            public ParseQuery<Game> create() {
                ParseQuery<Game> queryCreatedBy = new ParseQuery<Game>("Game");
                queryCreatedBy.whereEqualTo("Created_By", ParseUser.getCurrentUser());

                ParseQuery<Game> queryOppOf = new ParseQuery<Game>("Game");
                queryOppOf.whereEqualTo("Opponent_Id", myFbId);

                ParseQuery<Game> query = ParseQuery.or(Arrays.asList(queryCreatedBy, queryOppOf));
                query.whereNotEqualTo("Creator_Score", "");
                query.whereNotEqualTo("Opponent_Score", "");
                query.orderByDescending("updatedAt");
                query.setLimit(4);
                return query;
            }

        });
    }


    @Override
    public View getItemView(Game game, View view, ViewGroup parent) {

        if (view == null) {
            view = View.inflate(getContext(), R.layout.completed_game_layout, null);
        }

        super.getItemView(game, view, parent);
        mFbName = FaceBookFriends.getMyFbName();

        TextView winOrLose = (TextView)view.findViewById(R.id.winOrLose);


        int oppScore = Integer.parseInt(game.getOpponentScore());
        int creatScore = Integer.parseInt(game.getCreatorScore());





        if(oppScore > creatScore){
            mWinnerScore = String.valueOf(oppScore);
            mWinnerName = game.getOpponentName();
            mLoserScore = String.valueOf(creatScore);
            mLoserName = game.getCreatorFbName();
        }
        else {
            mWinnerScore = String.valueOf(creatScore);
            mWinnerName = game.getCreatorFbName();
            mLoserScore = String.valueOf(oppScore);
            mLoserName = game.getOpponentName();
        }

        TextView winnerName = (TextView)view.findViewById(R.id.winnerName);
        winnerName.setText(mWinnerName);

        TextView winnerScore = (TextView)view.findViewById(R.id.winnerScore);
        winnerScore.setText(mWinnerScore);

        TextView loserName = (TextView)view.findViewById(R.id.loserName);
        loserName.setText(mLoserName);

        TextView loserScore = (TextView)view.findViewById(R.id.loserScore);
        loserScore.setText(mLoserScore);

        if(mWinnerName.equals(mFbName)){
            view.setBackgroundColor(Color.GREEN);
            winOrLose.setText("WIN!!");
        }
        else {
            view.setBackgroundColor(Color.RED);
            winOrLose.setText("LOSER!!");
        }


        return view;
    }


}

这是适配器,它为等待用户的游戏设置listview:

/**
 * Created by Tom Schinler on 9/22/2015.
 */
public class WoyAdapter extends ParseQueryAdapter<Game>{


    public WoyAdapter(Context context, final String fbId) {
        super(context, new ParseQueryAdapter.QueryFactory<Game>() {

            public ParseQuery<Game> create() {
                //Log.v("var fbid is ", fbId);
                ParseQuery query = new ParseQuery("Game");
                query.whereEqualTo("Opponent_Id", fbId);
                query.whereEqualTo("Opponent_Score", "");
                query.orderByDescending("updatedAt");
                query.setLimit(4);
                return query;
            }
        });
    }

    @Override
    public View getItemView(Game game, View view, ViewGroup parent) {

        if (view == null) {
            view = View.inflate(getContext(), R.layout.waiting_on_you_layout, null);
            Log.v("WOY is this", "running");
        }

        super.getItemView(game, view, parent);

        ProfilePictureView friendPic = (ProfilePictureView) view.findViewById(R.id.woyFbPic);
        String friendId = game.getCreatorId();
        if(friendId != null){
            friendPic.setProfileId(friendId);
        }

        TextView oppName = (TextView)view.findViewById(R.id.woyNameText);
        oppName.setText(game.getCreatorFbName());

        TextView oppScore = (TextView)view.findViewById(R.id.woyScoreNumber);
        oppScore.setText(game.getCreatorScore());

        TextView GOTV = (TextView)view.findViewById(R.id.gameObjectHiddenTextView);
        GOTV.setText(game.getObjectId());


        return view;
    }
}

我用这样的Facebook方法设置mMyFbId:

mMyFbId = FaceBookFriends.getMyFbId();

但是在代码运行后它返回大约1秒来构建列表视图,因为它们没有变量,它们什么都没有填充。请帮忙。

0 个答案:

没有答案