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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| public class MainActivity extends ListActivity {
// Search EditText
EditText inputSearch;
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "http://pokeapi.co/api/v1/pokedex/1";
// JSON Node names
private static final String TAG_POKEMON = "pokemon";
private static final String TAG_NAME= "name";
private static final String TAG_RESOURCE_URI = "resource_uri";
// contacts JSONArray
JSONArray pokemon = null;
// Hashmap for ListView
ArrayList<Pokemon> pokemonList = new ArrayList<Pokemon>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String cost = ((TextView) view.findViewById(R.id.recourceuri))
.getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
SingleContactActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_RESOURCE_URI, cost);
//in.putExtra(TAG_PHOTO, description);
startActivity(in);
}
});
// Calling async task to get json
new GetContacts().execute();
lv = (ListView) findViewById(android.R.id.list);
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
// MainActivity.this.contactList.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
pokemon = jsonObj.getJSONArray(TAG_POKEMON);
JSONObject pokemonobject = pokemon.getJSONObject(0);
Log.d("bert", "first pokemon: " + pokemonobject.getString(TAG_NAME));
pokemonobject = pokemon.getJSONObject(1);
Log.d("bert","second pokemon: " + pokemonobject.getString(TAG_NAME) + " met URI " + pokemonobject.getString(TAG_RESOURCE_URI) );
//Log.d("bert","first " + pokemon.);
// looping through All Contacts
for (int i = 0; i < pokemon.length() ; i++) {
JSONObject c = pokemon.getJSONObject(i);
String name = c.getString(TAG_NAME);
String resource = c.getString(TAG_RESOURCE_URI);
Log.d("bert", i + name);
// adding contact to contact list
pokemonList.add(new Pokemon(name,resource));
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
/*ListAdapter adapter = new SimpleAdapter(
MainActivity.this, pokemonList,
R.layout.list_item, new String[] { TAG_NAME, TAG_RESOURCE_URI,
}, new int[] { R.id.name,
R.id.recourceuri });*/
// setListAdapter(adapter);
}
}
} |
Partager