代码之家  ›  专栏  ›  技术社区  ›  Siddharth Lele

将结果从SQLite数据库强制转换为EditText时出现问题

  •  0
  • Siddharth Lele  · 技术社区  · 14 年前

    android.database.sqlite.SQLiteCursor@43d34310

    我把代码片段附在帖子里征求意见。

    ListActivity onCreate()代码:

    public class ContactsList extends ListActivity {
    DBAdapter dbContactList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editproject_list);
    
        dbContactList = new DBAdapter(this);
        // Pull the data from the database
        String[] fields = new String[]    {    dbContactList.TABLE_CON_NAME    };
        int[] views = new int[]    {    android.R.id.text1    };
    
        Cursor c = dbContactList.getAllContacts();
        startManagingCursor(c);
    
        // Set the ListView
        ListAdapter prjName = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, c, fields, views);
        setListAdapter(prjName);
    
        dbContactList.close();
    }
    

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
    
        Intent selectedContact = new Intent();
    
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
    
        selectedContact.putExtra("selectedcontact", keyword);
    
        setResult(RESULT_OK, selectedContact);
        finish();
    
    }
    

    以及用于启动ListView活动和获取结果的项目UI代码:

    public void onClickContactPicker(View target)
    {
        Intent contactPicker = new Intent(this, com.dzinesunlimited.quotetogo.ContactsList.class);
        startActivityForResult(contactPicker, contact);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String contact = data.getExtras().getString("selectedcontact").toString();
    
        ((EditText)findViewById(R.id.txtProjectContacts)).setText(contact);
    }
    

    public Cursor getAllContacts()
    {
        Cursor cursor = null;
    
        try
        {
            cursor = db.query(TABLE_CONTACTS, new String[] {  
                    TABLE_CON_ID, TABLE_CON_NAME, TABLE_CON_EMAIL,
                    TABLE_CON_EXPERTISE, TABLE_CON_CHARGES}, 
                    null,
                    null, 
                    null, 
                    null, 
                    null);
        }
        catch (SQLiteException e)
        {
            Log.e("Database.addRow", "Database Error: " + e.toString());
            e.printStackTrace();
        }
    
        return cursor;
    }
    

    请提出解决办法。

    提前谢谢。

    1 回复  |  直到 13 年前
        1
  •  4
  •   CommonsWare    14 年前

    对象o=this.getListAdapter().getItem(位置);

    o 是一个 Cursor ,位于正确的行。

    字符串关键字=o.toString();

    此时, keyword android.database.sqlite.SQLiteCursor@43d34310 ,因为这就是你打电话时得到的 toString() .

    也许你该打电话来 getString() 用一些列索引代替。