我使用这个代码从数据库&将其直接显示给正确显示的自定义列表视图。这就是我使用SimpleCursorAdapter
的方法public void queryArticles(String querytext,Context mContext)
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor cur=null;
String strquery="select rowid _id,part,A_name,italic,AS_name,Desc_art from Articles where AS_name LIKE '%"+querytext+"%'";
cur=db.rawQuery(strquery,null);
if(cur!=null&&cur.getCount() > 0)
{
do
{
String[] fromColumns = { "part", "A_name", "italic", "AS_name" };
int[] toViews = { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(mContext,R.layout.query_cell, cur, fromColumns, toViews, 0);
QueryFragment.listview1.setAdapter(adapter);
}
while(cur.moveToNext());
}
}
我试着让它点击这个
QueryFragment.listview1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
在while循环中,在if条件之外,也进入到我的活动中但是它给了我这个异常。
07-29 13:24:22.970: E/AndroidRuntime(25608): FATAL EXCEPTION: main
07-29 13:24:22.970: E/AndroidRuntime(25608): java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.widget.AdapterView.setOnClickListener(AdapterView.java:780)
07-29 13:24:22.970: E/AndroidRuntime(25608): at com.vivekwarde.indianconstitution.QueryFragment.onCreateView(QueryFragment.java:48)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.os.Handler.handleCallback(Handler.java:615)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.os.Looper.loop(Looper.java:137)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.app.ActivityThread.main(ActivityThread.java:4794)
07-29 13:24:22.970: E/AndroidRuntime(25608): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 13:24:22.970: E/AndroidRuntime(25608): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 13:24:22.970: E/AndroidRuntime(25608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-29 13:24:22.970: E/AndroidRuntime(25608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-29 13:24:22.970: E/AndroidRuntime(25608): at dalvik.system.NativeStart.main(Native Method)
我这样使用setOnItemClickListener
listview1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.v("hi","not getting called ? ");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
但是仍然没有触发setOnItemSelectedListener事件,有什么想法,如何解决这个问题?
你的问题在这里
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,.....
改成
listview1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//do your task
}
}
这里position
将帮助您找到选中的项目..所以没有必要在选中和未选中中流失人才。
试试这个方法,希望能帮助你解决你的问题
ListView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
转轮
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
任何视图,如(按钮,TextView,ImageView等)
yourView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});