Android List View Using Custom Adapter and SQLite
Android List View Using Custom Adapter and SQLite
Android List View Using Custom Adapter and SQLite
package com.arun.democustomadapter;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
package com.arun.democustomadapter;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
sqlDatabase.execSQL(query);
} catch (Exception e) {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
c1 = sqlDatabase.rawQuery(query, null);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
return c1;
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name" />
<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone" />
<EditText
android:id="@+id/et_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn_submit"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:text="@string/submit" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
android:id="@+id/tv_slno"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="slno"
android:textColor="#000" />
<TextView
android:id="@+id/tv_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/name"
android:textColor="#000" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/phone"
android:textColor="#000" />
</LinearLayout>
<ListView
android:id="@+id/lv_custom_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
</ListView>
</LinearLayout>
4. After designing Main.xml next is to create the
MainActivity, the class is given below
package com.arun.democustomadapter;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
SqlHandler sqlHandler;
ListView lvCustomList;
EditText etName, etPhone;
Button btnsubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvCustomList = (ListView) findViewById(R.id.lv_custom_list);
etName = (EditText) findViewById(R.id.et_name);
etPhone = (EditText) findViewById(R.id.et_phone);
btnsubmit = (Button) findViewById(R.id.btn_submit);
sqlHandler = new SqlHandler(this);
showList();
btnsubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
contactListItems.setSlno(c1.getString(c1
.getColumnIndex("slno")));
contactListItems.setName(c1.getString(c1
.getColumnIndex("name")));
contactListItems.setPhone(c1.getString(c1
.getColumnIndex("phone")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
How code works>>>when the submit button is clicked
values in EditTextfields are inserted into database that is
written inside
1 btnsubmit.setOnClickListener(new OnClickListener() {
2
3 @Override
4 public void onClick(View v) {.....
package com.arun.democustomadapter;
String slno;
String name;
String phone;
}
6. Then create a Custom Adapter class by extending
BaseAdapter , class is given below
package com.arun.democustomadapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
Context context;
ArrayList<ContactListItems> contactList;
this.context = context;
contactList = list;
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ContactListItems contactListItems = contactList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_list_row, null);
}
TextView tvSlNo = (TextView) convertView.findViewById(R.id.tv_slno);
tvSlNo.setText(contactListItems.getSlno());
TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
tvName.setText(contactListItems.getName());
TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
tvPhone.setText(contactListItems.getPhone());
return convertView;
}
}
how custom Adapter works >>>> the main part of custom
Adapter is
public View getView(int position, View convertView, ViewGroup arg2)
1 {.....
to customize the listview we need to create an
xml(contact_list_row.xml).This xml layout is the row of our
listView.This layout is inflated using inflater service
<TextView
android:id="@+id/tv_slno"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="slno"
android:textColor="#000" />
<TextView
android:id="@+id/tv_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView"
android:textColor="#000" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView"
android:textColor="#000" />
</LinearLayout>
Posted by arun krishna at 11:25