2013-05-16 221 views
0

這是我SQLBlog類getDataFunc就是我從數據庫foreach循環打破數據

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 

public class SQLBlog { 

    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_NAME = "persons_name"; 
    public static final String KEY_BLOG = "persons_blog"; 

    private static final String DATABASE_NAME = "blog"; 
    private static final String DATABASE_TABLE = "admin_blog"; 
    private static final int DATABASE_VERSION = 1; 

    private DBhelper ourhelper; 
    private final Context ourcontext; 
    private SQLiteDatabase ourdatabase; 

    private static class DBhelper extends SQLiteOpenHelper{ 

     public DBhelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      // TODO Auto-generated method stub 
      db.execSQL("Create table "+ DATABASE_TABLE +" (" + KEY_ROWID + " Integer primary key autoincrement, "+ 
         KEY_NAME +" text not null, "+ 
         KEY_BLOG +" text not null);"); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 
      db.execSQL("drop table if exists "+ DATABASE_TABLE); 
      onCreate(db); 
     } 

     } 
    public SQLBlog(Context c){ 
     ourcontext=c; 
    } 

    public SQLBlog open() throws SQLException{ 
     ourhelper= new DBhelper(ourcontext); 
     ourdatabase= ourhelper.getWritableDatabase(); 
     return this; 

    } 
    public void close(){ 

     ourhelper.close(); 
    } 

    public long createEntry(String name, String blog) { 
     // TODO Auto-generated method stub 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_NAME, name); 
     cv.put(KEY_BLOG, blog); 
     return ourdatabase.insert(DATABASE_TABLE, null, cv); 

    } 

    public String getDataFunc() { 
     // TODO Auto-generated method stub 
     String columns[]= new String[]{KEY_ROWID, KEY_NAME, KEY_BLOG}; 
     Cursor c = ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
     String result= ""; 
     int iRow = c.getColumnIndex(KEY_ROWID); 
     int iName = c.getColumnIndex(KEY_NAME); 
     int iBlog = c.getColumnIndex(KEY_BLOG); 

     for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iBlog) + "\n"; 
     } 
     return result; 
    } 


} 

我在這裏使用結果表明博客,我的用戶得到的結果的功能

public class SQLLiteView extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sqlview); 
     TextView tv =(TextView)findViewById(R.id.tvSQLinfo); 
     SQLBlog getdata = new SQLBlog(this); 
     getdata.open(); 
     String data = getdata.getDataFunc(); 
     getdata.close(); 
     tv.setText(data); 
    } 



} 

但問題是我得到的數據作爲一個字符串,但我想更多的像動態顯示數據...

嗨你是如何:發佈傑克

//意見將在這裏給...

如何從我的數據庫中的數據,並把它放在一個ListView是可點擊:發佈者妮可

//評論到這裏

我怎麼能實現這一點...需要幫助...我想我正在尋找一些類型的foreach循環像PHP ...它可以打破數據..但因爲我使用字符串作爲返回類型我不能得到它..

+0

請提供有關的文章和評論你的數據庫結構,我認爲這將有助於。但很少有建議:你應該創建一個代表你的表的類,並使用列表視圖的自定義適配器來映射數據(更多關於適配器和列表視圖的信息:http://www.vogella.com/articles/AndroidListView/article.html) – NeeL

+0

我發佈全班..檢查出來.. @ NeeL –

回答

0

您已有foreach循環:

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iBlog) + "\n"; 
    } 

在這裏,您concate你串起來,只是把這個foreach循環在你的活動(返回cursor,而不是String)或讓與有三個字段一類新的清單,並儲存你的領域。

這裏是將結果包裝到課堂中的一個例子 - 無論如何,這通常是個好主意。但我真的推薦閱讀一些關於java和麪向對象編程的東西。

如果你不想把光標重新放到活動上,你可以這樣做。 首先createa新的類,它保持博客的對象:

public class Blog { 

    private String author; 
    private String content; 

    public Blog(String author, String content){ 
     this.author = author; 
     this.content = content; 
    } 

    public String getAuthor(){ 
     return author; 
    } 

    public String getContent(){ 
     return content; 
    } 
} 

,然後改變你這樣的DB-方法:

public List<Blog> getDataFunc() { 
    // TODO Auto-generated method stub 
    String columns[]= new String[]{KEY_ROWID, KEY_NAME, KEY_BLOG}; 
    Cursor c = ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    List<Blog> results = new ArrayList<Blog>(); 
    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iBlog = c.getColumnIndex(KEY_BLOG); 

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
    results.add(new Blog(c.getString(iName),c.getString(iBlog))); 
    } 
    return results; 
} 

現在在你的活動,你可以重複這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sqlview); 
    TextView tv =(TextView)findViewById(R.id.tvSQLinfo); 
    SQLBlog getdata = new SQLBlog(this); 
    getdata.open(); 
    for(Blog blog : getdata.getDataFunc()){ 
     Log.v("tag",blog.getAuthor()); 
     Log.v("tag",blog.getContent()); 
    } 
    getdata.close(); 
    tv.setText(data); 
} 
+0

這條線是什麼意思?? ... Log.v(「tag」,blog.getAuthor()); –

+0

現在我得到錯誤,因爲tv.setText(data);對於數據... –

+0

你可以建議我在哪裏閱讀關於Java和麪向對象的編程..應該是有幫助的 –

1

首先,創建一個類,它將存儲您的內容:

public class BlogEntry { //I suppose you will call that a BlogEntry ? 
    public int id; 
    public String name; 
    public String question; 
    public String blog; 
    public ArrayList<Comment> comments = new ArrayList<Comment>(); 
} 

和類來存儲您的評論:

public class Comment { 
    public int id; 
    public int questionId; 
    public String name; 
    public String comment; 
    public String blog; 

} 

然後創建2代表與SQLBlog類將匹配這兩個類中所描述的數據。

您現在應該創建等的方法:

public ArrayList<BlogEntry> getAllEntries() { 

    Cursor c = db.rawQuery("SELECT * FROM blogentries", null); 
    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iBlog = c.getColumnIndex(KEY_BLOG); 
    int iQuestion = c.getColumnIndex("question"); 

    //table comment 
    int row = c.getColumnIndex("id"); 
    int name = c.getColumnIndex("name"); 
    int blog = c.getColumnIndex("blog"); 
    int iComment = c.getColumnIndex("comment"); 

    ArrayList<BlogEntry> entries = new ArrayList<BlogEntry>(); 
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     BlogEntry b = new BlogEntry(); 
     b.id = c.getInt(iRow); 
     b.name = c.getString(iName); 
     b.blog = c.getString(iBlog); 
     b.question = c.getString(iQuestion); 
     Cursor c2 = db.rawQuery("SELECT * FROM comments WHERE questionId=? ORDER BY id ASC", new String[]{String.valueOf(b.id)}); 

     for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
      Comment com = new Comment(); 
      com.questionId = b.id; 
      com.id = c.getInt(row); 
      com.name = c.getString(name); 
      com.blog = c.getString(blog); 
      com.comment = c.getString(iComment); 
      b.comments.add(com); 
     } 
     entries.add(b); 
    } 
    return entries; 
} 

你應該有東西,你可以用現在的工作,下一步是與ListView和適配器將DATAS映射創建活動。

在我的例子,我將使用ExpandableListView誰的樣子:ExpandableListView

這裏是ListViewLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
android:orientation="horizontal" android:baselineAligned="false"> 
    <ExpandableListView android:id="@+id/myExpandableListView " 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    </ExpandableListView> 
</LinearLayout> 

您的活動:

public class myActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout); 
    ExpandableListView list = (ExpandableListView) findViewById(R.id.myExpandableListView); 
    ArrayList<BlogEntry> entries = new SQLBlog().getAllEntries(); 
    list.setAdapter(new MyExpandableListAdapter(entries)); 
} 

我們需要創建一個項目佈局:(稱之爲itemlayout.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" android:baselineAligned="false"> 
<TextView 
     android:id="@+id/myTextViewThatWillHoldTheQuestion" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

你MyExpandableListAdapter:

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
    ArrayList<BlogEntry> entries; 
    LayoutInflater inflater; 
    public MyExpandableListAdapter(ArrayList<BlogEntry> entries, Context ctx) { 
     this.entries = entries; 
     inflater = LayoutInflater.from(ctx); 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return entries.get(groupPosition).comments.get(childPosition); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return entries.get(groupPosition).comments.get(childPosition).id; 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     commentHolder holder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.id.itemlayout, null); //we use the same for comment and blogentry but you shouldn't coz you want the display to change 
      holder.myTextViewThatHoldComment = (TextView)convertView.findViewById(R.id.myTextViewThatWillHoldTheQuestion); 
      convertView.setTag(holder); 
     } else { 
      holder = (commentHolder) convertView.getTag(); 
     } 
     Comment item = (Comment)getChild(groupPosition,childPosition); 
     holder.myTextViewThatHoldComment.setText(item.comment); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return entries.get(groupPosition).comments.size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return entries.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return entries.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return entries.get(groupPosition).id; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     BlogEntryHolder holder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.id.itemlayout, null); //we use the same for comment and blogentry but you shouldn't coz you want the display to change 
      holder.myTextViewThatHoldQuestion = (TextView)convertView.findViewById(R.id.myTextViewThatWillHoldTheQuestion); 
      convertView.setTag(holder); 
     } else { 
      holder = (BlogEntryHolder) convertView.getTag(); 
     } 
     BlogEntry item = (BlogEntry)getGroup(groupPosition); 
     holder.myTextViewThatHoldComment.setText(item.question); 
     return convertView; 
    } 

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

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

    private class BlogEntryHolder{ 
     TextView myTextViewThatHoldQuestion; 
    } 
    private class commentHolder{ 
     TextView myTextViewThatHoldComment; 
    } 

} 

你應該有這樣的事情你問:)