未触发ListView的OnClick事件

时间:2013-01-21 16:40:42

标签: android android-listview

ListView未在onClick事件中调用。

我想有人必须对我做错了什么有一个公平的想法。以及制作listView的正确方法。

public class CardapioCategoriaSection extends BaseSection implements ListAdapter {  


    public int getCount() {
        return result.size();
    }

    public Object getItem(int position) {
        return result.get(position);
    }

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

    public int getItemViewType(int position) {
        return 1;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) 
        {
            convertView = l_Inflater.inflate(R.layout.cardapio_categorias_cell_section, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView.findViewById(R.id.title);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txt_itemName.setText(result.get(position).get("title"));
        return convertView;
    }

    static class ViewHolder {
        TextView txt_itemName;
        TextView txt_itemDescription;
        TextView txt_itemPrice;
        ImageView itemImage;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean hasStableIds() {return false;}

    public boolean isEmpty() {return false; }

    public void registerDataSetObserver(DataSetObserver observer) { }

    public void unregisterDataSetObserver(DataSetObserver observer) {}

    public boolean areAllItemsEnabled() {return false;}

    public boolean isEnabled(int position) {return false;}

    //METHOD CONSTRUCTOR
    public CardapioCategoriaSection( Context context, AttributeSet attrs, int defStyle )
    {
        super( context, attrs, defStyle );
    }

    public CardapioCategoriaSection( Context context, AttributeSet attrs )
    {
        super( context, attrs );
    }

    public CardapioCategoriaSection( Context context )
    {
        super( context );
    }

    private static EasyDBLines result;

    private LayoutInflater l_Inflater;

    @Override
    protected void onFinishInflate() {
        // TODO Auto-generated method stub
        super.onFinishInflate();

        /*
        Typeface tf = Typeface.createFromAsset(getAssets(),
                                "fonts/BPreplay.otf");
                        TextView tv = (TextView) findViewById(R.id.CustomFontText);
                        tv.setTypeface(tf);
        */
        result = AppContent.getInstance().selectCardapioCategorias();

        l_Inflater = LayoutInflater.from(MainPhone.getContext());   

        final ListView listViewCategoria = (ListView) findViewById(R.id.listV_main);     

        listViewCategoria.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a , View v, int position, long id) {
            System.out.print("Now it works");
            }
        });


        listViewCategoria.setAdapter( this);
    }

}  

1 个答案:

答案 0 :(得分:1)

如果您使isEnabled方法返回false,则适配器将禁用所有行,并且不会触发OnItemClickListener

此外,您不应在自定义视图中插入适配器的逻辑,使适配器成为单独的类并扩展其中一个具体类(如BaseAdapterArrayAdapter等)而不是实现ListAdapter接口。