Sorry for the long description, it's hard to describe my problem without giving you some background.
Okay so I have an app that uses one SQLite database and has two tables in it Table 1 and Table 2. I have a main activity with fragment with a button that starts my Log activity with fragment. The Log activity has a tabbed viewpager and in each view of the viewpager is a listview (Tab 1 listview and Tab 2 listview) with data from the corresponding tables (Table 1 and Table 2) in the database.
So when I'm in my Log Activity with Fragment I can click on the Tab 1 and it will populate the Tab 1 listview with the data from the Table 1 in my database. When I click on Tab 2 it'll populate the Tab 2 listview with the data from my Table 2 in my database. Each tab has it's own fragment. Tab 1 fragment and Tab 2 fragment.
So to explain my issue I'll just focus on my Tab 1, because both tabs are doing the same time.
WHAT WORKS:
Let me explain first though that if I add Data X into my Tab 1, Table 1, it will populate Listview 1 with Data X and add the Data X into my Table 1 just fine. I can Log.d my Table 1 and see it got entered just fine. If I press back on the phone to go back to my Main Activity with Fragment and then decide to go back into my Log Activity with Fragment it will populate the Listview 1 just fine with Data X and Data X is still in my Table 1, because again I can Log.d my Table 1 and see the data has been entered just fine. If I delete Data X now after having gone to the Main Activity and back into the Log Activity the Data X will delete from my Table 1 just fine and it will be removed from my Listview 1. I can log.d my Table 1 after deleting and see it has been deleted from my Table 1.
WHAT DOESN'T WORK:
So my actual issue is when I add lets say Data X and Data Y into my Tab 1, Table 1 of my database. The Listview 1 will populate with Data X and Data Y and insert Data X and Data Y into the database just fine because I can Log.d the table and see Data X and Data Y have been entered into my table.... But now lets say I decide to instantly delete the Data Y (While have never left the Log Activity this time) it will not delete from the Table 1 but it will delete Data Y from my Listview 1. So now when I Log.d my Table 1 I see that I still have Data X and Data Y in my Table 1, but my Listview 1 shows only Data X. If I now press the back button and go back to my Main Activity, and then again go back into my Log Activity it will populate my Tab 1 Listview 1 with Data X and Data Y because Data Y was not actually removed from my Table 1.
It seems as if I can't Delete or Edit freshly entered Data into my Tables without first leaving my Log Activity and going back into it to "Commit/Finalize" the data entered into the Table 1. My Insert, Update, and Delete functions work just fine after I have left the Logs Activity after adding fresh data and then going back into my Logs Activity. But My Delete and Update functions do not work properly if I add fresh data and then instantly try to alter that data. My Listview is updating and populating correctly though.
WHAT I'VE RESEARCHED AND TRIED:
I've made sure I have one instance of my SQLiteOpenHelper and my SQLiteDatabase variables. I have also tried to open the database before every insert, edit, or delete and then close it after I inserted, editted, or deleted. It didn't seem to have any effect. Currently i have the datasource get closed onPause() and opens again on onResume(). And finally I have tried setting up a setTransactionSuccessful() and then endTransaction() after beginTransaction() for each Insert, Update, or Delete.
CURRENT IDEAS:
I'm thinking it might have to do with my id's of my entries in my Tables... I believe it should auto increment the id when a new entry is added to the table. They might not be what I'm expecting them to be when the data is added and then instantly try to alter the data. The id's shouldn't change though when I back out of the Log activity and go back in though so I'm not too sure.
CODE:
Here is code for one of my Tab fragments I stripped out a lot of stuff due to size of the file.
public class LogOilTabFragment extends Fragment implements View.OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mContext = getActivity();
DataSource.getInstance(mContext).open();
mListView = (ListView) view.findViewById(R.id.oil_list);
arrayOfLogs = new ArrayList<Log>();
arrayOfLogs = DataSource.getInstance(mContext).getAllLogs(LogsContract.OilTable.TAG);
mAdapter = new LogAdapter(getActivity(), arrayOfLogs);
mListView.setAdapter(mAdapter);
}
}
#Override
public void onResume() {
DataSource.getInstance(mContext).open();
super.onResume();
}
#Override
public void onPause() {
DataSource.getInstance(mContext).close();
super.onPause();
}
private void addLog() {
LayoutInflater factory = LayoutInflater.from(getActivity());
final View view = factory.inflate(R.layout.add_edit_log, null);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setView(view);
alert.setPositiveButton("ADD LOG", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
String date = dateInput.getText().toString();
String amount = amountInput.getText().toString();
String miles = milesInput.getText().toString();
if (error_count == 0) {
Log log = new Log();
log.setmDate(date);
log.setmAmount(amount);
log.setmMiles(miles);
DataSource.getInstance(mContext).insert(date, amount, miles, OilTable.TAG);
mAdapter.add(log);
mAdapter.notifyDataSetChanged();
mListView.smoothScrollToPosition(mPosition);
}
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
private void editLog() {
LayoutInflater factory = LayoutInflater.from(getActivity());
final View view = factory.inflate(R.layout.add_edit_log, null);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setView(view);
Log log = null;
//Pre fill in the edit text views with the data already in the database
if (mAdapter.getCount() > 0 && mPosition >= 0) {
log = (Log) mAdapter.getItem(mPosition);
dateInput.setText(log.getmDate());
amountInput.setText(log.getmAmount());
milesInput.setText(log.getmMiles());
}
alert.setPositiveButton("EDIT LOG", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String date = dateInput.getText().toString();
String amount = amountInput.getText().toString();
String miles = milesInput.getText().toString();
if (error_count == 0) {
Log log = (Log) mAdapter.getItem(mPosition);
DataSource.getInstance(mContext).edit(log.getmId(), date, amount, miles, OilTable.TAG);
mAdapter.clear();
arrayOfLogs = new ArrayList<Log>();
arrayOfLogs = DataSource.getInstance(mContext).getAllLogs(OilTable.TAG);
mAdapter.addAll(arrayOfLogs);
mAdapter.notifyDataSetChanged();
}
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do Nothing
}
});
alert.show();
}
private void deleteLog() {
LayoutInflater factory = LayoutInflater.from(getActivity());
final View view = factory.inflate(R.layout.delete_log, null);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setView(view);
alert.setPositiveButton("DELETE LOG", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log log = null;
if (mAdapter.getCount() > 0) {
log = (Log) mAdapter.getItem(mPosition);
DataSource.getInstance(mContext).delete(log, OilTable.TAG);
mAdapter.remove(log);
mAdapter.notifyDataSetChanged();
}
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do Nothing
}
});
alert.show();
}
Here is my class that has my Insert, Update, and Delete code from my DataSource
public class DataSource {
public static DataSource dataSource;
private SQLiteDatabase mSQLiteDatabase;
private LogsDBHelper mLogsDBHelper; //LogsDBHelper Extends SQLiteOpenHelper
}
//String of column names for the oil sqlite database.
private String[] mAllOilColumns = {
OilTable.COLUMN_ID,
OilTable.COLUMN_DATE,
OilTable.COLUMN_AMOUNT,
OilTable.COLUMN_MILES};
//String of column names for the coolant sqlite database.
private String[] mAllCoolantColumns = {
OilTable.COLUMN_ID,
OilTable.COLUMN_DATE,
OilTable.COLUMN_AMOUNT,
OilTable.COLUMN_MILES};
public static synchronized DataSource getInstance(Context context) {
if (dataSource == null) {
dataSource = new DataSource(context.getApplicationContext());
}
return dataSource;
}
private DataSource(Context context) {
mLogsDBHelper = new LogsDBHelper(context); //LogsDBHelper Extends SQLiteOpenHelper
}
public void open() throws SQLiteException {
mSQLiteDatabase = mLogsDBHelper.getWritableDatabase();
}
public void close() {
mLogsDBHelper.close();
// mSQLiteDatabase.close();
}
//Method takes a string and depending on the string creates a query from the correct
//database table and returns the amount of items in the table
public int getLength(String tag){
Cursor cursor;
switch (tag) {
case OilTable.TAG:
cursor = mSQLiteDatabase.query(OilTable.TABLE_NAME,
mAllOilColumns, null, null, null, null, null);
return cursor.getCount();
case CoolantTable.TAG:
cursor = mSQLiteDatabase.query(CoolantTable.TABLE_NAME,
mAllCoolantColumns, null, null, null, null, null);
return cursor.getCount();
}
return 0;
}
//Inserts a log into the correct sqlite table with the passed values
public void insert(String date, String amount, String miles, String tag) {
ContentValues values = new ContentValues();
switch (tag) {
case OilTable.TAG:
values.put(OilTable.COLUMN_DATE, date);
values.put(OilTable.COLUMN_AMOUNT, amount);
values.put(OilTable.COLUMN_MILES, miles);
mSQLiteDatabase.insert(OilTable.TABLE_NAME, null, values);
break;
case CoolantTable.TAG:
values.put(CoolantTable.COLUMN_DATE, date);
values.put(CoolantTable.COLUMN_AMOUNT, amount);
values.put(CoolantTable.COLUMN_MILES, miles);
mSQLiteDatabase.insert(CoolantTable.TABLE_NAME, null, values);
break;
}
}
//Edits/Updates a log into the correct sqlite table with the passed values
public void edit(Long id, String date, String amount, String miles, String tag){
ContentValues values = new ContentValues();
switch (tag) {
case OilTable.TAG:
values.put(OilTable.COLUMN_DATE, date);
values.put(OilTable.COLUMN_AMOUNT, amount);
values.put(OilTable.COLUMN_MILES, miles);
mSQLiteDatabase.update(OilTable.TABLE_NAME, values, "_id=" + id, null);
break;
case CoolantTable.TAG:
values.put(CoolantTable.COLUMN_DATE, date);
values.put(CoolantTable.COLUMN_AMOUNT, amount);
values.put(CoolantTable.COLUMN_MILES, miles);
mSQLiteDatabase.update(CoolantTable.TABLE_NAME, values, "_id=" + id, null);
break;
}
}
//Deletes a log into the correct sqlite table based on the log id.
public void delete(Log log, String tag) {
long id = log.getmId();
switch(tag) {
case OilTable.TAG:
mSQLiteDatabase.delete(OilTable.TABLE_NAME,
OilTable.COLUMN_ID + " = " + id, null);
break;
case CoolantTable.TAG:
mSQLiteDatabase.delete(CoolantTable.TABLE_NAME,
CoolantTable.COLUMN_ID + " = " + id, null);
break;
}
}
//Returns an Arraylist of all of the logs from the table specified from the passed in value.
public ArrayList getAllLogs(String tag){
ArrayList logs = new ArrayList();
Cursor cursor;
Log log;
switch(tag) {
case OilTable.TAG:
cursor = mSQLiteDatabase.query(OilTable.TABLE_NAME,
mAllOilColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
log = cursorToLog(cursor);
logs.add(log);
cursor.moveToNext();
}
cursor.close();
break;
case CoolantTable.TAG:
cursor = mSQLiteDatabase.query(CoolantTable.TABLE_NAME,
mAllCoolantColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
log = cursorToLog(cursor);
logs.add(log);
cursor.moveToNext();
}
cursor.close();
break;
}
return logs;
}
//Used in local method getAllLogs()
private Log cursorToLog(Cursor cursor) {
Log log = new Log();
log.setmId(cursor.getLong(0));
log.setmDate(cursor.getString(1));
log.setmAmount(cursor.getString(2));
log.setmMiles(cursor.getString(3));
return log;
}
//Used to get all of the dates from the specified database in the call.
public String[] getAllDates(String tag){
Cursor cursor;
int index;
String[] dates = new String[0];
int i = 0;
switch(tag) {
case OilTable.TAG:
cursor = mSQLiteDatabase.query(OilTable.TABLE_NAME, mAllOilColumns, null, null, null, null, null);
index = cursor.getColumnIndex(OilTable.COLUMN_DATE);
dates = new String[cursor.getCount()];
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
dates[i] = cursor.getString(index);
i++;
cursor.moveToNext();
}
cursor.close();
break;
case CoolantTable.TAG:
cursor = mSQLiteDatabase.query(CoolantTable.TABLE_NAME, mAllCoolantColumns, null, null, null, null, null);
index = cursor.getColumnIndex(CoolantTable.COLUMN_DATE);
dates = new String[cursor.getCount()];
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
dates[i] = cursor.getString(index);
i++;
cursor.moveToNext();
}
cursor.close();
break;
}
return dates;
}
//Used to get all of the amounts from the specified database in the call.
public String[] getAllAmounts(String tag){
Cursor cursor;
int index;
String[] amounts = new String[0];
int i = 0;
switch(tag) {
case OilTable.TAG:
cursor = mSQLiteDatabase.query(OilTable.TABLE_NAME, mAllOilColumns, null, null, null, null, null);
index = cursor.getColumnIndex(OilTable.COLUMN_AMOUNT);
amounts = new String[cursor.getCount()];
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
amounts[i] = cursor.getString(index);
i++;
cursor.moveToNext();
}
cursor.close();
break;
case CoolantTable.TAG:
cursor = mSQLiteDatabase.query(CoolantTable.TABLE_NAME, mAllCoolantColumns, null, null, null, null, null);
index = cursor.getColumnIndex(CoolantTable.COLUMN_AMOUNT);
amounts = new String[cursor.getCount()];
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
amounts[i] = cursor.getString(index);
i++;
cursor.moveToNext();
}
cursor.close();
break;
}
return amounts;
}
//Used to get all of the miles from the specified database in the call.
public String[] getAllMiles(String tag){
Cursor cursor;
int index;
String[] miles = new String[0];
int i = 0;
switch(tag) {
case OilTable.TAG:
cursor = mSQLiteDatabase.query(OilTable.TABLE_NAME, mAllOilColumns, null, null, null, null, null);
index = cursor.getColumnIndex(OilTable.COLUMN_MILES);
miles = new String[cursor.getCount()];
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
miles[i] = cursor.getString(index);
i++;
cursor.moveToNext();
}
cursor.close();
break;
case CoolantTable.TAG:
cursor = mSQLiteDatabase.query(CoolantTable.TABLE_NAME, mAllCoolantColumns, null, null, null, null, null);
index = cursor.getColumnIndex(CoolantTable.COLUMN_MILES);
miles = new String[cursor.getCount()];
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
miles[i] = cursor.getString(index);
i++;
cursor.moveToNext();
}
cursor.close();
break;
}
return miles;
}
}
LogDBHelper Code
public class LogsDBHelper extends SQLiteOpenHelper {
//Application database name and version
private static final String DATABASE_NAME = "logs.db";
private static final int DATABASE_VERSION = 1;
public LogsDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
// Database creation sql statement for oil table
final String SQL_CREATE_OIL_TABLE = "CREATE TABLE "
+ OilTable.TABLE_NAME + "( "
+ OilTable.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ OilTable.COLUMN_DATE + " TEXT NOT NULL,"
+ OilTable.COLUMN_AMOUNT + " REAL NOT NULL,"
+ OilTable.COLUMN_MILES + " INTEGER NOT NULL);";
// Database creation sql statement for coolant table
final String SQL_CREATE_COOLANT_TABLE = "CREATE TABLE "
+ CoolantTable.TABLE_NAME + "( "
+ CoolantTable.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ CoolantTable.COLUMN_DATE + " TEXT NOT NULL,"
+ CoolantTable.COLUMN_AMOUNT + " REAL NOT NULL,"
+ CoolantTable.COLUMN_MILES + " INTEGER NOT NULL);";
//Create the tables in the database
db.execSQL(SQL_CREATE_OIL_TABLE);
db.execSQL(SQL_CREATE_COOLANT_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ OilTable.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS "+ CoolantTable.TABLE_NAME);
onCreate(db);
}
}
UPDATE #1:
What I've found out after messing around and debugging for a few hours is that when I add a new data item to my table and then try to delete that freshly added item it thinks it's ID is 0... But it should not.
Example:
I have 1 data item in my table. It's data row looks like
ID: 1, Data: "Example Data 1"
Now if I add another data item to that table I'll have,
ID: 1, Data: "Example Data 1"
ID: 2, Data: "Example Data 2"
If I want to delete data item two right now after JUST inserting it. My delete query thinks I'm looking for ID: 0 so it doesn't find it and doesn't delete it. Makes sense.... But if I had my original
ID: 1, Data: "Example Data 1"
And then added a second data item so it now looks like this
ID: 1, Data: "Example Data 1"
ID: 2, Data: "Example Data 2"
And then press the back button to go back to my Main Activity... Then go back into my Log Activity to try and delete data item 2 the delete query will find the ID = 2 and delete the right data item
It's like my ID auto incremented id number isn't being committed unless I back out and go back in and then it finds it.
UPDATE #2:
Solved it,
Problem was in my Add method of my Tab Fragment... I was never updating the Log object with it's new ID after inserting the data into the table...
Log log = new Log();
Needed to set ID before adding it to mAdapter
log.setmDate(date);
log.setmAmount(amount);
log.setmMiles(miles);
mDataSource.insert(date, amount, miles, OilTable.TAG);
log.setmId(mDataSource.getLastLogID(OilTable.TAG));
mAdapter.add(log);
Solved it,
Problem was in my Add method of my Tab Fragment... I was never updating the Log object with it's new ID after inserting the data into the table...
Log log = new Log();
Needed to set ID before adding it to mAdapter
log.setmDate(date);
log.setmAmount(amount);
log.setmMiles(miles);
mDataSource.insert(date, amount, miles, OilTable.TAG);
log.setmId(mDataSource.getLastLogID(OilTable.TAG));
mAdapter.add(log);
Without it any recently added data would not be accessible by an ID in a query because I was referencing my ListView of Logs to get the Log information to edit and delete. It would work fine once i went out and loaded back into the Log Activity because my ListAdapter would populate with the full data including the IDs.
I have an application that displays a listView of contacts sorted by Last, then first names. Beside each contact is an image (icon). There are 3 kinds of contacts for which I'd like to display 3 different images (customers/suppliers/other) I have a default image now that is set to customer. I'm wondering if there's a way using the cusorLoader shown below to alternate images on the fly, or whether it would just be best to add a method involving a cursor in my onResume. (onResume is called each time I need to display the images). I believe simpleCursorAdapter can only take textViews as args, so if it's possible, maybe a compound textview/image would work. My icons are not stored in the database, just in the drawables.
Thanks in advance for any replies.
#Override
protected void onResume() {
super.onResume();
//Starts a new or restarts an existing Loader in this manager
getLoaderManager().restartLoader(0, null, this);
}
/*
* The fillData method binds the simpleCursorAadapter to the listView.
*/
private void fillData() {
String[] from = new String[] { ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
//The XML views that the data will be bound to:
int[] to = new int[] {R.id.label2, R.id.label};
getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.contact_row, null, from,
to, 0);
setListAdapter(adapter);
}
// Sort the names by last name, then by first name
String orderBy = ContactsDB.COLUMN_LAST_NAME + " COLLATE NOCASE ASC"
+ "," + ContactsDB.COLUMN_FIRST_NAME + " COLLATE NOCASE ASC" ;
// Creates a new loader after the initLoader () call
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { ContactsDB.ROW_ID, ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
CursorLoader cursorLoader = new CursorLoader(this,
SomeContentProvider.CONTENT_URI, projection, null, null, orderBy);
return cursorLoader;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in.
// (The framework will take care of closing the old cursor once we return.)
adapter.swapCursor(data); //Call requires Min API 11
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed.
// Data is no longer available, delete the reference
adapter.swapCursor(null);
}
}
Here is the code I use to dynamically show a drawable on a ListView, you have to use the function setViewBinder on your adapter:
mAdapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
//Modification of the icon to display in the list
if (aColumnIndex == aCursor.getColumnIndex(DatabaseHandler.RATE_EMOTION)) {
int emotionID = aCursor.getInt(aColumnIndex);
Drawable emotionDrawable = resources.getDrawable(R.drawable.ic_unknown_rate);
//if emotion is set
if(emotionID > 0){
String emotionDrawablePath = "ic_smi" + emotionID;
int emotionDrawableID = resources.getIdentifier(emotionDrawablePath,"drawable", getPackageName());
//if a drawable is found
if(emotionDrawableID > 0){
emotionDrawable = resources.getDrawable(emotionDrawableID);
}
}
ImageView emotionImage = (ImageView) aView;
emotionImage.setImageDrawable(emotionDrawable);
return true;
}
return false;
}
});
You can see in this example that I change the drawable according to the data I get from the cursor for every row.
My Function In SQLAdapter class is
public ArrayList<Airline> getairlinedetails(String bookingdate) {
Cursor curCalllog =db.rawQuery("SELECT * FROM "+ BOOK +
" WHERE " + date +
" BETWEEN '" + startdate + "' AND '" + enddate + "'", null);
if (curCalllog != null) {
if (curCalllog.moveToFirst()) {
do {
a=new Airline();
//a.setBookingdate(curCalllog.getString(1));
a.setPickupadd(curCalllog.getString(2));
a.setCity(curCalllog.getString(3));
a.setTrip(curCalllog.getString(4));
a.setFdate(curCalllog.getString(5));
a.setFtime(curCalllog.getString(6));
a.setCdate(curCalllog.getString(7));
a.setPtime(curCalllog.getString(8));
a.setSeats(curCalllog.getInt(9));
a.setAmount(curCalllog.getInt(10));
update.add(a);
} while (curCalllog.moveToNext());
}
}
return update;
}
M Fetching data between two dates and
I Want To show the fetched data into listview please help me how to do it I m new in android development.
You can use SimpleCursorAdapter for showing Databse contents in Listview. Make instance of SimpleCursorAdapter and pass Cursor object into it. Refer this link
If you want Customized Listview, you can customize SimpleCursorAdapter by extending this with your custom adapter class.
you can follow this example :
DataManipulator.java -helper class
//to retrieve data in a list
public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME, new String[] { "id","name","number","skypeId","address" },
null, null, null, null, "name asc");
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
CheckData.java
// to show data in a list view
public class CheckData extends ListActivity {
TextView selection;
public int idToModify;
DataManipulator dm;
List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
String[] stg1;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
dm = new DataManipulator(this);
names2 = dm.selectAll();
stg1=new String[names2.size()];
int x=0;
String stg;
for (String[] name : names2) {
stg = name[1]+" - "+name[2]+ " - "+name[3]+" - "+name[4];
stg1[x]=stg;
x++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,
stg1);
this.setListAdapter(adapter);
selection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
selection.setText(stg1[position]);
}
}
follow this link for complete tutorial
I assume you are getting data from database and there by complete ArrayList with Airline objects.
Now to display data in ListView from ArrayList<Airline>, you have to define Custom adapter class by extending either BaseAdapter or ArrayAdapter.
Here you go: How to define custom adapter for ListView?
I Want To show the fetched data into listview please help me how to do
it I m new in android development
Simpliest way is to use ArrayAdapter with build-in ListView's row layout.
ListView list = (ListView) findViewById(R.id.yourList);
ArrayList<Airline> data = db.getairlinedetails("someString");
ArrayAdapter<Airline> adapter = new ArrayAdapter<Airline>(this,
android.R.layout.simple_list_item_1, data);
list.setAdapter(adapter);
But since this you need to also override toString() method in your Airline class.
Reason is that your ArrayAdapter will convert each child(provided list of airlines) to String and if you won't override toString() you will get default string representation of object but you probably need to show for instance name of airline so your method can looks like
#Override
public String toString() {
return this.name;
}
Note:
This is simple way. But if you want to get more control over ListView and create custom list i recommend you to create own subclass of ListAdapter for example BaseAdapter and define your own Adapter. Sorry but i won't write you implementation because it requires much more code but nice examples you can find here:
Customizing Android ListView Items with Custom ArrayAdapter
Android ListView, ListActivity and ListFragment - Tutorial
Android Custom ListView with Image and Text
Hi I am developing an android in which I am loading data from database and showing it in a List View.
My code is as follows
public class rel extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.refill);
//Creating an object called mydbhelper
Dbms mydbhelper = new Dbms(this);
//Loading the database in writable format
SQLiteDatabase db=mydbhelper.getWritableDatabase();
String[] id= new String[]{"_id","medicine","hs"};
//Cursor
Cursor c = db.query("medicines",id,null,null,null, null,null);
startManagingCursor(c);
String[] from = new String[]{"medicine"};
int[] to = new int[] { R.id.textlist1};
// Now creating an array adapter and set it to display using my row
SimpleCursorAdapter notes =new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
What i want to achieve is show a list view as "Medicine-hs" in which medicine is from one column and hs is from an another column of database. For example as
XYZ-HS1
ABC-HS2
as the listview contents
How to achieve this?
Right now I am able to get only Medicine
Please give your kind suggestions
}
I want to say replace this:
String[] from = new String[]{"medicine"};
int[] to = new int[] { R.id.textlist1};
with this:
String[] from = new String[]{"medicine", "medicine-hs"};
int[] to = new int[] { R.id.textlist1, R.id.textlist2};
You will of course need to have a R.id.textlist2 available to use in your layout.
EDIT: Reverted back to the previous revision, which solved the problem.
What you could do is execute the rawQuery() method off the DBHelper class with SQL that combines the fields like so:
SELECT medicine + "-" + hs AS medicineHs FROM medicines ...
so the string array would be
String[] from = new String[]{"medicineHs"};
I get my data out of my db with the following code:
private void fillData() {
cursor = mDbAdapter.fetchAllSubjects();
startManagingCursor(cursor);
String[] from = new String[] { DatabaseAdapter.KEY_TITLE, DatabaseAdapter.KEY_LECTURER, DatabaseAdapter.KEY_BEGIN };
int[] to = new int[] { R.id.title, R.id.lecturer, R.id.time };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter subjects = new SimpleCursorAdapter(this, R.layout.subject_row, cursor, from, to);
setListAdapter(subjects);
}
Now my problem is, that I want to add 3 other columns from my db and want to get the following:
"("+DatabaseAdapter.KEY_TYPE+") "+DatabaseAdapter.KEY_TITLE
DatabaseAdapter.KEY_LECTURER
new Date(DatabaseAdapter.KEY_BEGIN)
new Date(DatabaseAdapter.KEY_END)
--> these two should be in one TextView in the way dd.MM. HH:mm (this is from BEGIN) - HH:mm (this is from END)
I don't know how I'm able to do that - please help me :)
Ok I finally figured out what you really wanted.
Instead of using "SimpleCursorAdapter" directly, you can create your own Cursor adapter, inside which you can mainipulate the data as you want.
Create a new Adapter "SubjectsAdapter.java". In this Adapter you will override the "bindView" and "newView". This allows us to apply a view to the cursor. But before doing so, gives us the opportunity to change the data from the cursor.
This will give you an idea what has to be done.
private void fillData()
{
cursor = mDbAdapter.fetchAllSubjects();
startManagingCursor(cursor);
SubjectsAdapter subjectsAdapter = new SubjectsAdapter(this, cursor);
setListAdapter(subjectsAdapter);
}
//SubjectsAdapter.java - make changes to fix bugs/compilation errors. This is untested.
public class SubjectsAdapter extends ResourceCursorAdapter
{
public SubjectsAdapter(Context context, Cursor cur) {
super(context, R.layout.subject_row, cur);
}
#Override
public View newView(Context context, Cursor cur, ViewGroup parent)
{
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.subject_row, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView titleText = (TextView)view.findViewById(R.id.title);
titleText.setText(cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_TITLE)));
//You can add code to retrieve other columns here.
//This is where you retrieve the date in long format from cursor, convert it to a required format, and then using it.
TextView beginTimeText = (TextView)view.findViewById(R.id.time);
Long lBeginDate = cursor.getLong(cursor.getColumnIndex(DatabaseAdapter.KEY_BEGIN));
String sBeginDate = getFormattedDate(lBeginDate);
beginTimeText.setText(sBeginDate);
}
private String getFormattedDate(Long lDate)
{
SimpleDateFormat smdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
String sDate = smdf.format( lDate ));
return sDate;
}
}