2013-01-21 72 views
0

ListView未在onClick事件中調用。未針對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

如果使isEnabled方法的返回false適配器將禁用所有行和OnItemClickListener不會被觸發。另外,你不應該在自定義視圖中插入適配器的邏輯,使適配器成爲一個單獨的類,並擴展其中一個具體的類(如BaseAdapter,ArrayAdapter等),而不是實現ListAdapter接口。

+0

確定..謝謝 公共布爾areAllItemsEnabled(){返回true;} \t公共布爾的IsEnabled(INT位置){返回true;} – Nagi