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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:30:21+00:00 2026-06-02T21:30:21+00:00

I have a class that extens a listview. I need to have onitemclicklistener on

  • 0

I have a class that extens a listview. I need to have onitemclicklistener on that listview. The following code is not working. I mean the on click listener is not working. How to solve this?

listview.xml

 <ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="47dip"
android:layout_marginTop="47dip" >
</ListView>

elements.xml

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:gravity="center" >

        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="ToggleButton"
            android:textOff="Inactive"
            android:textOn="Active" />

        <TextView
            android:id="@+id/label_note"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="38dp"
            android:layout_marginLeft="2dip"
            android:layout_toRightOf="@+id/check_note"
            android:text="@+id/label"
            android:textSize="25px" />

        <CheckBox
            android:id="@+id/check_note"
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:visibility="gone"
            android:button="@drawable/customdrawablecheckbox" />

    </RelativeLayout>

List.java

public class List extends ListActivity{
....

public void onCreate(Bundle icicle) {

        this.setContentView(R.layout.listview);
        super.onCreate(icicle);



}
ArrayAdapter<ListModel> adapter = new InteractiveArrayAdapter(
                this, getModel(), false);
        setListAdapter(adapter);

        final ListView list1 = getListView();
        list1.setItemsCanFocus(true);
        list1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                // TODO Auto-generated method stub
            //  HashMap<String, String> o = (HashMap<String, String>) list1.getItemAtPosition(pos);                   
                Toast.makeText(ContactsNoteList.this, "ID '" +list.get(pos).getName()+" "+list.get(pos).getId().toString() + "' was clicked.", Toast.LENGTH_LONG).show(); 

            }

        });

..}

    private List<ListModel> getModel() {


    for (int i=0;i<3;i++)       
list.add(get("Elem"+i.toString(),i);
            }
        }

        return list;
    }

    private ListModel get(String s1,int Id) {
        return new ListModel(s1,Id);
    }

InteractiveArrayAdapter.java

InteractiveArrayAdapter extends
        ArrayAdapter<ListModel> {

    private final List<ListModel> list;
    private final Activity context;
    private boolean visibility;
    public InteractiveArrayAdapter(Activity context, List<ListModel> list,boolean visibility) {
        super(context, R.layout.elements, list);
        this.context = context;
        this.list = list;
        this.visibility=visibility;

    }


    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.elements, null);


            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label_note);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check_note);
            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {



@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                                ListModel element = (ListModel) viewHolder.checkbox.getTag();
                                element.setSelected(buttonView.isChecked());

                            }
                        });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        if (visibility==true)
        holder.checkbox.setVisibility(View.VISIBLE);
        else
            holder.checkbox.setVisibility(View.INVISIBLE);
        return view;
    }
}

Need help. Thx. Appreciate

  • 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-06-02T21:30:27+00:00Added an answer on June 2, 2026 at 9:30 pm

    Use:

       view.setOnClickListener(new OnClickListener() {          
                @Override
                public void onClick(View v) {... 
                      Toast.makeText(context,"you clicked item: "+position,Toast.Lenght_LONG).show();
                      //code you want to execute on click of list item...
                }
       });
       return view;
    

    in your adapter class.

    EDIT changed sample code…

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

Sidebar

Related Questions

I have a activity that fills a ListView, and it is working. Because I
I have a string-array, that i need to display in a layout. The code
I have class that extend FragmentActivity in it I add fragment to layout as
I have a class that extends AsyncTask , which fetches the gps cordinates from
I have a class that extends Application. The class is fairly extensive. When the
I have a class that extends View. I override the onDraw method and allow
I have a class that extends android.app.Dialog, the layout is done in an xml
i have a class that extends another. when i iterate the current object i
I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I have a class that extends JPanel. On the panel I have added an

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.