Android从rawQuery获取数据到另一种形式

发布于 2024-11-07 04:48:18 字数 1052 浏览 0 评论 0原文

我想从光标获取 IdEmployeeDetails 表单,但没有得到结果。这是我的代码:

public class EmployeeList extends ListActivity {
    protected EditText searchText;
    protected SQLiteDatabase db;
    protected Cursor cursor;
    protected ListAdapter adapter;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = (new DatabaseHelper(this)).getWritableDatabase();
        searchText = (EditText) findViewById (R.id.searchText);
    }
    public void search(View view) {
// || is the concatenation operation in SQLite
        cursor = db.rawQuery("SELECT _id FROM employee WHERE firstName=?", 
            new String[]{searchText.getText().toString()});
        Intent intent = new Intent(this, EmployeeDetails.class);
        long id = cursor.getLong(cursor.getColumnIndex("_id"));
        intent.putExtra("EMPLOYEE_ID", id);
        startActivity(intent);
    }
}

I want get Id from the cursor to the EmployeeDetails form, but I get no result. This is my code:

public class EmployeeList extends ListActivity {
    protected EditText searchText;
    protected SQLiteDatabase db;
    protected Cursor cursor;
    protected ListAdapter adapter;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = (new DatabaseHelper(this)).getWritableDatabase();
        searchText = (EditText) findViewById (R.id.searchText);
    }
    public void search(View view) {
// || is the concatenation operation in SQLite
        cursor = db.rawQuery("SELECT _id FROM employee WHERE firstName=?", 
            new String[]{searchText.getText().toString()});
        Intent intent = new Intent(this, EmployeeDetails.class);
        long id = cursor.getLong(cursor.getColumnIndex("_id"));
        intent.putExtra("EMPLOYEE_ID", id);
        startActivity(intent);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⊕婉儿 2024-11-14 04:48:18

为了从游标对象获取记录值,您需要迭代它并从中检索数据...

if(c.getCount()>0){ if (c.moveToFirst()){  c.getLong(0); }}

其中 c 是游标对象...还要检查游标是否不为空...

For getting the record values from cursor object you need to iterate it and retrieve the data from it...

if(c.getCount()>0){ if (c.moveToFirst()){  c.getLong(0); }}

where c is your cursor object... Also check whether cursor is not null...

别忘他 2024-11-14 04:48:18

如果firstName是字符串,则使用firstName,如'XYZ'

If firstName is String use firstName like 'XYZ'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文