2015-04-05 15 views
6

Android SQLite parçaların içine nasıl uygulanır ve bu girdileri genişletilebilir liste görünümünün alt öğelerine yerleştirerek parçalara ayırmaz. http://wptrafficanalyzer.in/blog/implementing-horizontal-view-swiping-using-viewpager-and-fragmentpageradapter-in-android/Expandablelistview, simplecursoradapter'i sqlite'den doldurmak için genişletir

şimdiye i içindeki yapmak zorunda mıyız dolayısıyla Ya benim MainActivity görünüm çağrı cihazı uygulayan ediyorum:

Bu şimdiye uygulamıştır şeydir. Ben SQLite parça ve özel Expandablelist adaptör ile nasıl kullanacağımı bilmiyorum lütfen bana yardımcı olun.

Temel olarak, kullanıcının adını ve yaşını, expandablelistview'ın alt öğesinde görüntülenmesini istediğim kullanıcı girdileri olarak alıyorum. GÜNCELLEME

public class SQLiteDBAdapter { 

private static final String TAG = "DBAdapter"; //used for logging database version changes 
// DataBase info: 
public static final String DATABASE_NAME = "entry"; 
public static final String DATABASE_TABLE2 = "entryTable"; 
public static final String DATABASE_TABLE1= "dateTable"; 
public static final int DATABASE_VERSION = 7; // The version number must be incremented each time a change to DB structure occurs. 

//Field Names(date table): 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_UDATE ="date"; //common field 

// Field Names(entry table): 
public static final String KEY_EROWID = "_id"; 
public static final String KEY_CID = "Cid"; //spinner category 
public static final String KEY_TITLE = "title"; 
public static final String KEY_COST = "cost"; 
public static final String KEY_NOTE = "note"; 
public static final String KEY_DATE = "date"; 
public static final String KEY_DAY ="day"; 
private static final String KEY_MONTH = "month"; 
private static final String KEY_YEAR = "year"; 
public static final String KEY_TIME = "time"; 

public static final String[] ALL_KEYS_DATE = new String[] { KEY_UDATE,KEY_ROWID}; 
public static final String[] ALL_KEYS_ENTRY = new String[] {KEY_EROWID,KEY_CID, KEY_TITLE,KEY_COST,KEY_NOTE,KEY_DATE,KEY_DAY,KEY_MONTH,KEY_YEAR,KEY_TIME}; 



//SQL statement to create database 
private static final String DATABASE_CREATE_SQL_DATE= 
     "CREATE TABLE" + DATABASE_TABLE1 
     +"("+ KEY_ROWID +"INTEGER PRIMARY KEY AUTOINCREMENT, " 
     + KEY_UDATE +"INTEGER UNIQUE NOT NULL" 
     +");"; 
private static final String DATABASE_CREATE_SQL_ENTRY = 
     "CREATE TABLE " + DATABASE_TABLE2 
     + " (" + KEY_DATE + " TEXT NOT NULL," 
     + KEY_EROWID +"INTEGER PRIMARY KEY AUTOINCREMENT, " 
     + KEY_CID + " INTEGER," 
     + KEY_TITLE + " TEXT NOT NULL, " 
     + KEY_COST + " INTEGER," 
     + KEY_NOTE+ " TEXT NOT NULL," 
     + KEY_DAY + "INTEGER NOT NULL, " 
     + KEY_MONTH + " TEXT NOT NULL," 
     + KEY_YEAR + " INTEGER NOT NULL," 
     + KEY_TIME + " TEXT NOT NULL" 
       +"FOREIGN KEY ("+KEY_DATE+")REFERENCES "+DATABASE_TABLE1+"("+KEY_UDATE+")" //FOREIGN KEY 
     + ");"; 


private final Context context; 
private DatabaseHelper myDBHelper; 
private SQLiteDatabase db; 


public SQLiteDBAdapter(Context ctx) { 
    this.context = ctx; 
    myDBHelper = new DatabaseHelper(context); 
} 

// Open the database connection. 
public SQLiteDBAdapter open() { 
    db = myDBHelper.getWritableDatabase(); 
    return this; 
} 

// Close the database connection. 
public void close() { 
    myDBHelper.close(); 
} 

// Add a new set of values to be inserted into the database. 
//operations for the date table 
public long insertRowDate(String datestring){ 
    ContentValues values= new ContentValues(); 
    values.put(KEY_UDATE,datestring); 
    CharSequence text = " Date has been inserted"; 
    int duration = Toast.LENGTH_SHORT; 
    Toast toast = Toast.makeText(context, text, duration); 
    toast.show(); 
    return db.insert(DATABASE_TABLE1,null,values); 
} 
//operations for the entry table 
public long insertRowEntry(String cid, String title, String cost, String note,String date, String day, String monthDb, int year, String time) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_CID, cid); 
    initialValues.put(KEY_TITLE, title); 
    initialValues.put(KEY_COST,cost); 
    initialValues.put(KEY_NOTE,note); 
    initialValues.put(KEY_DATE, date); 
    initialValues.put(KEY_DAY, day); 
    initialValues.put(KEY_MONTH, monthDb); 
    initialValues.put(KEY_YEAR, year); 
    initialValues.put(KEY_TIME,time); 
    CharSequence text = " Row has been inserted"; 
    int duration = Toast.LENGTH_SHORT; 
    Toast toast = Toast.makeText(context, text, duration); 
    toast.show(); 
    // Insert the data into the database. 
    return db.insert(DATABASE_TABLE2, null, initialValues); 
} 

// Delete a row from the database, by rowId (primary key) 
public boolean deleteRowDate(long rowId) { 
    String where = KEY_ROWID + "=" + rowId; 
    return db.delete(DATABASE_TABLE1, where, null) != 0; 
} 
public boolean deleteRowEntry(long rowId) { 
    String where = KEY_EROWID + "=" + rowId; 
    return db.delete(DATABASE_TABLE2, where, null) != 0; 
} 
public void deleteAllDate() { 
    Cursor c = getAllRowsDate(); 
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID); 
    if (c.moveToFirst()) { 
     do { 
      deleteRowDate(c.getLong((int) rowId)); 
     } while (c.moveToNext()); 
    } 
    c.close(); 
} 
public void deleteAllEntry() { 
    Cursor c = getAllRowsEntry(); 
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID); 
    if (c.moveToFirst()) { 
     do { 
      deleteRowEntry(c.getLong((int) rowId)); 
     } while (c.moveToNext()); 
    } 
    c.close(); 
} 

// Return all data in the database. 
public Cursor getAllRowsDate() { 
    String where = null; 
    Cursor c = db.query(true, DATABASE_TABLE1, ALL_KEYS_DATE, where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
} 
public Cursor getAllRowsEntry() { 
    String where = null; 
    Cursor c = db.query(true, DATABASE_TABLE2, ALL_KEYS_ENTRY, where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
} 

// Get a specific row (by rowId) 
public Cursor getRowDate(long rowId) { 
    String where = KEY_ROWID + "=" + rowId; 
    Cursor c = db.query(true, DATABASE_TABLE1, ALL_KEYS_DATE, 
        where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
} 
public Cursor getRowEntry(long rowId) { 
    String where = KEY_EROWID + "=" + rowId; 
    Cursor c = db.query(true, DATABASE_TABLE2, ALL_KEYS_ENTRY, 
      where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
} 

// Change an existing row to be equal to new data. 
public boolean updateRowDate(long rowId,String date) { 
    String where = KEY_ROWID + "=" + rowId; 
    ContentValues newValues = new ContentValues(); 
    newValues.put(KEY_UDATE, date); 
    // Insert it into the database. 
    return db.update(DATABASE_TABLE2, newValues, where, null) != 0; 
} 
public boolean updateRowEntry(long rowId,String cid, String title, String cost, String note,String date,String day, String month, int year, String time) { 
    String where = KEY_EROWID + "=" + rowId; 
    ContentValues newValues = new ContentValues(); 
    newValues.put(KEY_CID, cid); 
    newValues.put(KEY_TITLE, title); 
    newValues.put(KEY_COST, cost); 
    newValues.put(KEY_NOTE, note); 
    newValues.put(KEY_DATE, date); 
    newValues.put(KEY_DAY, day); 
    newValues.put(KEY_MONTH, month); 
    newValues.put(KEY_YEAR, year); 
    newValues.put(KEY_TIME, time); 
    // Insert it into the database. 
    return db.update(DATABASE_TABLE1, newValues, where, null) != 0; 
} 
//fetching groups from the database db 
public Cursor fetchgroup(){ 
    String query="SELECT * FROM datetable"; 
      return db.rawQuery(query,null); 
} 
//fetching children from the database db 
public Cursor fetchChildren(String date) { 
    String query = "SELECT * FROM entrytable WHERE date = '" + date + "'"; 
    return db.rawQuery(query, null); 
} 
private static class DatabaseHelper extends SQLiteOpenHelper 
{ 
    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase _db) { 
     _db.execSQL(DATABASE_CREATE_SQL_ENTRY); 
     _db.execSQL(DATABASE_CREATE_SQL_DATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) { 
     Log.w(TAG, "Upgrading application's database from version " + oldVersion 
       + " to " + newVersion + ", which will destroy all old data!"); 

     // Destroy old database: 
     _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_SQL_DATE); 
     _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_SQL_ENTRY); 

     // Recreate new database: 
     onCreate(_db); 
    } 
} 
} 

:: Dan : tarih tabloyla

Sayı dolayısıyla 2

Güncelleme giriş alma faaliyeti yani aktivitesini :: SQLiteAdapter açamıyor Activity2 Verileri

entryDb.insertRowDate(date); 
       entryDb.insertRowEntry(a, title, cost, note,date, Daym,monthDb, year, time); 
olarak saklıyorum.

ve burada benim expandableListview

ben bu yüzden sorunu burada anlamaya çalışmak yorum eklemek edemez
lv = (ExpandableListView) v.findViewById(R.id.expandable_list); 
     ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter(weekdays, children); 
     lv.setAdapter(expandableListAdapter); 


public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private LayoutInflater inf; 
    private String[] groups; 
    private String[][] children; 

    public ExpandableListAdapter(String[] groups, String[][] children) { 
     this.groups = groups; 
     this.children = children; 
     inf = LayoutInflater.from(getActivity()); 
    } 

    @Override 
    public int getGroupCount() { 
     return groups.length; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 

     return children[groupPosition].length; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return groups[groupPosition]; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return children[groupPosition][childPosition]; 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     ViewHolder holder; 

     if (convertView == null) { 
      convertView = inf.inflate(R.layout.list_item, parent, false); 
      holder = new ViewHolder(); 

      holder.text = (TextView) convertView.findViewById(R.id.lblListItem); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
holder.text.setText(""+ myDataFromActivity); 
     return convertView; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     TextView textView; 
     if (convertView == null) { 
      convertView = inf.inflate(R.layout.list_group, parent, false); 

      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.lblListHeader); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 


     // holder.text.setText(getGroup(groupPosition).toString());//testing 
     //Sets the group position hence the Date 
     textView=(TextView)convertView.findViewById(R.id.date); 
     textView.setTextSize(30); 
     if((groupPosition+1)<10) 
     textView.setText("0"+(groupPosition+1)); 
     else 
     textView.setText(""+(groupPosition+1)); 
     //TODO 
     /* 
     * Think of a subtext to make the layout much more interactive 
     * */ 
     return convertView; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    private class ViewHolder { 
     TextView text; 
    } 
} 
+0

Ayrıca çağrı amaçlı olarak sqlite'deki limit ve ofsetleri de kullanabilirsiniz. sayfa uzunluğu = 10, sonra Tablo ofset 0 sınırından 10 seçin, sayfa 2 10 sınırını 10 dengeleyecekti. Kaydırmak ve simultane olarak yüklemek için, Datatables.net gibi bazı zihniyet kullanın http://datatables.net/release-datatables/ uzantılar/Scroller/example/server-side_processing.html – Pierre

+0

Üzgünüm, ancak bu yardımcı olmadı ve gönderdiğiniz bağlantı aslında tamamen bu soru ile ilgili tamamen farklı – silverFoxA

+0

Ad gibi değerleri depolamak gerekir özel bir sınıf 'Kişi' yapın ve çocuk değerlerini kurtaracak bir arraylist. Bağdaştırıcıda, üst değer ve tüm alt döngü için, alt öğe nesnesinden nesne değerlerini alır. Nesnenin listesini veritabanından geçireceksiniz. – Harry

cevap

-1

olduğunu. SQLiteAdapter'inizde gördüğünüz gibi: Expandablelistview'inizi doldurmak için veritabanınızdaki tüm kayıtları geri almak için bir yönteme sahip değilsiniz.

Belki gibi bir yöntem gerekir: Sen SetTitle, setCost yönetmek bir sınıf Record.java ihtiyaç

// Getting All records 
public List<Records> getAllRecords() { 
List<Records> recordList = new ArrayList<Records>(); 
// Select All Query 
String selectQuery = "SELECT * FROM " + DATABASE_TABLE; 

SQLiteDatabase db = this.getWritableDatabase(); 
Cursor cursor = db.rawQuery(selectQuery, null); 

// looping through all rows and adding to list 
if (cursor.moveToFirst()) { 
//String title, String cost, String note,String date, String month, int year, String time 
    do { 
     Records records = new Record(); 
     records.setTitle(cursor.getString(0)); 
     records.setCost(cursor.getString(1)); 
     records.setNote(cursor.getString(2)); 
     records.setDate(cursor.getString(3)); 
     records.setMounth(cursor.getString(4)); 
     records.setYear(Integer.parseInt(cursor.getString(5))); 
     records.setTime(cursor.getString(6)); 
     // Adding records to list 
     recordsList.add(record); 
    } while (cursor.moveToNext()); 
} 

// return contact list 
return recordsList; 
} 

.... ve bu

gibi bir bütün kaydını alabilir Bu kod, sqlite'den veri alan Faaliyete yazılmalıdır.

MySQLiteHelper db = new MySQLiteHelper(this); 
List<Records> records = db.getAllRecords();  

    for (Records rc : records) { 
     //Do something with the records 
    } 

kaynak buradadır: DatabaseSqlite with method

sorun bu mu?

+0

deneyeceğim Sağladığınız kodu ve size bildirir – silverFoxA

+0

Eğer sorun şu ki (tüm kayıtları sqlite'dan alıp kullanın). Bu kod çalışır, uygulamasında benzer bir şekilde kullanırım. Gönderdiğim bağlantıyı takip edin ve sorun yaşarsanız bana bildirin! – Mario

+0

bağlantı, öğretici dinamik değil ve ben SQLite ile uygun bir anlayış ya da deneyimim yok bu yüzden ben aşağıdaki dinamik ve ben expandablelistview adaptör – silverFoxA

İlgili konular