Last active
February 22, 2016 15:46
-
-
Save panchicore/4476d16c55997c3a7a3f to your computer and use it in GitHub Desktop.
Android for [smart] dummies (complete project here: https://gitlab.com/panchicore/android-introduction)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10. blank activity | |
20. hello world | |
change hello world | |
saludations | |
30. menu | |
create menu layout | |
override onCreateOptionsMenu y onOptionsItemSelected | |
40. open activity con Intent | |
50. application class | |
60. models | |
70. create the API (https://sheetsu.com/) | |
80. call the API with Volley (gradle ... https://github.com/mcxiaoke/android-volley) | |
¿porque volley? http://developer.android.com/training/volley/index.html | |
¿porque async? http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html | |
81. android permissions (INTERNET permission) <uses-permission android:name="android.permission.INTERNET"/> | |
82. mapping to models | |
90. layouts (lists and list items) | |
100. list view adapters and bindings | |
110. set event listerns on list views (OnItemClickListener, OnItemLongClickListener) | |
120. passing data to intents | |
130. refactor | |
140. delete from API and Adapter | |
---- | |
challenges here: | |
1000. morbo lifecycle (http://i.stack.imgur.com/2CP6n.png) | |
---- | |
challenges home: | |
2000. move delete to detail view | |
2010. add activity (POST) | |
2020. edit activity (PUT) | |
2010. passing mapped object to intents with Parcelable, not just strings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.panchicore.morosos; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import android.widget.ProgressBar; | |
import android.widget.Toast; | |
import com.android.volley.Request; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.JsonArrayRequest; | |
import com.android.volley.toolbox.StringRequest; | |
import com.panchicore.morosos.adapter.DebtorsListAdapter; | |
import com.panchicore.morosos.model.Debtor; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity { | |
private ListView mDebtorsListView; | |
private ArrayList<Debtor> mDebtorsList; | |
private DebtorsListAdapter mDebtorsListAdapter; | |
private ProgressBar loadingProgressBar; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// prepare objects | |
mDebtorsList = new ArrayList<>(); | |
mDebtorsListAdapter = new DebtorsListAdapter(getApplicationContext(), R.id.debtorsListView, mDebtorsList); | |
// prepare UI | |
mDebtorsListView = (ListView) findViewById(R.id.debtorsListView); | |
loadingProgressBar = (ProgressBar) findViewById(R.id.loadingProgressBar); | |
mDebtorsListView.setAdapter(mDebtorsListAdapter); | |
// do API calls | |
requestAllDebtors(); | |
// prepare listeners | |
mDebtorsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Debtor d = mDebtorsList.get(position); | |
Intent i = new Intent(getApplicationContext(), DebtorActivity.class); | |
i.putExtra("NAME", d.getName()); | |
startActivity(i); | |
} | |
}); | |
mDebtorsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { | |
@Override | |
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { | |
Debtor d = mDebtorsList.get(position); | |
mDebtorsList.remove(position); | |
mDebtorsListAdapter.remove(d); | |
deleteDebtor(d); | |
return true; | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
MenuInflater inflater = getMenuInflater(); | |
inflater.inflate(R.menu.menu, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if(item.getItemId() == R.id.action_item_add){ | |
Intent i = new Intent(getApplicationContext(), AddActivity.class); | |
i.putExtra("NAME", "LUIS PALLARES"); | |
startActivity(i); | |
} | |
if(item.getItemId() == R.id.action_item_refresh){ | |
requestAllDebtors(); | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
public void requestAllDebtors(){ | |
mDebtorsList.clear(); | |
mDebtorsListAdapter.clear(); | |
loadingProgressBar.setVisibility(View.VISIBLE); | |
final String URL = "https://sheetsu.com/apis/v1.0/00151fd1"; | |
JsonArrayRequest request = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() { | |
@Override | |
public void onResponse(JSONArray response) { | |
Log.i("RESULT", response.toString()); | |
for(int i = 0; i < response.length(); i++){ | |
try { | |
JSONObject o = (JSONObject) response.get(i); | |
if(!o.getString("id").isEmpty()){ | |
Debtor d = new Debtor(o); | |
mDebtorsList.add(d); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
mDebtorsListAdapter.notifyDataSetChanged(); | |
loadingProgressBar.setVisibility(View.GONE); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Log.e("ERROR", error.getMessage()); | |
} | |
}); | |
Application.getApplication().addToRequestQueue(request); | |
} | |
public void deleteDebtor(Debtor d){ | |
Toast.makeText(getApplicationContext(), "Borrando...", Toast.LENGTH_SHORT).show(); | |
final String URL = "https://sheetsu.com/apis/v1.0/00151fd1/id/" + d.getId(); | |
StringRequest request = new StringRequest(Request.Method.DELETE, URL, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
Toast.makeText(getApplicationContext(), "Borrado OK", Toast.LENGTH_SHORT).show(); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
} | |
}); | |
Application.getApplication().addToRequestQueue(request); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DebtorsListAdapter extends ArrayAdapter<Debtor> { | |
private Context context; | |
public DebtorsListAdapter(Context context, int resource, List<Debtor> objects) { | |
super(context, resource, objects); | |
this.context = context; | |
} | |
private class ViewHolder{ | |
TextView name; | |
TextView amount; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ViewHolder holder = null; | |
Debtor d = getItem(position); | |
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); | |
if(convertView == null){ | |
convertView = mInflater.inflate(R.layout.debtor_listitem_row, null); | |
holder = new ViewHolder(); | |
holder.name = (TextView) convertView.findViewById(R.id.nameTextView); | |
holder.amount = (TextView) convertView.findViewById(R.id.amountTextView); | |
convertView.setTag(holder); | |
}else{ | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
holder.name.setText(d.getName()); | |
holder.amount.setText(d.getAmount().toString()); | |
return convertView; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mDebtorsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Debtor d = mDebtorsList.get(position); | |
Log.d("CLICKED", d.getName()); | |
Intent i = new Intent(getApplicationContext(), DebtorDetailActivity.class); | |
i.putExtra("NAME", d.getName()); | |
i.putExtra("AMOUNT", d.getAmount()); | |
startActivity(i); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void delete(Debtor debtor){ | |
String URL = "https://sheetsu.com/apis/v1.0/fddc517a/id/" + debtor.getId(); | |
Toast.makeText(getApplicationContext(), "Deleting " + debtor.getName(), Toast.LENGTH_SHORT).show(); | |
StringRequest request = new StringRequest(Request.Method.DELETE, URL, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
Log.d("DELETE", response); | |
Toast.makeText(getApplicationContext(), "DONE", Toast.LENGTH_SHORT).show(); | |
listAllDebtors(); // reloader | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Log.d("DELETE", error.toString()); | |
} | |
}); | |
Application.getApplication().addToRequestQueue(request); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Application extends android.app.Application { | |
private static Application sInstance; | |
@Override | |
public void onCreate() { | |
sInstance = this; | |
Log.d("APP", "Iniciando App"); | |
super.onCreate(); | |
} | |
public static Application getApplication() { | |
return sInstance; | |
} | |
public void sayHello(){ | |
Log.d("APP", "Hola!!"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private RequestQueue mRequestQueue; | |
/** | |
* @return The Volley Request queue, the queue will be created if it is null | |
*/ | |
public RequestQueue getRequestQueue() { | |
// lazy initialize the request queue, the queue instance will be | |
// created when it is accessed for the first time | |
if (mRequestQueue == null) { | |
mRequestQueue = Volley.newRequestQueue(getApplicationContext()); | |
} | |
return mRequestQueue; | |
} | |
/** | |
* Adds the specified request to the global queue, if tag is specified | |
* then it is used else Default TAG is used. | |
*/ | |
public <T> void addToRequestQueue(Request<T> req, String tag) { | |
// set the default tag if tag is empty | |
req.setTag(TextUtils.isEmpty(tag) ? "Volley" : tag); | |
VolleyLog.d("Adding request to queue: %s", req.getUrl()); | |
getRequestQueue().add(req); | |
} | |
/** | |
* Adds the specified request to the global queue using the Default TAG. | |
*/ | |
public <T> void addToRequestQueue(Request<T> req) { | |
// set the default tag if tag is empty | |
req.setTag("Volley"); | |
getRequestQueue().add(req); | |
} | |
/** | |
* Cancels all pending requests by the specified TAG, it is important | |
* to specify a TAG so that the pending/ongoing requests can be cancelled. | |
*/ | |
public void cancelPendingRequests(Object tag) { | |
if (mRequestQueue != null) { | |
mRequestQueue.cancelAll(tag); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GET | |
final String URL = "/volley/resource/12"; | |
// pass second argument as "null" for GET requests | |
JsonObjectRequest req = new JsonObjectRequest(URL, null, | |
new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject response) { | |
try { | |
VolleyLog.v("Response:%n %s", response.toString(4)); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
VolleyLog.e("Error: ", error.getMessage()); | |
} | |
}); | |
// POST | |
final String URL = "/volley/resource/12"; | |
// Post params to be sent to the server | |
HashMap<String, String> params = new HashMap<String, String>(); | |
params.put("token", "AbCdEfGh123456"); | |
JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params), | |
new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject response) { | |
try { | |
VolleyLog.v("Response:%n %s", response.toString(4)); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
VolleyLog.e("Error: ", error.getMessage()); | |
} | |
}); | |
// GET LIST | |
final String URL = "/volley/resource/all?count=20"; | |
JsonArrayRequest req = new JsonArrayRequest(URL, new Response.Listener<JSONArray> () { | |
@Override | |
public void onResponse(JSONArray response) { | |
try { | |
VolleyLog.v("Response:%n %s", response.toString(4)); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
VolleyLog.e("Error: ", error.getMessage()); | |
} | |
}); | |
// add the request object to the queue to be executed | |
Application.getInstance().addToRequestQueue(req); | |
// SEND HEADERS | |
JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params), | |
new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject response) { | |
// handle response | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
// handle error | |
} | |
}) { | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
HashMap<String, String> headers = new HashMap<String, String>(); | |
headers.put("CUSTOM_HEADER", "Yahoo"); | |
headers.put("ANOTHER_CUSTOM_HEADER", "Google"); | |
return headers; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MODEL | |
public Debtor(JSONObject response) throws JSONException { | |
setId(response.getString("id")); | |
setName(response.getString("name")); | |
setAmount(Integer.parseInt(response.getString("amount"))); | |
setSendMoto(response.getBoolean("sendMoto")); | |
} | |
// REQUEST | |
mDebtorsList = new ArrayList<>(); | |
String URL = "https://sheetsu.com/apis/v1.0/fddc517a"; | |
JsonArrayRequest request = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() { | |
@Override | |
public void onResponse(JSONArray response) { | |
Log.d("REQ", response.toString()); | |
for(int i = 0; i<response.length(); i++){ | |
try { | |
JSONObject o = (JSONObject) response.get(i); | |
Debtor d = new Debtor(o); | |
Log.d("DEBT", d.getName()); | |
Log.d("DEBT", "" + d.isSendMoto()); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Log.e("REQ", error.toString()); | |
} | |
}); | |
Application.getApplication().addToRequestQueue(request); |
Author
panchicore
commented
Feb 19, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment