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 6212415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:30:41+00:00 2026-05-24T06:30:41+00:00

I have an Android app with a ListView. I’ve created my own item layout

  • 0

I have an Android app with a ListView. I’ve created my own item layout that has a CheckBox, followed by two TextViews and a Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dip"></CheckBox>
    <RelativeLayout android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_width="match_parent">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentRight="true" android:text="Button"></Button>
        <TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_toLeftOf="@+id/button1" android:layout_alignTop="@+id/button1" android:layout_width="match_parent"></TextView>
        <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="TextView" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1" android:layout_alignRight="@+id/textView1"></TextView>
    </RelativeLayout>
</LinearLayout>

When the user taps on the row, I want different behaviour depending on if they tapped on the CheckBox, the Button or the row itself (i.e. anything other than the CheckBox or Button). Here’s what I’ve tried to implement so far (using setOnItemClickListener):

package com.camelconsultants.shoplist;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class HelloAndroid extends ListActivity implements OnItemClickListener
    {
        ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>();

        /** Called when the activity is first created. */
        @Override public void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate( savedInstanceState );

                GetData();

                setListAdapter
                    ( 
                        new SimpleAdapter
                            (
                                this.getBaseContext(),
                                Items,
                                R.layout.multiitem,
                                new String[]{ "Code", "Description" },
                                new int[]{ R.id.textView1, R.id.textView2 }
                            ) 
                    );

                ListView lv = getListView();
                lv.setTextFilterEnabled( true );

                lv.setOnItemClickListener( this );
            }

        public void onItemClick( AdapterView<?> parent, View view, int position, long id )
            {
                Toast.makeText
                    (
                        getApplicationContext(), 
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT 
                    ).show();
            }

        void GetData()
            {
                HashMap<String, String> Item;

                try
                    {
                        JSONObject JsonObject = new JSONObject( this.getResources().getString(R.string.Json) );
                        JSONArray JsonArray = JsonObject.getJSONArray( "Items" );

                        for ( int i = 0; i < JsonArray.length(); i++ ) 
                            {
                                Item = new HashMap<String, String>();
                                Item.put( "Code", JsonArray.getJSONObject(i).getString("Code") );
                                Item.put( "Description", JsonArray.getJSONObject(i).getString("Description") );

                                Items.add( Item );
                            }
                    }
                catch( JSONException Bummer )
                    {
                        Bummer.printStackTrace();
                    }
            }
    }

When I run the application and click on one of the TextViews, nothing happens!

What’s the best way to approach this?

  • 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-24T06:30:42+00:00Added an answer on May 24, 2026 at 6:30 am

    I found out that I could set a callback for a View in the xml definition. Here’s the updated xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/linearLayout1" android:clickable="true" android:onClick="onClick" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
        <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dip" android:onClick="onCheckBoxClick"></CheckBox>
        <RelativeLayout android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_width="match_parent">
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentRight="true" android:text="Button" android:onClick="onButtonClick"></Button>
            <TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_toLeftOf="@+id/button1" android:layout_alignTop="@+id/button1" android:layout_width="match_parent"></TextView>
            <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="TextView" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1" android:layout_alignRight="@+id/textView1"></TextView>
        </RelativeLayout>
    </LinearLayout>
    

    I’ve updated the code with the appropriate callback hooks:

    package com.camelconsultants.shoplist;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
    
    public class HelloAndroid extends ListActivity
        {
            ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>();
    
            /** Called when the activity is first created. */
            @Override public void onCreate(Bundle savedInstanceState) 
                {
                    super.onCreate( savedInstanceState );
    
                    GetData();
    
                    setListAdapter
                        ( 
                            new SimpleAdapter
                                (
                                    this.getBaseContext(),
                                    Items,
                                    R.layout.multiitem,
                                    new String[]{ "Code", "Description" },
                                    new int[]{ R.id.textView1, R.id.textView2 }
                                ) 
                        );
    
                    ListView lv = getListView();
                    lv.setTextFilterEnabled( true );
                }
    
            @SuppressWarnings("unchecked") 
            private HashMap<String, String> getItem( View view )
                {
                    HashMap<String, String> Item = null;
    
                    ListView listView = getListView();
    
                    int Position = listView.getPositionForView( view );
    
                    if ( listView.getItemAtPosition(Position) instanceof HashMap )
                        Item = (HashMap<String, String>)listView.getItemAtPosition( Position );
    
                    return Item;
                }
    
            public void onClick( View view )
                {
                    HashMap<String, String> Item = getItem( view );
                    if ( Item == null )
                        return;
    
                    Toast.makeText
                        (
                            getApplicationContext(), 
                            Item.get("Code") + ", " + Item.get( "Description" ),
                            Toast.LENGTH_SHORT 
                        ).show();
                }
    
            public void onCheckBoxClick( View view )
                {
                    HashMap<String, String> Item = getItem( view );
                    if ( Item == null )
                        return;
    
                    Toast.makeText
                        (
                            getApplicationContext(), 
                            "CheckBox! - " + Item.get("Code") + ", " + Item.get( "Description" ),
                            Toast.LENGTH_SHORT 
                        ).show();
                }
    
            public void onButtonClick( View view )
                {
                    HashMap<String, String> Item = getItem( view );
                    if ( Item == null )
                        return;
    
                    Toast.makeText
                        (
                            getApplicationContext(), 
                            "Button! - " + Item.get("Code") + ", " + Item.get( "Description" ),
                            Toast.LENGTH_SHORT 
                        ).show();
                }
    
            void GetData()
                {
                    HashMap<String, String> Item;
    
                    try
                        {
                            JSONObject JsonObject = new JSONObject( this.getResources().getString(R.string.Json) );
                            JSONArray JsonArray = JsonObject.getJSONArray( "Items" );
    
                            for ( int i = 0; i < JsonArray.length(); i++ ) 
                                {
                                    Item = new HashMap<String, String>();
                                    Item.put( "Code", JsonArray.getJSONObject(i).getString("Code") );
                                    Item.put( "Description", JsonArray.getJSONObject(i).getString("Description") );
    
                                    Items.add( Item );
                                }
                        }
                    catch( JSONException Bummer )
                        {
                            Bummer.printStackTrace();
                        }
                }
        }
    

    The getItem function returns the HashMap for the current item by passing up the View to getItemAtPosition. The cool thing about that is that it doesn’t matter whether the view is nested or not.

    However, I’m assuming that creating my own Adapter and using the setTag method is more efficient. But for the time being, the above code suits my purposes!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android-app with a listview in an activity. The listview has, if
My android app works on landscape mode. I have a listview that contains some
I'm developing a RSS reader android app. I have a ListView that shows my
I have an Android App that uses a TabHost layout. Every tab calls the
I have an Android app project that contains all of my code. I've made
I have an Android app that I had working a few months ago which
I have an Android app that begins by displaying a dialog. When I set
In my android app I have a Tabhost with a ListView as one of
I have a simple Android app that pulls all music files on the phone
I have created a Java code for my Android App. String[] MovieName=new String[]{}; for

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.