Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7532333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:25:16+00:00 2026-05-30T05:25:16+00:00

I wish to create a ListView, where each item in the list should contain

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:25:18+00:00Added an answer on May 30, 2026 at 5:25 am

    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

        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;
            }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wish create a role for each user after that the user authenticate(login) to
I wish to create object of one class into another I have 2 classes
I wish to create top level folders corresponding to different projects. Each project folder
I wish to create a content rule for an event such that after expiry
I wish to create a very simple web app that simply displays an Image(s)
I wish to create a dynamic RSS feed to represent my site's content. Currently,
I wish to create own events and dispatch them. I never done this before
I wish to create a zip archive of my target directory (${project.build.directory). using the
I wish to create a java method that returns an array of type: ABCout,
So as the title suggests I wish to create a new dom element (the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.