I was trying to make a very simple list application using SQLite. The database has been successfully created and is working properly. It consists of columns such as ID, Name and Phone Number. I've displayed the names in a list on the screen and was wanting to view the other details of the person once I click their name in the list. I am passing the name of the person as an Extra from one intent to the other. I've created a function to view the other details by using this name. However, I am unable to view the details from the database. It does not show an error but it does not display any details as well. Please Help..
Main Activity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public static ArrayList<String> Array_Names= new ArrayList<String>();
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHandler = new MyDBHandler(this, null, null, 1);
dbHandler.addProduct(new Product("Mike", "222222222"));
dbHandler.addProduct(new Product("Ashley", "333333333"));
dbHandler.addProduct(new Product("Kevin", "444444444"));
dbHandler.addProduct(new Product("Nathan", "555555555"));
dbHandler.getAllContacts();
ListView mylist= (ListView)findViewById(R.id.mylist);
ArrayAdapter<String> myAdapter= new CustomAdapter(this,Array_Names);
mylist.setAdapter(myAdapter);
final Intent intent=new Intent(this,Display.class);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name=String.valueOf(parent.getItemAtPosition(position));
intent.putExtra("Name",name);
startActivity(intent);
}
});
}
}
Product Class
public class Product {
private int _id;
private String _CName;
private String _phno;
public Product(){
}
public Product(String _CName, int _id, String _phno) {
this._CName = _CName;
this._id = _id;
this._phno = _phno;
}
public Product(String _CName, String _phno) {
this._CName = _CName;
this._phno = _phno;
}
public String get_CName() {
return _CName;
}
public void set_CName(String _CName) {
this._CName = _CName;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String get_phno() {
return _phno;
}
public void set_phno(String _phno) {
this._phno = _phno;
}
}
Database Handler
import android.database.sqlite.*;
import android.content.*;
import android.database.Cursor;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1; //Change whenever db is modified.
private static final String DATABASE_NAME = "List.db"; //Db name
public static final String TABLE_NAME = "CustomerList"; //Table Name
public static final String COLUMN_ID = "Id"; //Column Name
public static final String COLUMN_CNAME = "Name";
public static final String COLUMN_PHNO= "PhoneNumber";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION); //Context is always background info.
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = " CREATE TABLE " + TABLE_NAME + " ( " +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " + " , " +
COLUMN_CNAME + " TEXT, " +
COLUMN_PHNO + " TEXT " +
" ); ";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void addProduct(Product product) {
ContentValues values = new ContentValues();
values.put(COLUMN_CNAME, product.get_CName());
values.put(COLUMN_PHNO,product.get_phno());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
}
public void removeProduct(String name) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL(" DELETE FROM " + TABLE_NAME + " WHERE " + COLUMN_CNAME + " =\" " + name + " \"; ");
}
public List<Product> getAllContacts(){
List<Product> contactList=new ArrayList<Product>();
SQLiteDatabase db= this.getWritableDatabase();
String Select_query= " SELECT * FROM " + TABLE_NAME + " ORDER BY " + COLUMN_CNAME;
Cursor cursor= db.rawQuery(Select_query,null);
if(cursor.moveToFirst()){
do {
Product contact= new Product();
contact.set_id(Integer.parseInt(cursor.getString(0)));
contact.set_CName(cursor.getString(1));
contact.set_phno(cursor.getString(2));
String name=cursor.getString(1) + "\n";
MainActivity.Array_Names.add(name);
contactList.add(contact);
}while (cursor.moveToNext());
}
return contactList;
}
public Product getProduct(String name){
SQLiteDatabase db=this.getWritableDatabase();
String query= "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CNAME + "=\""+ name+"\"";
Cursor cursor=db.rawQuery(query,null);
Log.d("This is the Name",name);
Product product= new Product();
if(cursor.moveToFirst()){
cursor.moveToFirst();
product.set_id(Integer.parseInt(cursor.getString(0)));
product.set_CName((cursor.getString(1)));
product.set_phno(cursor.getString(2));
cursor.close();
}
db.close();
return product;
}
}
Display
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Display extends AppCompatActivity {
TextView pname;
TextView pno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
Bundle main_data=getIntent().getExtras();
String message= main_data.getString("Name");
pname=(TextView)findViewById(R.id.pname);
pno=(TextView)findViewById(R.id.pno);
pname.setText(message);
MyDBHandler db=new MyDBHandler(this,null,null,1);
Product product= db.getProduct(pname.getText().toString());
pno.setText(product.get_phno());
}
}
Custom Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, ArrayList<String> names) {
super(context, R.layout.custom_list, names);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myinflater= LayoutInflater.from(getContext());
View customView= convertView;
if(customView==null){
customView=myinflater.inflate(R.layout.custom_list,parent,false);
}
String singleItem=getItem(position);
TextView mytext=(TextView) customView.findViewById(R.id.name);
ImageView defimage=(ImageView) customView.findViewById(R.id.myimage);
mytext.setText(singleItem);
defimage.setImageResource(R.mipmap.contacts_default);
return customView;
}
}