Android ListView中TextView和ImageButton上的独立侦听器
我希望创建一个ListView,列表中的每个项目都应该包含一个textview和一个imageview。单击文本视图应将我带到另一个活动,单击imageview/imagebutton应播放音频文件 这是迄今为止我编写的代码的相关部分:Android ListView中TextView和ImageButton上的独立侦听器,android,android-listview,Android,Android Listview,我希望创建一个ListView,列表中的每个项目都应该包含一个textview和一个imageview。单击文本视图应将我带到另一个活动,单击imageview/imagebutton应播放音频文件 这是迄今为止我编写的代码的相关部分: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
temp=mDbHelper.fetchAllNotes();
startManagingCursor(temp);
String[] from=new String[]{DbAdapter.KEY_TITLE};
int[] to= new int[]{R.id.tvList};
words = new SimpleCursorAdapter(Main.this, R.layout.emptylist, temp, from, to);
Main.this.setListAdapter(words);
ImageView ivSpeak=(ImageView) findViewById(R.id.ivSpeak);
ivSpeak.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main.this, "Hi", Toast.LENGTH_SHORT).show();
}
});}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i=new Intent(Main.this,WordDetail.class);
i.putExtra(DbAdapter.KEY_ROWID, id);
startActivity(i);
}
main.xml(整个活动的布局)文件为:
emptylist.xml(listview中各个项目的布局)文件为:
ivSpeak.setOnClickListener(new OnClickListener()给了我一个空指针异常。请帮助。我对Android非常陌生所有视图都有ClickListener绑定函数
setOnClickListener()
。我假设您可以执行以下操作:
ImageView img = new ImageView(context);
img.setOnClickListener(new OnClickListener() {
public void onClick() {
// Your action
}
}
最终,我自己找到了答案。我必须编写一个扩展simplecursoradapter的自定义适配器,并在初始代码中使用它而不是simplecursoradapter。之后它开始工作,但不确定确切原因是什么。如果有人能解释一下,那就太好了 这是新自定义适配器的代码
class MySimpleCursorAdapter extends SimpleCursorAdapter{
MySimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to){
super(context, layout, cursor, from, to);
}
public View getView(int position, View convertView,ViewGroup parent) {
View row=super.getView(position, convertView, parent);
ImageView ivSpeak=(ImageView) row.findViewById(R.id.ivSpeak);
ivSpeak.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main.this, "Hi", Toast.LENGTH_SHORT).show();
mp=MediaPlayer.create(Main.this, R.raw.correct);
mp.start();
}
});
return row;
}
}
你好,费耶斯。谢谢你的回复,但这不起作用。我已经编辑了这个问题。请看一看,如果可能的话帮助我。
ImageView img = new ImageView(context);
img.setOnClickListener(new OnClickListener() {
public void onClick() {
// Your action
}
}
class MySimpleCursorAdapter extends SimpleCursorAdapter{
MySimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to){
super(context, layout, cursor, from, to);
}
public View getView(int position, View convertView,ViewGroup parent) {
View row=super.getView(position, convertView, parent);
ImageView ivSpeak=(ImageView) row.findViewById(R.id.ivSpeak);
ivSpeak.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main.this, "Hi", Toast.LENGTH_SHORT).show();
mp=MediaPlayer.create(Main.this, R.raw.correct);
mp.start();
}
});
return row;
}
}