Back to project page runescape-highscore.
The source code is released under:
Apache License
If you think the Android project runescape-highscore listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.rsstat.ui; //ww w . j a v a 2 s . c om import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.preference.PreferenceManager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.rsstat.Constants; import com.rsstat.R; import com.rsstat.models.Triple; import com.rsstat.rest.api.RSHighScoreAPI; import com.rsstat.ui.adapters.SearchHistoryListAdapter; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import retrofit.Callback; import retrofit.RestAdapter; import retrofit.RetrofitError; import retrofit.client.Response; import retrofit.converter.ConversionException; import retrofit.converter.Converter; import retrofit.mime.TypedInput; import retrofit.mime.TypedOutput; public class MainActivity extends ActionBarActivity { private SharedPreferences preferences; private RSHighScoreAPI api; private LinkedList<String> history; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Constants.ENDPOINT).setConverter(new TripleConverter()).build(); api = restAdapter.create(RSHighScoreAPI.class); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getSupportActionBar().setElevation(0f); } // restore previous user searches preferences = PreferenceManager.getDefaultSharedPreferences(this); history = getSearchHistory(); if (history == null) { history = new LinkedList<>(); history.add("GL RAMBO GF"); updateSearchHistory(history); } SearchHistoryListAdapter searchHistoryListAdapter = new SearchHistoryListAdapter(this, history); ListView searchList = (ListView) findViewById(R.id.listview); searchList.setAdapter(searchHistoryListAdapter); searchList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { search(history.get(position)); } }); Button searchButton = (Button) findViewById(R.id.search_button); final EditText searchText = (EditText) findViewById(R.id.search_edit_text); searchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final Editable search = searchText.getText(); if (search != null) { search(search.toString()); } } }); } public void search(final String username) { api.getHighScore(username, new Callback<ArrayList<Triple>>() { @Override public void success(ArrayList<Triple> result, Response response) { Intent intent = new Intent(MainActivity.this, ResultsActivity.class); intent.putParcelableArrayListExtra(Constants.DATA_TAG, result); intent.putExtra(Constants.NAME_TAG, username); if (!history.contains(username)){ history.add(username); updateSearchHistory(history); } startActivity(intent); } @Override public void failure(RetrofitError e) { Toast.makeText(MainActivity.this, Constants.PLAYER_NOT_FOUND, Toast.LENGTH_SHORT).show(); } }); } private LinkedList<String> getSearchHistory() { String historyAsJson = preferences.getString(Constants.SEARCH_HISTORY, null); Type type = new TypeToken<LinkedList<String>>() { }.getType(); return new Gson().fromJson(historyAsJson, type); } private void updateSearchHistory(LinkedList<String> history) { if (history.size() > 15) { history.removeFirst(); } SharedPreferences.Editor editor = preferences.edit(); String historyAsJson = new Gson().toJson(history); editor.putString(Constants.SEARCH_HISTORY, historyAsJson); editor.apply(); } private static class TripleConverter implements Converter { @Override public Object fromBody(TypedInput typedInput, Type type) throws ConversionException { List<Triple> hsList = new ArrayList<>(); String csv = null; try { csv = new Scanner(typedInput.in()).useDelimiter("\\A").next().replace(",", " ").replace("-1", "Unranked"); } catch (IOException e) { e.printStackTrace(); } Scanner readVals = new Scanner(csv); for (int i = 0; i < 27; i++) { hsList.add(new Triple(readVals.next(), readVals.next(), readVals.next())); } return hsList; } @Override public TypedOutput toBody(Object o) { return null; } } }