aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/camilstaps/rushhour/FinishedActivity.java
blob: a22ca80ababfb5aea2c8c76b5a56e6ec47ac97e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.camilstaps.rushhour;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.camilstaps.rushhour.R;

import java.util.ArrayList;

public class FinishedActivity extends ActionBarActivity {

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finished);

        context = this;

        final HighScoreList list = new HighScoreList(this);

        final int score = getIntent().getIntExtra("score", -1);
        if (score != -1) {
            final EditText input = new EditText(this);
            new AlertDialog.Builder(this)
                    .setTitle("Congratulations!")
                    .setMessage("Enter name:")
                    .setView(input)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Editable value = input.getText();
                            list.addToList(new HighScore(score, value.toString()));
                            list.save(context);
                        }
                    })
                    .show();
        }

        ListView highscoresListView = (ListView) findViewById(R.id.highscoresListView);

        HighScoreAdapter arrayAdapter = new HighScoreAdapter(this, R.layout.highscore_item, list.getList());

        highscoresListView.setAdapter(arrayAdapter);
    }

    @Override
    public void onBackPressed() {
        // See http://stackoverflow.com/a/13483049/1544337
        Intent intent = new Intent();
        if (getParent() == null) {
            setResult(Activity.RESULT_OK, intent);
        } else {
            getParent().setResult(Activity.RESULT_OK, intent);
        }
        finish();
    }

    private class HighScoreAdapter extends ArrayAdapter<HighScore> {
        Context context;
        int layoutResourceId;
        ArrayList<HighScore> data;

        public HighScoreAdapter(Context context, int layoutResourceId, ArrayList<HighScore> data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            HighScoreHolder holder;

            if(row == null) {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new HighScoreHolder();
                holder.score = (TextView)row.findViewById(R.id.score);
                holder.name = (TextView)row.findViewById(R.id.name);

                row.setTag(holder);
            } else {
                holder = (HighScoreHolder)row.getTag();
            }

            HighScore highscore = data.get(position);
            holder.name.setText(highscore.getName());
            holder.score.setText(Integer.toString(highscore.getScore()));

            return row;
        }

        private class HighScoreHolder {
            TextView score, name;
        }
    }

}