2013-04-17 30 views
0

在我的Android应用程序,我有一个正常的少数listview使用String [],但我有一个从SQLite数据库的列表视图。它表明它很好,这不是问题。此问题已记录我按它的选择,因为它只是似乎登录[email protected]????,当我使用举杯显示字符串从SQLite填充列表视图中的选择作为字符串

我不得不代码做什么,我想用选项名称

做所以如果有人能够帮助我将选项名称存储在一个字符串中并用于烤面包,这将非常有帮助...因为这是我测试代码的方式。由于

我发布,填补下方如果使用列表视图SQLite的值

final ListView list = (ListView) findViewById(R.id.listView1); 
@SuppressWarnings("deprecation") 
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, //context 
    android.R.layout.simple_list_item_1, db.getValues(), //Cursor 
    new String[] {"SocietyName"}, new int[] { 
     android.R.id.text1 
    }); 
list.setAdapter(adapter); 
list.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 
    String favname = (String)((Cursor) list).getString(position); 
    Toast.makeText(getApplicationContext(), favname, Toast.LENGTH_SHORT).show(); 
    } 
}); 

MyDBAdapter.java帮助

FavouritesScreen.java ... ListView控件的代码......得到的值从SQLite数据库

public Cursor getValues() { 
    if (!isOpen()) { 
    open(); 
    } 
    System.out.println("3a"); 
    Cursor mCursor = db.query(true, "Favourites", // table name 
    new String[] { 
    "_id", "SocietyName" 
    }, // select clause 
    null, // where clause 
    null, // where clause parameters 
    null, // group by 
    null, // having 
    null, // order by 
    null); // limit 
    System.out.println("4a"); 
    return mCursor; 
} 

回答

1

list是一个ListView不是光标(我已经期待抛出异常。)

String favname = (String) ((Cursor) list).getString(position); 

首先提取光标,然后调用getString()对你有兴趣在列:

String favname = ((Cursor) list.getItemAtPosition(position)).getString(columnIndex); 
/* I assume the column you want is 1 */ 
+0

非常感谢'columnIndex'与什么关系? – user2291530

+0

游标包含尽可能多的列,你使用'new String [] {“_id”,“SocietyName”}'。如果你想使用'getString(1)'显示''SocietyName'''数据,所以'columnIndex'是你的String Array中列的索引。 – Sam

+0

完美,非常感谢:) – user2291530

0

android.database.sqlite.sqlitecursor @ ????”

String favname = (String) ((Cursor) list).getString(position); 

是def。不给你一个字符串,而是一个SQLLite游标。而是使用游标对象来获取所需的字符串。

您可能应该握住与该列表关联的Adapter对象,然后询问游标,然后询问该位置上的内容。

或者更好的是,使用这个家伙:

public void onItemClick(AdapterView<?> arg0 

的适配器视图对象。询问'arg0'对象的位置。

Toast.makeText(getApplicationContext(), arg0.getItemAtPosition(position), Toast.LENGTH_SHORT).show();