问题描述
我无法得到吐司消息为 onItemClick 永远不会被调用。登录猫不显示任何错误。请看看我的code和纠正我,如果我错了任何地方。我已经使用阵列适配器。
公共类开放式扩展ListActivity {
DbAdapter MDB =新DbAdapter(本); @覆盖
保护无效的onDestroy(){
// TODO自动生成方法存根
mDb.close();
super.onDestroy();
} / **当第一次创建活动调用。 * /
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.open); ListView控件的listContent =(ListView控件)findViewById(android.R.id.list); mDb.open();
光标光标= mDb.fetchAllPrograms();
startManagingCursor(光标); 的String [] =由新的String [] {} DbAdapter.KEY_TITLE;
INT []为= INT新[] {} R.id.textViewName; SimpleCursorAdapter的CursorAdapter =新SimpleCursorAdapter(这一点,
R.layout.row,光标,从,到);
listContent.setAdapter(CursorAdapter的); // ONCLICK的ListView使用setListener
listContent.setTextFilterEnabled(真);
的listContent = getListView();
listContent.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司,查看 看来,INT位置,长的id){
Toast.makeText(getApplicationContext(),
加工!,
Toast.LENGTH_LONG).show();
}
}); }
}
Row.xml如下:
<?XML版本=1.0编码=UTF-8&GT?;
<的TextView
机器人:ID =@ + ID / textViewName
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_marginTop =10dp
机器人:layout_marginBottom =10dp
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =25sp
机器人:文字颜色=#0000FF
机器人:textIsSelectable =真
/>
我跑成一个类似的问题。不仅是我的onItemClick方法被忽略了,也是我的列表选择不工作(的android:listSelector
)
原来这是的android:textIsSelectable
我的TextView行元素属性。尝试将它设置为假
。
I am unable to get Toast message as onItemClick is never called. Log-cat doesn't show any error. Please check out my code and correct me, if I am going wrong anywhere. I have used array adapter.
public class Open extends ListActivity {
DbAdapter mDb = new DbAdapter(this);
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mDb.close();
super.onDestroy();
}
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open);
ListView listContent = (ListView) findViewById(android.R.id.list);
mDb.open();
Cursor cursor =mDb.fetchAllPrograms();
startManagingCursor(cursor);
String[] from = new String[] { DbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.textViewName };
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
//Onclick ListView setlistener
listContent.setTextFilterEnabled(true);
listContent=getListView();
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View
view,int position, long id) {
Toast.makeText(getApplicationContext(),
"Working!",
Toast.LENGTH_LONG).show();
}
});
}
}
Row.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/textViewName"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#0000FF"
android:textIsSelectable="true"
/>
I was running into a similar issue. Not only was my onItemClick method being ignored, but also my List Selector wasn't working (android:listSelector
.)
Turns out it was the android:textIsSelectable
attribute on my TextView row elements. Try setting it to false
.
这篇关于onItemClick ListView中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!