Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anthony fermin #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="nyc.c4q"
android:versionCode="2"
android:versionName="1.0.0-SNAPSHOT">
<uses-permission android:name="android.permission.INTERNET"/>

<application android:name=".Unit2AssessmentApplication">
<activity
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/nyc/c4q/ColorAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package nyc.c4q;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
* Created by c4q-anthonyf on 6/28/15.
*/
public class ColorAdapter extends BaseAdapter {

private Context context;
private String[] colors;
private int itemHeight;
private int numOfItems;

private LayoutInflater inflater;

public ColorAdapter(Context context, String[] colors, int itemHeight, int numOfItems) {
this.context = context;
this.colors = colors;
this.itemHeight = itemHeight;
this.numOfItems = numOfItems;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return numOfItems;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

String color = colors[i%colors.length];

View row = inflater.inflate(R.layout.list_item_layout, null);
TextView itemView = (TextView) row.findViewById(R.id.item_view);
itemView.setBackgroundColor(Color.parseColor(color));
itemView.setText(color);
ViewGroup.LayoutParams params= itemView.getLayoutParams();
if(itemHeight > 9){
params.height=itemHeight * 5;
}else{
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
itemView.setLayoutParams(params);

return row;
}
}
83 changes: 83 additions & 0 deletions src/main/java/nyc/c4q/JSONActivity.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package nyc.c4q;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -23,6 +34,7 @@
public class JSONActivity extends Activity {

public List<Zipcode> zipcodes;
String json;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -41,10 +53,42 @@ protected void onCreate(Bundle savedInstanceState) {
final TextView state = (TextView) findViewById(R.id.fieldstatevalue);
final TextView _lat = (TextView) findViewById(R.id.fieldloclatvalue);
final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue);
final TextView jsonViewer = (TextView) findViewById(R.id.json_viewer);

addjson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if( !(_id.getText().toString().equals("")
|| pop.getText().toString().equals("")
|| city.getText().toString().equals("")
|| state.getText().toString().equals("")
|| _lat.getText().toString().equals("")
|| _long.getText().toString().equals("")) ) {

Zipcode zipcode = new Zipcode();

zipcode.id = _id.getText().toString();
zipcode.pop = pop.getText().toString();
zipcode.city = city.getText().toString();
zipcode.state = state.getText().toString();
String[] loc = {_lat.getText().toString(),_long.getText().toString()};
zipcode.loc = loc;

_id.setText("");
pop.setText("");
city.setText("");
state.setText("");
_lat.setText("");
_long.setText("");

zipcodes.add(zipcode);
(Toast.makeText(getApplicationContext(),"Added to Zipcodes",Toast.LENGTH_LONG)).show();

}else{
(Toast.makeText(getApplicationContext(),"Fill in all fields",Toast.LENGTH_LONG)).show();
}
generateJSON();
}
});

Expand All @@ -53,6 +97,21 @@ public void onClick(View v) {
public void onClick(View v) {
File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");

FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(json);
os.close();
fos.close();
(Toast.makeText(getApplicationContext(),"Zipcodes Saved",Toast.LENGTH_LONG)).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
});

Expand All @@ -62,7 +121,31 @@ public void onClick(View v) {
public void onClick(View v) {
File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");

FileInputStream fis = null;
try{
fis = new FileInputStream(file);
ObjectInputStream is = new ObjectInputStream(fis);
json = (String) is.readObject();
is.close();
fis.close();
(Toast.makeText(getApplicationContext(),"Zipcodes Loaded",Toast.LENGTH_LONG)).show();
jsonViewer.setText(json);
}catch(FileNotFoundException e){
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}

private void generateJSON(){
json = new Gson().toJson(zipcodes);
}

}
88 changes: 88 additions & 0 deletions src/main/java/nyc/c4q/ListViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewActivity extends Activity {
Expand All @@ -19,11 +27,91 @@ public class ListViewActivity extends Activity {
"#bf538d"
};
public TextView textLog;
public ListView list;
public EditText itemHeight;
public EditText numOfItems;

public int height;
public int itemAmount;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
textLog = (TextView) findViewById(R.id.textLog);
list = (ListView) findViewById(R.id.list);

height = 0;
itemAmount = 0;

itemHeight = (EditText) findViewById(R.id.input_item_height);
numOfItems = (EditText) findViewById(R.id.input_num_items);

itemHeight.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
String text = itemHeight.getText().toString();
int defaultHeight = 0;
try {
if(text.equals("")){
height = 0;
}else {
defaultHeight = Integer.parseInt(text);
height = defaultHeight;
list.setAdapter(new ColorAdapter(getApplicationContext(), COLORS, defaultHeight, itemAmount));
}
}catch(NumberFormatException e){

}
}
@Override
public void afterTextChanged(Editable editable) {

}
});

numOfItems.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
String text = numOfItems.getText().toString();
int defaultAmount = 0;

if(text.isEmpty()){
itemAmount = 0;
list.setAdapter(new ColorAdapter(getApplicationContext(), COLORS, height, defaultAmount));
}else {
try {
defaultAmount = Integer.parseInt(text);
itemAmount = defaultAmount;
list.setAdapter(new ColorAdapter(getApplicationContext(), COLORS, height, defaultAmount));
}catch(NumberFormatException e){

}
}

}

@Override
public void afterTextChanged(Editable editable) {

}
});

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView tView = (TextView) view.findViewById(R.id.item_view);
textLog.setText("You clicked on Item(position=" + i + ", color=" + tView.getText().toString() + ")");
}
});
}
}
Loading