I am new to android and have a sqlite database with two tables, I have joined the two tables and they display perfectly on a toast. My problem is that, when I try and use a simpleCursorAdapter, the list doesn't display. Can anyone help me solve this issue?
HERE'S MY MAIN
package com.harun.offload002;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class VehicleActivity extends ListActivity {
private final Context context = this;
private SimpleCursorAdapter mCursorAdapter;
DbAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vehicle);
dbAdapter = new DbAdapter(context);
populateList();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_vehicle, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_add_vehicle) {
showVehicleInputDialog();
}
else if (id == R.id.action_add_offload){
showOffloadInputDialog();
}
else if (id == R.id.action_delete_vehicle){
}
return super.onOptionsItemSelected(item);
}
private void showVehicleInputDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
final EditText mVehicleInput = (EditText) promptView.findViewById(R.id.vehicle_input);
mVehicleInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(getString(R.string.enter_vehicle_registration_instruction));
builder.setView(promptView);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Get string input data
String vehicleRegistration = mVehicleInput.getText().toString();
//Convert input to string and show in a toast
// Toast.makeText(context, vehicleRegistration, Toast.LENGTH_LONG).show();
//Save input to db
long id = dbAdapter.insertVehicle(vehicleRegistration);
if(id < 0){
Toast.makeText(context, "Unsuccessful", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, vehicleRegistration, Toast.LENGTH_LONG).show();
populateList();
}
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void showOffloadInputDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
final EditText mOffloadInput = (EditText) promptView.findViewById(R.id.vehicle_input);
mOffloadInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(getString(R.string.enter_offload_registration_instruction));
builder.setView(promptView);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Get string input data
int offloadInput = Integer.parseInt(mOffloadInput.getText().toString());
//Convert input to string and show in a toast
// Toast.makeText(context, vehicleRegistration, Toast.LENGTH_LONG).show();
//Save input to db
long id = dbAdapter.insertOffload(offloadInput);
if(id < 0){
Toast.makeText(context, "Unsuccessful", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, offloadInput+"", Toast.LENGTH_LONG).show();
populateList();
}
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
public void populateList() {
Cursor cursor = dbAdapter.getAllData();
String[] from = { DbAdapter.DbHelper.VEHICLE_REG, DbAdapter.DbHelper.KEY_ID};
int[] to = {android.R.id.text1};
mCursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_1, cursor, from, to, 0);
setListAdapter(mCursorAdapter);
}
}
HERE'S MY XML
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VehicleActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@android:id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:text="@string/empty_vehicle_list"/>
</RelativeLayout>
And here's my database Adapter
package com.harun.offload002;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.widget.Toast;
/**
* Created by HARUN on 2/4/2015.
*/
public class DbAdapter {
DbHelper helper;
SQLiteDatabase db;
public DbAdapter(Context context){
helper = new DbHelper(context);
}
StringBuffer buffer = new StringBuffer();
public long insertVehicle(String vehicleRegistration){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DbHelper.VEHICLE_REG, vehicleRegistration);
long insertId = db.insert(DbHelper.VEHICLES_TABLE, null, contentValues);
return insertId;
}
public long insertOffload(int vehicleOffload){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DbHelper.OFFLOAD, vehicleOffload);
long insertId = db.insert(DbHelper.OFFLOAD_TABLE, null, contentValues);
return insertId;
}
public String getVehicleData(){
//Select _id, vehicleRegistration from table Vehicles
Cursor cursor = readVehicles();
while (cursor.moveToNext()){
int index0 = cursor.getColumnIndex(DbHelper.KEY_ID);
int index1 = cursor.getColumnIndex(DbHelper.VEHICLE_REG);
String cId = cursor.getString(index0);
String vehicleNo = cursor.getString(index1);
buffer.append(cId+" "+vehicleNo);
}
return buffer.toString();
}
public String getOffloadData(){
//Select _id, vehicleRegistration from table Vehicles
Cursor cursor = readOffload();
while (cursor.moveToNext()){
int index0 = cursor.getColumnIndex(DbHelper.KEY_ID);
int index1 = cursor.getColumnIndex(DbHelper.OFFLOAD);
String cId = cursor.getString(index0);
String offload = cursor.getString(index1);
buffer.append(offload);
}
return buffer.toString();
}
public Cursor getAllData(){
//Select _id, vehicleRegistration from table Vehicles
Cursor cursor = readAllData();
while (cursor.moveToNext()){
int index0 = cursor.getColumnIndex(DbHelper.KEY_ID);
int index1 = cursor.getColumnIndex(DbHelper.VEHICLE_REG);
int index2 = cursor.getColumnIndex(DbHelper.OFFLOAD);
String cId = cursor.getString(index0);
String vehicleNo = cursor.getString(index1);
String offload = cursor.getString(index2);
buffer.append(cId+" "+vehicleNo+" "+offload);
}
return cursor;
}
//Read data from the db...
private Cursor readVehicles() {
db = helper.getWritableDatabase();
return db.query(DbHelper.VEHICLES_TABLE, DbHelper.VEHICLE_COLUMNS, null, null, null, null, null);
}
private Cursor readOffload() {
db = helper.getWritableDatabase();
return db.query(DbHelper.OFFLOAD_TABLE, DbHelper.OFFLOAD_COLUMNS, null, null, null, null, null);
}
private Cursor readAllData() {
db = helper.getWritableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(DbHelper.VEHICLES_TABLE+","+ DbHelper.OFFLOAD_TABLE);
qb.appendWhere(DbHelper.VEHICLES_TABLE+"."+DbHelper.VEHICLE_REG+"="+DbHelper.OFFLOAD_TABLE+"."+DbHelper.VEHICLE_REG);
String asColumnReturn[] ={
DbHelper.VEHICLES_TABLE+"."+DbHelper.KEY_ID,
DbHelper.VEHICLES_TABLE+"."+DbHelper.VEHICLE_REG,
DbHelper.OFFLOAD_TABLE+"."+DbHelper.KEY_ID,
DbHelper.OFFLOAD_TABLE+"."+DbHelper.VEHICLE_REG,
DbHelper.OFFLOAD_TABLE+"."+DbHelper.OFFLOAD
};
return qb.query(db, asColumnReturn, null, null, null, null, null);
}
static class DbHelper extends SQLiteOpenHelper{
private Context context;
private static final String DATABASE_NAME = "VehicleDatabase";
private static final int DATABASE_VERSION = 6;
//Common columns
protected static final String KEY_ID = "_id";
protected static final String VEHICLE_REG = "VehicleReg";
//Column of Vehicles Table
protected static final String VEHICLES_TABLE = "VEHICLETABLE";
private static final String[] VEHICLE_COLUMNS = {KEY_ID, VEHICLE_REG};
//Column of Offload Table
protected static final String OFFLOAD_TABLE = "OFFLOADTABLE";
protected static final String OFFLOAD = "VehicleOff";
protected static final String[] OFFLOAD_COLUMNS = {KEY_ID, OFFLOAD};
private static final String CREATE_VEHICLES_TABLE = "CREATE TABLE "
+ VEHICLES_TABLE +" ("
+ KEY_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ VEHICLE_REG+" VARCHAR(255));";
private static final String CREATE_OFFLOAD_TABLE = "CREATE TABLE "
+ OFFLOAD_TABLE +" ("
+ KEY_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ VEHICLE_REG + " VARCHAR(255), "
+ OFFLOAD+" VARCHAR(255));";
private static final String DROP_VEHICLES_TABLE = "DROP TABLE IF EXISTS "+ VEHICLES_TABLE;
private static final String DROP_OFFLOAD_TABLE = "DROP TABLE IF EXISTS "+ OFFLOAD_TABLE;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Toast.makeText(context, context.getString(R.string.constructor_call), Toast.LENGTH_LONG).show();
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_VEHICLES_TABLE);
db.execSQL(CREATE_OFFLOAD_TABLE);
Toast.makeText(context, context.getString(R.string.on_create_call), Toast.LENGTH_LONG).show();
}
catch (SQLException e){
Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Toast.makeText(context, context.getString(R.string.on_upgrade_call), Toast.LENGTH_LONG).show();
db.execSQL(DROP_VEHICLES_TABLE);
db.execSQL(DROP_OFFLOAD_TABLE);
onCreate(db);
}
catch (SQLException e){
Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
}
}
public void deleteDatabase(){
context.deleteDatabase(DATABASE_NAME);
}
}
}
Aucun commentaire:
Enregistrer un commentaire