I need to populate a listview with data from an existing sqlite database. Also we want to load the images from the Internet by using the URLs stored in the database.
I have been able to populate the list view with images from the Internet by creating string arrays in my activity and using Lazy Loader. Lazy Loader accepts a String[] of URLs no problem
What I also have been able to do is populate my listview with data from the existing database. The only thing not working is the URLs which is why I'm having a problem.
I need to to get these URLs from the database into a String Array that Lazy Loader will accept as an argument.
I've checked in my books, every Internet posting I could find even remotely related to this topic, and I've tried everything I can think of. Please help!
Here is the Activity that works for populating the listview with data and loading images from internet by using URLs. At this point the getList String[] isn't being used.
package com.example.ordergenie;
import android.annotation.TargetApi;
import android.app.Activity;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//import static com.example.ordergenie.DBAdapter.getImage;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class ViewDB extends Activity {
ListView list;
LazyAdapter adapter;
DBAdapter myDb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewdb);
try {
openDB();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list = (ListView) findViewById(R.id.currentSupportList);
adapter = new LazyAdapter(this, mStrings, mSizes, mTypes, mManufacturers, mPartNumbers);
list.setAdapter(adapter);
}
private void openDB() throws SQLException {
myDb = new DBAdapter(this);
DBAdapter.open();
}
@Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
public OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
};
private String[] mSizes = {
//DBAdapter.getSupportSize()
//String.valueOf(DBAdapter.getSupportSize())
"3/8", "1/2", "3/4", "12_8 AWG", "1/2-3/4", "1/2", "1", "1", "1", "1_1/4"
};
private String[] mTypes = {
// DBAdapter.getSupportType()
//String.valueOf(DBAdapter.getSupportType())
"One-Hole", "One-Hole", "One-Hole", "Wire Clip", "Hammer-On", "Conduit Hanger", "Two Piece", "One Piece", "One-Hole", "One Piece"
};
private String[] mManufacturers = {
// DBAdapter.getSupportManufacturer()
//String.valueOf(DBAdapter.getSupportManufacturer())
"Arlington", "Arlington", "Arlington", "CADDY", "CADDY", "Arlington", "Cooper B-line", "CADDY", "Arlington", "CADDY"
};
private String[] mPartNumbers = {
// DBAdapter.getSupportPartNumber()
// String.valueOf(DBAdapter.getSupportPartNumber())
"310", "370", "371", "KX", "812M24SM", "2000", "B2210PAZN", "SCH16", "372", "SCH20B"
};
/////////////////////////////////////////////////////////////////////I've tried passing this to Lazy Loader, but it didnt work either.
// public String[] getList() {
// try {
// ArrayList<String> rv = new ArrayList<String>();
// openDB();
// String[] columns = new String[]{DBAdapter.KEY_SUPPORT_IMAGE};
// Cursor c = myDb.db.query(myDb.DATABASE_TABLE2, new String[] {DBAdapter.KEY_SUPPORT_IMAGE}, null, null, null, null, null);
//
// if (c==null)
// return null;
// c.moveToFirst();
// c.moveToNext();
// c.isAfterLast();
// rv.add(String.valueOf(c.getColumnIndex(DBAdapter.KEY_SUPPORT_IMAGE)));
// c.close();
// return columns;
// } catch (SQLException e) {
// e.printStackTrace();
// return null;
// }
// }
String[] mStrings = new String[]
//{DBAdapter.KEY_SUPPORT_IMAGE};
//private String[] mStrings = DBAdapter.getSupportImage();
//private String[] mStrings = DBAdapter.getSupportImage();
{
//String.valueOf(DBAdapter.getSupportImage())
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1 T0VPYTZUQXpvcG1zVUU/a70.png",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a90.png",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a102.png",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a103.png",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a94.png",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a116.png",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a131.png",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a137.png",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a167.jpg",
"https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a174.jpg"
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a179.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a187.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a190.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a208.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a211.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a212.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a213.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a214.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a216.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a220.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a222.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a232.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a233.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a237.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a241.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a252.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a268.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a274.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a275.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a276.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a280.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a281.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a295.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a296.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a298.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a304.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a312.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a314.jpg"
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a181.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a182.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a183.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a184.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a185.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a187.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a188.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a189.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a19.png",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a190.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a191.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a192.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a193.jpg",
//// "https://0668aec6af557716c1add31dcc5232f13eaf1aab.googledrive.com/host/0B1U_avJ1T0VPYTZUQXpvcG1zVUU/a174.jpg"
};
}
Here is the activity that is loading data from the existing database.
package com.example.ordergenie;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;
import java.sql.SQLException;
public class viewImage extends Activity {
DBAdapter myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_image);
try {
openDB();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
populateListFromDbSupport();
}
private void openDB() throws SQLException {
myDb = new DBAdapter(this);
DBAdapter.open();
}
private void closeDB() {
myDb.close();
}
private void populateListFromDbConduit() {
Cursor cursor = DBAdapter.getAllItemsSupport();
startManagingCursor(cursor);
///setup mapping from cursor to view fields//////
String[] fromDatabase = new String[]
{DBAdapter.KEY_SUPPORT_SIZE, DBAdapter.KEY_SUPPORT_TYPE, DBAdapter.KEY_SUPPORT_PARTNUMBER, DBAdapter.KEY_SUPPORT_IMAGE};
int[] toList = new int[]
{R.id.item_size, R.id.item_type, R.id.item_quantity, R.id.item_icon};
////create adapter to map columns of database to rows in listview//////
@SuppressWarnings("deprecation")
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this,
R.layout.current_item_layout,
cursor,
fromDatabase,
toList
);
////set the adapter for the listview
ListView myList = (ListView) findViewById(R.id.currentSupportList);
myList.setAdapter(myCursorAdapter);
///////////end of clear all button/////////////////
///////end of populatiing the listview method////////////////////
/////////////////////end of toast for item clicked on listview//////////////////////
}
}
/////////////end of current order class////////////////////////
So what I need to do is create a String array that Lazy Loader will accept from the URLs gathered from the database. Please let me know what other information may be needed. I'm new to these help sites.. Also there is a lot that is commented out from all of the trial and error. It's a mess
///////////////////////Update 2 hours later////////////////////////////////////////
After weeks of trying, I post here seeking help, but end up getting it to work shortly after. Here is what I did to be able to pull data and image URLs from Sqlite database column to populate listview.
private String[] populateImageList(){
Cursor crs = DBAdapter.getSupportImage();
startManagingCursor(crs);
String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex(DBAdapter.KEY_SUPPORT_IMAGE));
array[i] = uname;
i++;
}
Log.d("myTag", array[0]);
return array;
}
Now it iterates through the data storing it in a string array that LazyAdapter will accept.