我已经经历了与此相关的在本网站上的各种问题搜索,但我未能解决这个问题我收到。
我想从数据库ID上点击列表视图项目
这是我的类别类:
包com.example.reminders;
进口的java.util.List;
进口android.app.ListActivity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.Toast;
公共类分类扩展ListActivity {
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
DBAdapter DB =新DBAdapter(Categories.this);
db.open();
名单<字符串> CS = db.getAllCategoriesList();
setListAdapter(新ArrayAdapter<字符串>(这一点,R.layout.activity_categories,CS));
ListView控件的ListView = getListView();
listView.setTextFilterEnabled(真正的);
listView.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
//单击时,显示出与TextView的文字敬酒
光标CUR =(光标)parent.getItemAtPosition(位置);
Toast.makeText(getApplicationContext(),
ID:+编号+的位置:+位置+ROWID:+ cur.getInt(cur.getColumnIndex(_ ID)),Toast.LENGTH_LONG).show();
}
});
db.close();
}
}
在DBAdapter类中定义的
getAllCategoriesList功能是:
// ---检索所有类别数据---
公开名单<字符串> getAllCategoriesList()
{
的String []列=新的String [] {KEY_NAME2};
光标C = db.query(DATABASE_TABLE2,列,NULL,NULL,NULL,NULL,
KEY_NAME2);
//字符串结果=;
名单<字符串>结果=新的ArrayList<字符串>();
INT ICM = c.getColumnIndex(KEY_NAME2);
为(c.moveToFirst();!c.isAfterLast(); c.moveToNext()){
results.add(c.getString(ICM));
}
返回结果;
}
当我跑的例子code以下错误来了:
10-01 15:05:22.507:E / AndroidRuntime(20846):java.lang.ClassCastException:java.lang.String中不能转换为android.database.Cursor
解决方案
你是不是从数据库(仅KEY_NAME2列)查询_id,所以你不能从适配器得到它。
这行:
光标CUR =(光标)parent.getItemAtPosition(位置);
是完全错误的。您正在试图施放一个String(这是由返回 ArrayAdapter<字符串>
来一个光标,它不能正常工作
您所要做的,就是用的CursorAdapter
(或 SimpleCursorAdapter
)为您的ListView。光标应该查询至少_id和KEY_NAME2。
通过这个适配器的getItem(INT位置)
将返回一个指针设置为要求的位置。然后,所有你需要做的就是 cursor.getInt(cursor.getColumnIndex(_ ID))
和你的存在。
I have searched through various questions related to this on this website but i am unable to resolve the issue i am getting.
I want to get id from database on click of listview item
This is my categories class:
package com.example.reminders;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Categories extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBAdapter db = new DBAdapter(Categories.this);
db.open();
List<String> cs = db.getAllCategoriesList();
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_categories,cs));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Cursor cur = (Cursor) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"id:"+id+"position:"+position+"rowid:"+cur.getInt(cur.getColumnIndex("_id")), Toast.LENGTH_LONG).show();
}
});
db.close();
}
}
getAllCategoriesList function defined in the DBAdapter class is :
//---retrieves all the category data---
public List<String> getAllCategoriesList()
{
String[] columns = new String[] {KEY_NAME2};
Cursor c = db.query(DATABASE_TABLE2, columns, null, null, null, null,
KEY_NAME2);
// String results = "";
List<String> results = new ArrayList<String>();
int iCM = c.getColumnIndex(KEY_NAME2);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results.add(c.getString(iCM));
}
return results;
}
When i ran the example code the following error comes:
10-01 15:05:22.507: E/AndroidRuntime(20846): java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor
解决方案
You are not querying the _id from the database (only the KEY_NAME2 column), so you're not able to get it from the adapter.
This line:
Cursor cur = (Cursor) parent.getItemAtPosition(position);
is entirely wrong. You are trying to cast a String (which is returned by ArrayAdapter<String>
to a cursor, which can never work.
What you have to do, is use a CursorAdapter
(or SimpleCursorAdapter
) for your ListView. The cursor should query at least for _id and KEY_NAME2.
With this adapter the getItem(int position)
will return a cursor set to requested position. Then all you need to do is cursor.getInt(cursor.getColumnIndex("_id"))
and you're there.