I created a listView that is hardCoded and now I want to change it to populate from a dB on the sd-card.
I created a SQLHelper (borrowed from HERE) containing the Cursor at the bottom and I modified it for a getExternalStorageState and getExternalStorageDirectory to set my dB path.
I read several tutorials on the subject but now I am lost and not sure how to modify my existing classes to work with the SQLHelper to get the listView to populate. Any help, suggestions, or snippets would be greatly appreciated and I hope this post helps out anyone who codes them selves n2 a corner - LOL THNX!
SQLHelper.java I need to implement:
public class AC_SqlHelper extends SQLiteOpenHelper
{
static String extStorageDirectory;
public void sdState()
{
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
}
else
{
//TODO
}
}
public static final String DATABASE_PATH = (extStorageDirectory + "/Folder/Folder/dB/");
public static final String DATABASE_NAME = "myAppDB";
public static final String TABLE_NAME = "aTable";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_LABEL = "label";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_CAPTION = "caption";
public static final String COLUMN_URL = "url";
public SQLiteDatabase dbSqlite;
private final Context myContext;
public AC_SqlHelper(Context context)
{
super(context, DATABASE_NAME, null, 1);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db)
{
// check if exists and copy database from resource
createDB();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w("SqlHelper", "Upgrading database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
onCreate(db);
}
public void createDatabase()
{
createDB();
}
private void createDB()
{
boolean dbExist = DBExists();
if (!dbExist)
{
copyDBFromResource();
}
}
private boolean DBExists()
{
SQLiteDatabase db = null;
try {
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e)
{
Log.e("SqlHelper", "database not found");
}
if (db != null)
{
db.close();
}
return db != null ? true : false;
}
private void copyDBFromResource()
{
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try
{
inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
}
catch (IOException e)
{
throw new Error("Problem copying database from resource file.");
}
}
public void openDataBase() throws SQLException
{
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close()
{
if (dbSqlite != null)
dbSqlite.close();
super.close();
}
public Cursor getCursor()
{
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_LABEL,
COLUMN_TITLE, COLUMN_CAPTION, COLUMN_URL };
Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
null, null, null, "title ASC");
return mCursor;
}
}
This is my current list activity (AC_List.java):
public class List_AC extends Activity
{
protected TextView activityTitle;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_view);
activityTitle = (TextView) findViewById(R.id.titleBarTitle);
activityTitle.setText("ADVISORY CIRCULATORS");
ArrayList<SearchResult> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.listItems);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Object o = lv1.getItemAtPosition(position);
SearchResult fullObject = (SearchResult)o;
Toast.makeText(List_AC.this, "You have chosen: " + " " + fullObject.getListTitle(), Toast.LENGTH_LONG).show();
Intent i = new Intent(List_AC.this, View_ACdoc.class);
i.putExtra("url", fullObject.getURL());
startActivity(i);
}
});
private ArrayList<SearchResult> GetSearchResults()
{
ArrayList<开发者_Go百科SearchResult> results = new ArrayList<SearchResult>();
SearchResult sr1 = new SearchResult();
sr1.setLabel("A Label");
sr1.setListTitle("The Title");
sr1.setCaption("Some captions.");
sr1.setURL("http://www.mysite/index.html");
results.add(sr1);
return results;
}
}
...And below are my current supporting classes:
MyCustomBaseAdapter.java:
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResult> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResult> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtLabel = (TextView) convertView.findViewById(R.id.label);
holder.txtListTitle = (TextView) convertView.findViewById(R.id.listTitle);
holder.txtCaption = (TextView) convertView.findViewById(R.id.caption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtLabel.setText(searchArrayList.get(position).getLabel());
holder.txtListTitle.setText(searchArrayList.get(position).getListTitle());
holder.txtCaption.setText(searchArrayList.get(position).getCaption());
return convertView;
}
static class ViewHolder {
TextView txtLabel;
TextView txtListTitle;
TextView txtCaption;
}
}
SearchResult.java:
public class SearchResult
{
private String label = "";
private String listTitle = "";
private String caption = "";
private String listURL = "";
private String listActivity = "";
public void setLabel(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void setListTitle(String listTitle) {
this.listTitle = listTitle;
}
public String getListTitle() {
return listTitle;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getCaption() {
return caption;
}
//
public void setURL(String listURL) {
this.listURL = listURL;
}
public String getURL() {
return listURL;
}
public void setActivity(String listActivity) {
this.listActivity = listActivity;
}
public String getActivity() {
return listActivity;
}
}
The easiest approach is to use SimpleCursorAdapter; see Binding to Data with AdapterView for a good introduction to this.
精彩评论