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

  • Home
  • SEARCH
  • 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 9242823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:40:01+00:00 2026-06-18T08:40:01+00:00

Is there a way to get the selected item’s header text (from that section)

  • 0

Is there a way to get the selected item’s header text (from that section) on click?

With this code i can get the selected item text but I also want to know in which header that item is clicked.

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
    System.out.println( "CLICKED LOCATION: " + 
        ((TextView) v.findViewById(R.id.list_content1)).getText().toString() );
}

I create my headers and row dynamically depending on the data that downloaded and saved in a TreeMap containing a Key and another Treemap that contains a Key and a String ( TreeMap<String, TreeMap<String, String>> ).

Then i loop both Maps to get the headers and rows:

public void setLocationsCompanyHashMap(TreeMap<String, TreeMap<String, Object>> map)
    {
        companyMap = map;

        List<Item> items = new ArrayList<Item>();

        Iterator<?> it = companyMap.entrySet().iterator();
        while (it.hasNext()) {


            Map.Entry pairs = (Map.Entry)it.next();
            items.add(new Header(null, (String) pairs.getKey()));

            Iterator<?> ite = ((Map<String, Object>) pairs.getValue()).entrySet().iterator();
            while (ite.hasNext()) {
                Map.Entry in_pairs = (Map.Entry)ite.next();
                items.add(new ListItem(null, (String) in_pairs.getKey()));
            }

        }

        LocationsArrayAdapter adapter = new LocationsArrayAdapter(getActivity(), items);
        setListAdapter(adapter);
    }

This is my xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cacheColorHint="@android:color/transparent" 
    android:choiceMode="singleChoice"/>

header.java:

import android.view.LayoutInflater;

public class Header implements Item {

    private final String         name;

    public Header(LayoutInflater inflater, String name) {
        this.name = name;
    }

    @Override
    public int getViewType() {
        return RowType.HEADER_ITEM.ordinal();
    }

    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        View view;
        if (convertView == null) {
            view = (View) inflater.inflate(R.layout.header, null);
            // Do some initialization
        } else {
            view = convertView;
        }

        TextView text = (TextView) view.findViewById(R.id.separator);
        text.setText(name);

        return view;
    }

}

location_header.xml:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" android:scrollbars="none"
    style="?android:attr/listSeparatorTextViewStyle" />

Listitem.java

import android.view.LayoutInflater;

public class ListItem implements Item {
    private final String         str1;
    private final LayoutInflater inflater;

    public ListItem(LayoutInflater inflater, String text1) {
        this.str1 = text1;
        this.inflater = inflater;
    }

    @Override
    public int getViewType() {
        return RowType.LIST_ITEM.ordinal();
    }

    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        View view;
        if (convertView == null) {
            view = (View) inflater.inflate(R.layout.locations_list_item, null);
            // Do some initialization
        } else {
            view = convertView;
        }

        TextView text1 = (TextView) view.findViewById(R.id.list_content1);
        text1.setText(str1);

        return view;
    }

}

location_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/listitem_background">

    <TextView
        android:id="@+id/list_content1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="5dip"
        android:clickable="false"
        android:gravity="center"
        android:longClickable="false"
        android:paddingBottom="1dip"
        android:paddingTop="1dip"
        android:textColor="#28465a"
        android:textSize="17sp"
        android:textStyle="bold" 
        />


</LinearLayout>

Interface Item:

import android.view.LayoutInflater;

public interface Item {
    public int getViewType();
    public View getView(LayoutInflater inflater, View convertView);
}

LocationsArrayAdapter.java:

import java.util.List;

public class LocationsArrayAdapter extends ArrayAdapter<Item> {

    private LayoutInflater mInflater;

    public enum RowType {
        LIST_ITEM, HEADER_ITEM
    }

    private List<Item> items;

    public LocationsArrayAdapter(Context context, List<Item> items) {
        super(context, 0, items);
        this.items = items;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getViewTypeCount() {
        return RowType.values().length;

    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position).getViewType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return items.get(position).getView(mInflater, convertView);
    }

}

In my activity i just add a new Fragment (the LocationViewFragment.java)

getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.viewLayout, fragment)
        .commit();
  • 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-18T08:40:02+00:00Added an answer on June 18, 2026 at 8:40 am

    Ok , The easiest solution I could think of giving the above code is to tag The Item Object to the TextView object that is related to it.
    The code would be like:

    text1.setText(str1);
    text1.setTag(this);
    

    and on the onListItemClick:

    Header header = (Header) v.getTag();
    

    but I strongly suggest you to set the getView method inside the adapter itself instead of calling it from the children views.
    and keep all the items in an array inside it , so you can use the onListItemClick position value to access the Header you want directly

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

Sidebar

Related Questions

is there a more simple way to get combobox (WinForms) selected items text? string
is there any way to get the selected item in a qmenu ? i
Is there way to get file from windows xp command prompt? I tried to
is there way how to get name ov event from Lambda expression like with
Is there a way to get the item pointed at by an iterator in
Is there a way to get HTML contents from TinyMCE editor using jQuery so
I have a CheckBoxList in my page.is there any way to get all selected
Is there a way to get just the selected items of a document library
Is this any way to find selected record key value of selected item of
Is there a way to explicitly capture a click on a TreeView item? Currently,

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.