我正在开发一个Android项目而且卡住了。我试图从ListView获取一个项目,但我得到的是关于项目在我创建的数据库中的位置的信息,而不是项目本身。
这是我的代码:
public class Map extends Activity {
Cursor cursor;
ListView listView;
String categoria = "livro";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
final CitySQL dbHelper = new CitySQL(Map.this, null, null, 1);
final SQLiteDatabase db = dbHelper.getWritableDatabase();
final String[] camposDb = {"city", BaseColumns._ID};
cursor = db.query("cities", camposDb, null, null, null, null, "city ASC");
int[] camposView = new int[] {android.R.id.text1};
@SuppressWarnings("deprecation")
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(Map.this,
android.R.layout.two_line_list_item, cursor, camposDb, camposView);
listView= (ListView)findViewById(R.id.city_list);
listView.setAdapter(adapter);
final Geocoder geocoder=new Geocoder(this, Locale.ENGLISH);
listView.setOnItemClickListener(new OnItemClickListener(){
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onItemClick(AdapterView<?> parent, View view, int posicao,long id) {
final String morada = listView.getAdapter().getItem(posicao).toString();
Toast.makeText(Map.this, morada, Toast.LENGTH_LONG).show();
我做了一个Toast,看看我点击时到底发生了什么,但我的目标是获取项目(城市名称)并寻找位置。
答案 0 :(得分:0)
更改
final String morada = listView.getAdapter().getItem(posicao).toString();
到
Cursor cursor = (Cursor) listView.getAdapter().getItem(posicao);
String morada = cursor.getString(cursor.getColumnIndex("column_name_for_city"));