2011-12-06 83 views
0

我希望我的listitem仍然響應onListItemClick(),但另外我想要一個額外的圖像在一個原始的它有自己的點擊事件。經典的方式,爲圖像定義onClickListener不工作。什麼I have found is that I have to define my own listadapter and override getView() method。我鎖定了定義新的適配器類,我從來沒有使用過自定義適配器。我想通過標籤導航,onListItemClick()去標籤1和ImageClick去標籤2.它適用於onListItemClick()但是當我在MySimpleAdapter.getView與NullPointerException異常添加第二個點擊事件的圖像應用程序崩潰()。任何幫助將不勝感激。謝謝。onListItemClick()+ ListView上的可點擊圖像

public class ListaActivity extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list); 

    MySimpleAdapter adapter = new MySimpleAdapter(this, elem, R.layout.list_item, 
      new String[]{"name","number","address"}, new int[]{R.id.name,R.id.num,R.id.adr}); 
    setListAdapter(adapter); 
    } 
    public class MySimpleAdapter extends SimpleAdapter{ 
    Context context; 
    //Activity activity; 
public MySimpleAdapter(Context context, ArrayList<HashMap<String, String>> elements,int layout, String [] from, int [] to){ 
     super(context,el,layout,from,to); 
      this.context = context; 
      //this.activity = (Activity) context; 
    } 

    @Override 
    public View getView(int position,View convertView, ViewGroup parent){ 
     View view = super.getView(position, convertView, parent); 
     ImageView image = (ImageView)findViewById(R.id.btn); 
     image.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       int tab=2; 
       MyTabActivity.switchToTab(tab); 
      } 
     }); 
     return view; 
    } 


@Override 
protected void onListItemClick(ListView l, View v, int position, long id){ 
    int tab = 1; 
    MyTabActivity.switchToTab(tab); 
    } 
} 
+0

後堆棧跟蹤,也你在哪裏定義標籤? – user370305

回答

1

以下行更改從:

ImageView image = (ImageView)findViewById(R.id.btn); 

ImageView image = (ImageView)view.findViewById(R.id.btn); 
+0

您的解決方案似乎很好地工作:d你能告訴我,如果我也延長'CursorAdapter'代替SimpleAdapter'的'?我的意思是如果我唯一需要做的改變是在適配器的構造函數中? 'new SimpleCursorAdapter(this,R.layout.item_list,cursor,FROM,TO);'謝謝! – AlexAndro

+0

按我的理解,你可以擴展的類MySimpleAdapter與SimpleCursorAdapter,然後重寫一樣的東西休息,嘗試,如果這可以幫助您。 – Maneesh