getSingleEntry始终返回0

时间:2014-06-21 10:46:16

标签: android sqlite

我有两个表剧院[id,descr]和theatre_tables [id,id_theatre,descr]的SQLite DB。我取了点击项目的名称,但我也想要id。对于id,我调用getSinlgeEntry(item),但它总是返回0.是否有人发现问题?

Menu.java

public void onItemClick( AdapterView<?> adapter, View arg1, int position, long arg3) {

    String item = adapter.getItemAtPosition(position).toString();
    int item_id=MenuDBAdapter.getSinlgeEntry(item);
    Toast.makeText(getApplicationContext(), "Theatre " + item_id, Toast.LENGTH_SHORT).show();


     userMenuDBAdapter = new UserMenuDBAdapter(this);
     userMenuDBAdapter=userMenuDBAdapter.open();



     AlertDialog.Builder alertDialog = new AlertDialog.Builder(Menu.this);
     LayoutInflater inflater = getLayoutInflater();
     View convertView = (View) inflater.inflate(R.layout.menu, null);
     alertDialog.setView(convertView);
     alertDialog.setTitle("List");
     ListView lv = (ListView) convertView.findViewById(R.id.list);
     ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
                MenuDBAdapter.getTablesOf(item_id));
     lv.setAdapter(adapter2);
     alertDialog.show();    

}

MenuBDAdapter

public int getSinlgeEntry(String item){

            db = dbHelper.getReadableDatabase(); 
    Cursor cursor=db.query("THEATRE", null, " DESCR=?", new String[]{item}, null, null, null);



    if(cursor.getCount()<1) // if Not Exist
    {
        cursor.close();
        return 7;
    }
    cursor.moveToFirst();
    int id= cursor.getColumnIndex("ID");
    cursor.close();
    return id;              
}

1 个答案:

答案 0 :(得分:1)

int id= cursor.getColumnIndex("ID");

您正在检索显然为0的列索引。

更改为

int id= cursor.getInt(cursor.getColumnIndex("ID"));

获得那里的价值。