2

im really newbie android. Now i try to create a simple custom listview with Header section. But after i run my program, the listview item isn't clickable. Here is my code :

MainActivity.java

package io.hidayat.headerlistview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;


public class MainActivity extends Activity {


    private CustomAdapter mAdapter;
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.list_header_detail);

        mAdapter = new CustomAdapter(this);
        mAdapter.addSectionHeaderItem("Section #1");
        for (int i = 1; i < 30; i++) {
            mAdapter.addItem("Row Item #" + i);
            if (i % 4 == 0) {
                mAdapter.addSectionHeaderItem("Section #" + i);
            }
        }

        listView.setAdapter(mAdapter);
    }
}

CustomAdapter.java

package io.hidayat.headerlistview;

import java.util.ArrayList;
import java.util.TreeSet;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

class CustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;

    private ArrayList<String> mData = new ArrayList<String>();
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

    private LayoutInflater mInflater;

    public CustomAdapter(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSectionHeaderItem(final String item) {
        mData.add(item);
        sectionHeader.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int rowType = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();
            switch (rowType) {
            case TYPE_ITEM:
                convertView = mInflater.inflate(R.layout.row, parent, false);
                holder.textView = (TextView) convertView.findViewById(R.id.text);
                break;
            case TYPE_SEPARATOR:
                convertView = mInflater.inflate(R.layout.header, parent, false);
                holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(mData.get(position));

        return convertView;
    }

    public static class ViewHolder {
        public TextView textView;
    }

}

Header and Row XML is simillar.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:descendantFocusability="blocksDescendants" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:gravity="center_vertical"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FF000000" />

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list_header_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="16dip"
        android:paddingBottom="16dip"
        android:clipToPadding="false"
        android:divider="@null"/>   

</LinearLayout>

I'm sory, but here the screenshoot of my aplication : screenshoot

3
  • 2
    where have you written the listitem click code ? Did you try setting onItemClickListener for the listview ? Commented May 20, 2015 at 3:55
  • 1
    actually i'm not yet add code for on click listener. i'm just wondering why list item that i touch didn't marked like it clicked. Because when i follow this tutorial when i touch the item it marked. Commented May 20, 2015 at 3:59
  • 1
    set onitemclicklistener on your list and check This Post to make it proper working. Commented May 20, 2015 at 4:17

3 Answers 3

4

You have not applied the OnItemClickListener. Thats the problem. No worries. You can see the example below to change your code as required.

First implement your Class with OnItemClickListener like

public class MainActivity extends Activity implements OnItemClickListener

Then attach the OnItemClickListener to your list

listView.setOnItemClickListener(YourClassName.this);

Then override the onItemClick method to implement your functionality at the position being clicked.

@Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub
        String item = adapter.getItemAtPosition(position).toString();
        Toast.makeText(Test.this, "CLICK: " + item, Toast.LENGTH_SHORT).show();
    }

Please let me know, if you face any trouble. Thanks.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Shaik, your code is work for an on ClickListener. Thank you so much. but do you know why the item that i touch didn't marked like i clicked, the color is always same like i never click the item.
My pleasure. Please accept my answer, if you are satisfied.
Off course, it done. :) but please help me why the item that i touch didn't marked like i clicked, the color is always same like i never click the item.
You can refer to Esteam's answer in stackoverflow.com/questions/16976431/…
1

invoke the below method on your listview

 listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();

        }
    });

1 Comment

hi dora, thanks for your help, but Shaik give me more detail how to do it. Your answer is also help me. but i still wondering why the item didn't marked like it clicked, the color is always same like never clicked
1

The row item must have a param like android:descendantFocusability="blocksDescendants". use this for your row item parent .. It works perfectly for a listview that has CustomAdapter..

2 Comments

hi RajaReddy i already add that param in my XML for row item. Look at my attached code please..
you have to add onItemClickListener to your list view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.