I wish to create a ListView, where each item in the list should contain a textview and an imageview next to it. Clicking on the textView should take me to another activity and clicking on the imageview/imagebutton should play an audio file.
This is the relevant portion of the code written by me so far:
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);
}
The main.xml (layout for the entire activity) file is:
<?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>
The emptylist.xml (the layout for the individual items in the listview) file is:
<?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>
The ivSpeak.setOnClickListener(new OnClickListener() is giving me a null pointer exception. Kindly help. I am quite new to Android
Eventually, I figured it out myself. I had to write a custom adapter extending simplecursoradapter and use it instead of the simplecursor adapter in my initial code. After that it starts working, not sure what is the exact reason though. It would be great if someone can explain it.
This is the code for the new custom adapter