2011-05-05 76 views
2

我正在使用MyExpandableListAdapter,它擴展了 BaseExpandableListAdapter並且我在我的適配器中實現了BaseExpandListList的所有方法,我創建了MyExpadableListActivity來擴展ExpandableListActivity。 MyExpandableListAdapter是MyExpadableListActivity的內部類。 我正在使用xml視圖來添加子視圖和分組UI(過去在下面)。 當我運行應用程序時,它的顯示效果很好。 我的問題是這個,我重寫MyExpadableListActivity ExpandableListActivity onChildClick(....)的方法。當我運行我的代碼它不能去onChildClick方法。 我怎麼能實現onChildClick(...)方法使用這種情況或其他建議.. 希望有人指出我做錯了什麼.... 請感謝如果有人指出我可以如何實現解決方案。 。Android ExpandableListVIew使用BaseExpandableListAdapter

public class MyExpadableListActivity extends ExpandableListActivity implements 
    ExpandableListView.OnChildClickListener { 
    ExpandableListAdapter mAdapter; 
    Context mContext; 
    static final String groups[] = { "Chats", "Contacts (289)", 
     "Group Chat (7)", "e-Card (137)", "Pending (37)", 
     "Bookmarks" }; 

static final String children[][] = { 
     { "ListofChats" }, 
     { "Group", "groupt01", "groupt02", "groupt03" }, 
     { "Groupd 01", "", "Group 02", "", "Group 03", "" }, 
     { "E-Card-01", "", "E-Card-02", "", 
       "Business Card Received", "" }, 
     { " Req", "", " Req", "", " Req", "" } }; 

private static final String TAG = "Hookup"; 
XMPPConnection connection = SignInMainActivity.connection; 

@Override 
public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);     
    mAdapter = new MyExpandableListAdapter(this); 
    setListAdapter(mAdapter); 
    registerForContextMenu(getExpandableListView());   
} 
@Override 
public boolean onChildClick(ExpandableListView parent, View v, 
     int groupPosition, int childPosition, long id) { 
    // TODO Auto-generated method stub 
    System.out.println("Insidded onContextItemSelected"); 
    return super.onChildClick(parent, v, groupPosition, childPosition, id); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    menu.setHeaderTitle("Hookup Menu"); 
    menu.add(0, 0, 0, R.string.hello); 
} 

public boolean onContextItemSelected(MenuItem item) { 
    System.out.println("Insidded onContextItemSelected"); 

    Log.i(TAG, "onContextItemSelected"); 
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item 
      .getMenuInfo(); 

    String title = ((TextView) info.targetView).getText().toString(); 
    int type = ExpandableListView 
      .getPackedPositionType(info.packedPosition); 

    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
     int groupPos = ExpandableListView 
       .getPackedPositionGroup(info.packedPosition); 
     int childPos = ExpandableListView 
       .getPackedPositionChild(info.packedPosition); 
     Toast.makeText(this,title + ": Child " + childPos + " clicked in     group " + groupPos, Toast.LENGTH_SHORT).show(); 
     return true; 
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
     int groupPos = ExpandableListView 
       .getPackedPositionGroup(info.packedPosition); 
     Toast.makeText(this, title + ": Group " + groupPos + " clicked", 
       Toast.LENGTH_SHORT).show(); 
     return true; 
    } 
    return false; 
} 

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
    Context mContext; 

    public MyExpandableListAdapter() { 
     // TODO Auto-generated constructor stub 
    } 

    public MyExpandableListAdapter(Context context) { 
     mContext = context; 

    } 

    public Object getChild(int groupPosition, int childPosition) { 
     Log.i(TAG, "getChild"); 
     return children[groupPosition][childPosition]; 
    } 

    public long getChildId(int groupPosition, int childPosition) { 
     Log.i(TAG, "getChildId"); 
     return childPosition; 
    } 

    public int getChildrenCount(int groupPosition) { 
     Log.i(TAG, "getChildId"); 
     return children[groupPosition].length; 
    } 

    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     Log.i(TAG, "getChildView"); 

     LayoutInflater layoutInflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = null; 

     TextView tt = null; 
     if (groupPosition == 4) { 
      v = layoutInflater.inflate(R.layout.button_group, null); 
     } else { 
      v = layoutInflater.inflate(R.layout.album_row, null); 
      tt = (TextView) v.findViewById(R.id.text1); 

      String myText = this.getChild(groupPosition, childPosition) 
        .toString(); 

      tt.setText(myText); 
      CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox); 
      cb.setVisibility(View.VISIBLE); 

      if (groupPosition == 0) { 
       ImageView icon = (ImageView) v.findViewById(R.id.rowicon); 
       icon.setImageResource(R.drawable.add_picture); 
      } else { 
       ImageView icon = (ImageView) v.findViewById(R.id.rowicon); 
       icon.setImageResource(R.drawable.add_picture); 

      } 
     } 

        return v; 
    } 

    public Object getGroup(int groupPosition) { 
     Log.i(TAG, "getGroup"); 
     return groups[groupPosition]; 
    } 

    public int getGroupCount() { 
     Log.i(TAG, "getGroupCount"); 
     return groups.length; 
    } 

    public long getGroupId(int groupPosition) { 
     Log.i(TAG, "getGroupId"); 
     return groupPosition; 
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     TextView groupTitle = null; 
     ImageView imgDot; 
     LayoutInflater layoutInflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = null; 
     v = layoutInflater.inflate(R.layout.button_group, null); 
     groupTitle = (TextView) v.findViewById(R.id.TextView01); 
     String myText = this.getGroup(groupPosition).toString(); 
     groupTitle.setText(myText); 
     imgDot = (ImageView) v.findViewById(R.id.ImageView01); 
     imgDot.setVisibility(View.VISIBLE); 
     return v; 
    } 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     Log.i(TAG, "isChildSelectable"); 
     return true; 
    } 
    public boolean hasStableIds() { 
     Log.i(TAG, "hasStableIds"); 
     return true; 
    } 
    public void registerDataSetObserver(DataSetObserver observer) { 
    } 
} // closing of MyExpandableListAdapter 
} // closing of MyExpadableListActivity 

我的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:minHeight="?android:attr/listPreferredItemHeight" 
android:gravity="center_vertical" 
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
android:paddingRight="?android:attr/scrollbarSize" 
android:layout_weight="1" android:background="#fafafa"> 
<ImageView android:id="@+id/rowicon" android:layout_width="wrap_content" 
android:layout_height="fill_parent" android:layout_marginRight="6dip" /> <TextView 
android:id="@+id/text1" 
android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_weight="1" 
/> 
<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:layout_marginRight="2dip" 
android:focusable="true" android:clickable="true" android:gravity="center_vertical" 
android:orientation="vertical" android:duplicateParentState="true" 
android:visibility="visible" 
android:text="myTest" 
/> </LinearLayout> 

<?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:minHeight="?android:attr/listPreferredItemHeight" 
android:gravity="center_vertical" 
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
android:paddingRight="?android:attr/scrollbarSize" 
android:layout_weight="1" android:background="#fafafa"> 

+0

檢查此問題:http://stackoverflow.com/questions/2367936/listview-onitemclicklistener-not-responding – Flo 2011-05-05 11:52:21

回答

2

您忘記註冊setOnChildClickListener(...),它不能去 onChildClick()方法。

編輯1:你只需要添加一個行onCreate方法:

@Override 
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);     
    mAdapter = new MyExpandableListAdapter(this); 
    setListAdapter(mAdapter); 
    getExpandableListView().setOnChildClickListener(this); //This should fix issue. 
    registerForContextMenu(getExpandableListView()); //ContextMenu event override onChildClick and onGroupClick. Remove this line  
} 

編輯2:嘗試this鏈接。如果它沒有幫助你,我建議你不要使用BaseExpandableListAdapter。使用SimpleExpandableListAdapter重寫4個方法:getChildView(),getGroupView(),getChildrenCount()和getGroupCount()。一切都應該很好。

並且不要忘記寫評論。如果這沒有幫助,我會嘗試尋找更多的想法。

+0

如果這可以幫助你,請不要忘記標記答案:) – Pasha 2011-05-05 12:30:59

+0

嗨@Pasha感謝您的快速回復,但我的問題仍然在這裏。你建議我使用這個ExpandableListView list = this.getExpandableListView(); \t \t int childCount = list.getChildCount(); \t \t System.out.print(「Child Count」+ childCount); \t \t list.setOnChildClickListener(新OnChildClickListener(){ \t \t \t @覆蓋 \t \t \t公共布爾onChildClick(ExpandableListView父母,視圖V, \t \t \t \t \t INT groupPosition,INT childPosition,長ID){ \t \t \t \t System.out。打印( 「測試」); \t \t \t \t // TODO自動生成方法存根 \t \t \t \t返回假; \t \t \t} \t \t}); – aftab 2011-05-05 12:35:00

+0

我也遵循這個網站鏈接http://stackoverflow.com/questions/2512436/how-to-handle-android-simpleexpandablelistadapter-entry-選擇,但同樣的問題控制仍然沒有進入onChildClick方法。 – aftab 2011-05-05 13:07:58

4

BaseExpandableListAdapter是一個功能:public boolean isChildSelectable(int groupPosition, int childPosition)。該函數返回默認false。將其更改爲:返回true。你的onChildClickListener將被調用!

+0

親愛的理查德我用其他方式解決我的問題。我創建了一個動態擴展列表,而不使用xml佈局,它工作正常。如果您閱讀我的代碼,那麼您可以看到我也寫作爲> public boolean isChildSelectable(int groupPosition,int childPosition){Logging.i(TAG,「isChildSelectable」)) ; 返回true; },意思是我已經做了你的建議,但現在我的第二種方法工作正常.. – aftab 2011-05-26 15:38:00

+0

你添加了什麼? – Mikey 2011-07-21 15:41:48

+0

嗨mikedroid如果你想解決方案,我可以給,因爲在這裏在stackoverflow我不能ablve過去我的完整代碼..我會看到任何方法,如果我可以通過我的代碼在這邊這邊.please ping me tonasirinbox(at)gmail (點)com – aftab 2011-07-24 12:18:16

相關問題