如何在android中显示hashmap中的数据?

时间:2012-03-04 10:47:36

标签: android sqlite android-layout android-edittext

我有一个我想要显示的hashmap。用户将在editText中输入一个名称,当他们单击搜索按钮时,它应该通过hashmap并显示与edittext中的文本匹配的条目和键。

HashMap<String, Staff> h = new HashMap<String, Staff>();
Staff staff = new Staff("Thomas", "133", "thomas133@email.com");
h.put("Thomsas", staff);

我试图改编记事本应用程序

package android;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.project.R;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;

public class SingleStaff extends ListActivity {
private DBAdapter mDbHelper;
private EditText staffName;

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

    setContentView(R.layout.searchstaff);
    staffName = (EditText) findViewById(R.id.staffname);
    mDbHelper = new DBAdapter(this);
    mDbHelper.open();
    registerForContextMenu(getListView());

    Button search = (Button) findViewById(R.id.confirm);
    search.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) 
        {
            String a = staffName.getText().toString();
            fillData(a);          
        }
    });

}

 private void fillData(String staffName) {
        Cursor notesCursor = mDbHelper.fetchSingleStaff(staffName);
        startManagingCursor(notesCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{ DBAdapter.STAFF_NAME, DBAdapter.STAFF_ROOM, DBAdapter.STAFF_EMAIL};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.note_row_staff, notesCursor, from, to);
        setListAdapter(notes);
    }

}

当我运行应用程序并在edittext中键入例如“Thomas”并单击搜索时,我收到错误消息,

android.database.sqlite.SQLiteException: no such column: Thomas: , while compiling: SELECT     
DISTINCT _id, name, room, email FROM staff WHERE name=Thomas

1 个答案:

答案 0 :(得分:0)

您可以将查询行更改为(请注意staffName周围的单引号:

Cursor mCursor = mDb.query(true, STAFF_TABLE, new String[] {STAFF_ROWID, STAFF_NAME, STAFF_ROOM, STAFF_EMAIL}, STAFF_NAME + "='" + staffName + "'", null, null, null, null, null);

最好使用?占位符来避免此类问题(未经测试)

public Cursor fetchSingleStaff(String staffName) throws SQLException {
    String[] arg = new String[] { staffName }; 
    Cursor mCursor = mDb.query(true, STAFF_TABLE, new String[] {STAFF_ROWID, STAFF_NAME, STAFF_ROOM, STAFF_EMAIL}, STAFF_NAME + "=?", arg , null, null, null, null); 
    if (mCursor != null) { 
        mCursor.moveToFirst(); 
    } 
    return mCursor; 
}

如果您想使用地图,可以重载上述方法,并使用地图中相应的值设置staffName

public Cursor fetchSingleStaff(Map<String, Staff> m) throws SQLException {
    return fetchSingleStaff(m.getFirstName); // m.getFirstName() should be mapped the `Thomas` in `Staff` - Update according to your implementation
}