我希望创建一个ListView,其中列表中的每个项目都应包含textview和旁边的imageview。单击textView应该转到另一个活动,然后单击imageview / imagebutton应该播放音频文件。
这是我到目前为止编写的代码的相关部分:
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(整个活动的布局)文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="@drawable/changedback">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="0dp" android:layout_weight="1"
android:textFilterEnabled="true"
android:background="@android:color/transparent"
android:cacheColorHint="#00000000">
</ListView> </LinearLayout>
emptylist.xml(listview中各个项目的布局)文件是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight" android:background="@android:color/transparent">
<TextView android:id="@+id/tvList" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:layout_alignParentLeft="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerVertical="true"
android:background="@android:color/transparent"/>
<ImageView
android:id="@+id/ivSpeak"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"
android:layout_alignParentRight="true"
android:focusable="false"
android:background="@android:color/transparent"/>
</RelativeLayout>
ivSpeak.setOnClickListener(新的OnClickListener()给我一个空指针异常。请帮忙。我是Android新手
答案 0 :(得分:0)
所有视图都有ClickListener绑定函数setOnClickListener()
。我假设您可以执行以下操作:
ImageView img = new ImageView(context);
img.setOnClickListener(new OnClickListener() {
public void onClick() {
// Your action
}
}
答案 1 :(得分:0)
最终,我自己想出来了。我必须编写一个扩展simplecursoradapter的自定义适配器,并在我的初始代码中使用它而不是simplecursor适配器。之后它开始工作,不知道究竟是什么原因。如果有人可以解释它会很棒。
这是新自定义适配器的代码
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;
}
}