2016-02-12 104 views
1

我有一個ListView顯示沒有extends ListActivity。我想啓用setOnItemClickListener。這是我的onCreate自定義ListView setOnItemClickListener

adapter = new CustomAdapter(this, MyDB); 
     setListAdapter(adapter); 

     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, 
            long id) { 

       Toast.makeText(getBaseContext(), "test", Toast.LENGTH_LONG).show(); 

      } 

下代碼,我用下面的代碼,以使ListView

protected ListView getListView() { 
     if (mListView == null) { 
      mListView = (ListView) findViewById(android.R.id.list); 
      empty = (TextView) findViewById(android.R.id.empty); 
      mListView.setEmptyView(empty); 
     } 
     return mListView; 
    } 

    protected void setListAdapter(ListAdapter adapter) { 
     getListView().setAdapter(adapter); 
    } 

然而,setOnItemClickListener不工作。

+0

您是否收到任何錯誤? – Rohit5k2

+0

你的listView是如何創建的?有沒有像按鈕裏面的一些意見? – Opiatefuchs

+0

沒有錯誤,我根本拿不到麪包。 – user2872856

回答

2

如果使用內點擊按鈕,我認爲,最好的辦法是不使用AdapterView.OnItemClickListener但通常onClick事件。 或使用android:descendantFocusability="blocksDescendants"我希望你的情況。

0

也許你在xml佈局中設置onClickListener - 刪除它。

您可以指定onClickListener每個列表項目視圖不ListView.setOnItemClickListener

0

您可以設置OnClickListener在適配器像這個 -

public class DeviceList_Adapter extends BaseAdapter implements View.OnClickListener 
{ 
/************* CustomAdapter Constructor *****************/ 
public DeviceList_Adapter(Activity a, ArrayList d) 
{ 

    /********** Take passed values **********/ 
    activity = a; 
    data=d; 

    /*********** Layout inflator to call external xml layout() ***********/ 
    inflater = (LayoutInflater)activity. 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 

public int getCount() { 

    if(data.size()<=0) 
     return 1; 
    return data.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

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

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

    View vi = convertView; 
    ViewHolder holder; 

    if(convertView==null){ 

     /****** Inflate listitem_device.xml file for each row (Defined below) *******/ 
     vi = inflater.inflate(R.layout.listitem_device, null); 

     /******** Set Item Click Listner for LayoutInflater for each row *******/ 

     vi.setOnClickListener(new OnItemClickListener(position)); 
    } 
    return vi; 
} 

@Override 
public void onClick(View v) { 
    Log.v("CustomAdapter", "=====Row button clicked====="); 
} 

/********* Called when Item click in ListView ************/ 
private class OnItemClickListener implements View.OnClickListener { 
    private int mPosition; 

    OnItemClickListener(int position){ 
     mPosition = position; 
    } 

    @Override 
    public void onClick(View arg0) 
    { 
     MainActivity mainActivity = (MainActivity)activity; 

     /**** Call onItemClick Method inside CustomListViewAndroidExample Class (See Below)****/ 

     mainActivity.onItemClick(mPosition); 
    } 
} 
} 

有一個名爲onItemClick()在活動方法,其中計劃使用列表視圖。每次在列表視圖上單擊一行時,都會調用此方法。

相關問題