2011-04-15 2 views
0

J'ai créé un listView qui est codé en dur et maintenant je veux le changer pour remplir à partir d'un dB sur la carte SD.ListView et SQLite sur l'aide SD

J'ai créé un SQLHelper (emprunté à HERE) contenant le curseur en bas et je l'ai modifié pour un getExternalStorageState et getExternalStorageDirectory pour définir mon chemin dB.

J'ai lu plusieurs tutoriels sur le sujet mais maintenant je suis perdu et je ne sais pas comment modifier mes classes existantes pour travailler avec SQLHelper pour obtenir le listView à peupler. Toute aide, suggestion, ou extrait serait grandement appréciée et j'espère que ce post aide tous ceux qui se le font n2 dans un coin - LOL THNX!

SQLHelper.java Je dois mettre en œuvre:

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; 
} 

} 

Ceci est mon activité courante de la liste (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<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; 
} 
} 

... Et ci-dessous sont mes classes actuelles de soutien:

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; 
    } 
} 

Répondre

Questions connexes