postheadericon Android SQLite Database tutorial




CarMainActivity.java

package com.bipin.rupadiya.Ex11;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CarMainActivity extends ListActivity {
SQLiteDatabase db;
String records[];
int i = 0;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setupDB();

Cursor cur = db.rawQuery("select name from CarDetail", null);
int len = cur.getCount();
records = new String[len];
cur.moveToFirst();
for (int i = 0; i < len; i++) {
  records[i] = cur.getString(0);
cur.moveToNext();
}
ArrayAdapter<String> arr = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, records);
setListAdapter(arr);

}

private void setupDB() {
db = openOrCreateDatabase("car.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("create table if not exists CarDetail(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, launch_dt TEXT,companynm text,colors Text);");
// db.execSQL("drop table CarDetail");
db.execSQL("delete from CarDetail");
db.execSQL("insert into CarDetail(name,launch_dt,companynm,colors) values('CAR-A','01/03/1987','A','White, Blue, Red, Light Yellow');");
db.execSQL("insert into CarDetail(name,launch_dt,companynm,colors) values('CAR-B','15/10/2010','B','Gray, Blue, Black, Maganta');");
db.execSQL("insert into CarDetail(name,launch_dt,companynm,colors) values('CAR-C','11/11/2011','C','Black,Green,Golden,Red');");

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

try {
Intent i = new Intent(v.getContext(), view.class);
i.putExtra("CarName", records[position]);
startActivity(i);
} catch (Exception e) {
Log.e("Ex11", e.getMessage());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}




view.java

package com.bipin.rupadiya.Ex11;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class view extends Activity {
TextView tv;
SQLiteDatabase db;
String records[];
private Gallery gallery;
String str = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);

db = openOrCreateDatabase("car.db", SQLiteDatabase.CREATE_IF_NECESSARY,
null);

tv = (TextView) findViewById(R.id.textView1);
gallery = (Gallery) findViewById(R.id.imgGallery);

Intent i = getIntent();
str = i.getStringExtra("CarName");

gallery.setAdapter(new ImageItem(this, str));
String myQuery = "select name,launch_dt,companynm,colors from CarDetail where name='"
+ str + "'";
Cursor cur = db.rawQuery(myQuery, null);

cur.moveToFirst();
tv.setText("Model: " + cur.getString(0) + "\n\n" + "Launched: "
+ cur.getString(1) + "\n\n" + "Company: " + cur.getString(2)
+ "\n\n" + "Available Colors: " + cur.getString(3));
cur.moveToNext();
}
}

class ImageItem extends BaseAdapter {
int GalItemBg;
private Context cont;
String SelectedCar;
private Integer[] carA = { R.drawable.b1, R.drawable.b2, R.drawable.b3 };
private Integer[] carB = { R.drawable.h1, R.drawable.h2, R.drawable.h3 };
private Integer[] carC = { R.drawable.m1, R.drawable.m2, R.drawable.m3 };

public ImageItem(Context c, String pos) {
cont = c;
SelectedCar = pos;
}

public int getCount() {

if (SelectedCar.equals("CAR-A")) {
return carA.length;
}

else if (SelectedCar.equals("CAR-B")) {
return carB.length;
}

else if (SelectedCar.equals("CAR-C")) {
return carC.length;
} else {
return carA.length;
}
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
if (SelectedCar.equals("CAR-A")) {
imgView.setImageResource(carA[position]);
} else if (SelectedCar.equals("CAR-B")) {
imgView.setImageResource(carB[position]);
} else if (SelectedCar.equals("CAR-C")) {
imgView.setImageResource(carC[position]);
} else {
imgView.setImageResource(carA[position]);
}
// Fixing width & height for image to display
imgView.setLayoutParams(new Gallery.LayoutParams(150, 150));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}




activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >



    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </RelativeLayout>

    <ListView
        android:id="@+android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" >

    </ListView>

</RelativeLayout>

view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="199dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Gallery
        android:id="@+id/imgGallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bipin.rupadiya.Ex11"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="CarMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="view"></activity>
    </application>

</manifest>


0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.