android中的foreach循环用于破坏数据 [英] foreach loop in android for breaking data

查看:23
本文介绍了android中的foreach循环用于破坏数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 SQLBlog 类,getDataFunc 是我从数据库获取结果的函数

this is my SQLBlog class and getDataFunc is the function where i am getting result from database

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


}

在这里,我使用该结果向我的用户显示博客

Here i am using that result to show blogs to my user

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



}

但问题是我以字符串形式获取数据,但我想更动态地显示数据,例如...

But the problem is i am getting data as a string but i want to show the data more dynamically like...

嘿伙计们,你好吗:杰克发布

hey guys how are you :Posted By Jack

//这里会给出评论...

//comments will be given here...

如何从我的数据库中获取数据并将其放在可点击的 ListView 上:Nicole 发布

How to get data from my database and put it on a ListView that is clickable :Posted By Nicole

//评论在这里

我怎样才能做到这一点...需要帮助...我想我正在寻找某种类型的 foreach 循环,如 php...它可以破坏数据...但是当我使用 String 作为返回类型时,我无法得到它..

How can i achieve this..need help...i think i am searching for some type foreach loop like php...which can break the data..but as i am using String as return type i cant get it..

推荐答案

您已经有一个 foreach 循环:

You already have a foreachloop:

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

在这里,您将字符串连接在一起,只需将此 foreach 循环放入您的活动中(返回 cursor,而不是 String)或使一个包含三个字段并存储字段的新类的列表.

Here you concate your string together, just put this foreach loop in your activity (return the cursor, instead of String) or make a list with a new class which has three fields and store your fields.

这里有一个将结果封装在一个类中的示例 - 无论如何,这通常是一个好主意.但我真的建议阅读一些关于 Java 和面向对象编程的内容.

Here an example of wrapping your results in a class - which generally is a good idea anyway. But I really recommend to read some stuff about java and objectoriented programming.

如果您不想将光标返回到活动,您可以这样做.首先创建一个保存博客对象的新类:

If you don't want to reutrn the cursor to the activity you could to something like this. First createa new class which keeps blog-objects:

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

然后像这样改变你的数据库方法:

And then change your db-method like this:

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

现在在您的活动中,您可以像这样迭代:

Now in your activity you can iterate like this:

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

这篇关于android中的foreach循环用于破坏数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆