Skip to content

Instantly share code, notes, and snippets.

@bpascazio
Last active December 17, 2015 09:38
Show Gist options
  • Save bpascazio/5588260 to your computer and use it in GitHub Desktop.
Save bpascazio/5588260 to your computer and use it in GitHub Desktop.
pace zoo example
activity_edit_animal.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCCCCC"
android:orientation="vertical"
android:paddingTop="1dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:text="Edit Animal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:padding="10dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_marginTop="30dp"
android:text="Animal" />
<EditText
android:id="@+id/animalName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:ems="10" />
<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/animalName"
android:layout_below="@+id/animalName"
android:layout_marginTop="32dp"
android:text="Update"
android:onClick="updateAnimal" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnadd"
android:layout_marginLeft="26dp"
android:layout_toRightOf="@+id/btnadd"
android:text="Delete"
android:onClick="removeAnimal" />
</RelativeLayout>
</LinearLayout>
EditAnimal.java
---------------
package com.bytefly.zooexample;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
public class EditAnimal extends Activity {
DBController controller = new DBController(this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_animal);
EditText animalNameEdit = (EditText)findViewById(R.id.animalName);
Intent thisIntent = getIntent();
String animalId = thisIntent.getStringExtra("animalId");
HashMap<String,String> animal = controller.getAnimalInfo(animalId);
String animalName = animal.get("animalName");
animalNameEdit.setText(animalName);
}
}
activity_main_zoo.xml
---------------------
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
</ListView>
DBController.java
-----------------
public HashMap<String, String> getAnimalInfo(String id) {
HashMap<String, String> animal = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM animals where animalId='"+id+"'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String did = cursor.getString(0);
String name = cursor.getString(1);
animal.put("animalId", did);
animal.put("animalName", name);
Log.d("got ", name);
} while(cursor.moveToNext());
}
return animal;
}
public ArrayList<HashMap<String,String>> getAllAnimals() {
ArrayList<HashMap<String,String>> alist;
alist = new ArrayList<HashMap<String,String>>();
String selectQuery = "SELECT * FROM animals";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String id = cursor.getString(0);
String name = cursor.getString(1);
HashMap<String,String> map = new HashMap<String,String>();
map.put("animalId", id);
map.put("animalName", name);
alist.add(map);
Log.d("DBController", name);
} while(cursor.moveToNext());
}
return alist;
}
MainZooActivity.java
--------------------
package com.bytefly.zooexample;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainZooActivity extends ListActivity {
DBController controller = new DBController(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_zoo);
ArrayList<HashMap<String,String>> animals = controller.getAllAnimals();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView animalId = (TextView) view.findViewById(R.id.animalId);
String valAnimalId = animalId.getText().toString();
Intent objIntent = new Intent(getApplicationContext(), EditAnimal.class);
objIntent.putExtra("animalId", valAnimalId);
startActivity(objIntent);
}
});
ListAdapter adapter = new SimpleAdapter(
MainZooActivity.this,
animals, R.layout.view_animal_entry,
new String[] {"animalId", "animalName"},
new int[] {R.id.animalId, R.id.animalName});
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_zoo, menu);
return true;
}
public void showAddForm(View view) {
Intent objIntent = new Intent(getApplicationContext(), NewAnimal.class);
startActivity(objIntent);
}
}
view_animal_entry.xml
---------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/animalId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/animalName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#A4C739"
android:textStyle="bold" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment