简体   繁体   中英

Cannot click listview android with custom adapter

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

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.

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();

        }
    });

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM