我仍然想不通使用SQLite结果定义光标适配器 [英] i still can not figure out custom cursor adapters with sqlite results

查看:192
本文介绍了我仍然想不通使用SQLite结果定义光标适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这一点。这是我的第一个Android应用程序,所以请帮我和裸陪我是这样一个小白....

i can not figure this out. this is my first android app so please help me out and bare with me for being such a noob....

我有以下结构的SQLite表:

i have an sqlite table with the following structure:

    String CREATE_ACHIEVEMENTS_TABLE = "CREATE TABLE achievements ("
             + "id INTEGER PRIMARY KEY,"
             + "name VARCHAR,"
             + "type VARCHAR,"
             + "value VARCHAR,"
             + "completed VARCHAR"
             + ")";

现在我所要做的是有它这样,如果已完成列设置为yes,我要排在我的列表视图的文本颜色是绿色。

now what i am trying to do is have it so that if the "completed" column is set to "yes", i want the text color of the row in my listview to be GREEN.

香港专业教育学院读教程,看着code的例子,我只是无法弄清楚如何使用自定义的CursorAdapter实现这一点。现在显示在ListView我用下面的code的结果:

ive read tutorials and looked at code examples and i just cant figure out how to implement this with a custom cursoradapter. right now to display the results in a listview i use the following code:

ShowAchievements.java(点击菜单项查看成绩时,本次活动启动):

ShowAchievements.java (this activity is started when a menu item "view achievements" is clicked):

package com.dd.qsg;

import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ShowAchievements extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.achievements);

        DatabaseHandler db = new DatabaseHandler(this);
        ArrayList<String> achievements = db.getAchievements(this);

        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, achievements));
    } 

}

在getAchievements()函数被调用上述活动:

the getAchievements() function that is called in the above activity:

public ArrayList<String> getAchievements(Context context) {
    ArrayList<String> achievementList = new ArrayList<String>();
    String selectQuery = "SELECT * FROM achievements ORDER BY id asc";

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

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                if (cursor.getString(4).equals("yes")) {
                    achievementList.add(cursor.getString(1)+" (completed)");
                }
                else {
                    achievementList.add(cursor.getString(1));
                }
            } while (cursor.moveToNext());
        }
    }
    else {
        achievementList.add(context.getResources().getString(R.string.na));
    }

    cursor.close();
    db.close();
    return achievementList;
}

就像我说我要实现自定义的CursorAdapter这样,当我从我的ShowAchievements活动中调用getAchievements(),它会使ListView的行中的文本绿色的颜色,如果已完成设置为是数据库中的该行。我希望这是有道理的。

like i said i want to implement a custom cursoradapter so that when i call getAchievements() from within my ShowAchievements activity, it will make the color of the text GREEN in the listview row if "completed" is set to "yes" for that row in the database. i hope this makes sense.

在另一个问题,我问别人帮了我,他们告诉我,来实现这样的:

in another question i asked someone helped me out, they told me to implement something like this:

private class MyCursorAdapter extends CursorAdapter {

    public MyCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View v, Context context, Cursor cursor) {
        if(cursor.getString(cursor.getColumnIndex("completed").equals("yes")){
            TextView tv = (TextView) v.findViewById(R.id.NAMEOFTEXTVIEW);
            tv.setTextColor(Color.GREEN);
        }
    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
        LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.achievement_item, parent, false);
        return row;
    }
}

我的问题是如何实现这一点,以便它工作时getAchievements()从我的活动中调用?我有什么XML文件添加?我一定要带的LinearLayout使用listview添加的XML文件在它的内部,然后用的LinearLayout和它内部的一个TextView一个单独的xml文件?或者我把TextView的在同一个布局列表视图中?做我的CursorAdapter code代替我的整个getAchievements()函数?

my question is how do i implement this so that it works when getAchievements() is called from within my activity??? what XML files do i have to add? do i have to add an xml file with a linearlayout with a listview inside of it, then a seperate xml file with a linearlayout and a textview inside of it? or do i put the textview in the same layout as the listview is in? do i replace my entire getAchievements() function with cursoradapter code?

如果有人可以请帮我的id美联社preciate了很多。我真的不知道如何实现这一点。我试过阅读一些教程和他们只是混淆了我很多。如果我能得到一个完整的例子或东西帮助我了解如何做到这一点它会帮助我很多。即时没有试图让别人来写我的所有code为我的事,但,这将有助于我把握究竟是如何做到这一点很简单是真的真棒。

if someone could please help me out id appreciate it a lot. i really have no clue how to implement this. i've tried reading some tutorials and they're just confusing me a lot. if i could get a full example or something to help me understand how to do this it would help me out a LOT. im not trying to ask for someone to write all my code for me, but something that would help me grasp exactly how to do this quite simply would be really awesome.

推荐答案

由于我无法重现你的开发环境,这是我所能做的重构你的code:

Since I'm not able to reproduce your development environment this is all I could do to refactor your code:

public class ShowAchievements extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DatabaseHandler db = new DatabaseHandler(this);
        Cursor cursor = db.getAchievements(this);
        MyCursorAdapter cursorAdapter = new MyCursorAdapter(this, cursor);

        this.setListAdapter(cursorAdapter);
    } 

    private Cursor getAchievements(Context context) {
        String selectQuery = "SELECT * FROM achievements ORDER BY id asc";

        SQLiteDatabase db = this.getWritableDatabase();
        return db.rawQuery(selectQuery, null);
    }

    private class MyCursorAdapter extends CursorAdapter {

        public MyCursorAdapter(Context context, Cursor c) {
            super(context, c);
        }

        @Override
        public void bindView(View v, Context context, Cursor cursor) {
            if(cursor.getString(cursor.getColumnIndex("completed")).equals("yes"){
                TextView tv = (TextView) v.findViewById(R.id.NAMEOFTEXTVIEW);
                tv.setTextColor(Color.GREEN);
            }
        }

        @Override
        public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
            LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.achievement_item, parent, false);
            return row;
        }
    }
}

请尝试一下,如果有固定的语法错误,让我知道它是如何工作或没有。

Please try it, fix the syntax errors if any and let me know how it works or not.

这篇关于我仍然想不通使用SQLite结果定义光标适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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