2012-07-16 63 views
1

我想知道如何使用setonItemclick監聽器方法來獲取可展開列表視圖的子值。 我的代碼如下,但點擊事件工作不正常。如何在android中獲取可擴展列表視圖的子值?

mExpandableListView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View v, int groupPosition, 
       long id) { 
      // TODO Auto-generated method stub 
      int itemType = ExpandableListView.getPackedPositionType(groupPosition); 
      Log.i("item type",""+itemType); 
      if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
        childPosition = ExpandableListView.getPackedPositionChild(id); 
        groupPosition = ExpandableListView.getPackedPositionGroup(id); 
        Log.i("child",""+ childPosition); 
        Log.i("child Group",""+ groupPosition); 
       } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
        groupPosition = ExpandableListView.getPackedPositionGroup(id); 
        Log.i("Group",""+ groupPosition); 
       } 
     } 
    }); 

回答

3

有沒有必要嘗試手動獲取職位。使用android定義的方法。

這是獲取子位置和父位置的方法

public class MainActivity extends Activity { 

    private ExpandableListView mExpandableListView; 
    private List<GroupEntity> mGroupCollection; 
    private int childPosition; 
     private int groupPosition; 
     boolean retVal; 

     ExpandableListAdapter adapter; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 
     prepareResource(); 
     initPage(); 


     /*mExpandableListView.setOnItemLongClickListener(new OnItemLongClickListener() { 
      @Override 
       public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
        int itemType = ExpandableListView.getPackedPositionType(id); 

      if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
         childPosition = ExpandableListView.getPackedPositionChild(id); 
         groupPosition = ExpandableListView.getPackedPositionGroup(id); 
         Log.i("child",""+ childPosition); 
         Log.i("child Group",""+ groupPosition); 
         //do your per-item callback here 
         return retVal; //true if we consumed the click, false if not 
        } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
         groupPosition = ExpandableListView.getPackedPositionGroup(id); 
         //do your per-group callback here 
         Log.i("Group",""+ groupPosition); 
         return retVal; //true if we consumed the click, false if not 
        } else { 
         // null item; we don't consume the click 
         return false; 
        } 
      } 
      });*/ 


    } 


    private void prepareResource() { 

     mGroupCollection = new ArrayList<GroupEntity>(); 

     for (int i = 1; i < 7; i++) { 
      GroupEntity ge = new GroupEntity(); 
      if (i == 1) 
       ge.Name = "Basic Info"; 
      if (i == 2) 
       ge.Name = "Acadamic Background"; 
      if (i == 3) 
       ge.Name = "Experience"; 
      if (i == 4) 
       ge.Name = "Skill"; 
      if (i == 5) 
       ge.Name = "Objective"; 
      if (i == 6) 
       ge.Name = "Reference"; 
      for (int j = 1; j < 8; j++) { 
       GroupItemEntity gi = ge.new GroupItemEntity(); 
       if (i == 1 && j == 1) 
        gi.Name = "FirstName"; 
       if (i == 1 && j == 2) 
        gi.Name = "MiddleName"; 
       if (i == 1 && j == 3) 
        gi.Name = "LastName"; 
       if (i == 1 && j == 4) 
        gi.Name = "Address"; 
       if (i == 1 && j == 5) 
        gi.Name = "Sex"; 
       if (i == 1 && j == 6) 
        gi.Name = "Merital stutas"; 
       if (i == 1 && j == 7) 
        gi.Name = "Date Of Birthday"; 
       ge.GroupItemCollection.add(gi); 
      } 
      mGroupCollection.add(ge); 
     } 

    } 

    private void initPage() { 
     mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 
     adapter = new ExpandableListAdapter(this, 
       mExpandableListView, mGroupCollection); 
     mExpandableListView.setAdapter(adapter); 
     registerForContextMenu(mExpandableListView); 
    } 

    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { 
     System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition); 

     return true; 
    } 

} 
+0

這種方法也不能properlly..please工作給你想用exp清單,這個例子 – ckpatel 2012-07-16 10:55:45

+0

我想要得到的子ID以及組ID .. – 2012-07-16 10:58:01

+0

要獲取的記錄完整的例子 – ckpatel 2012-07-16 10:59:17

1

爲了得到這個功能

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);   
ExpandList = (ExpandableListView) findViewById(R.id.exp_list); 
      ExpListItems = SetStandardGroups(); 
      ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems); 
      ExpandList.setAdapter(ExpAdapter); 

      ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

       @Override 
       public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
        //get the cuurrnt clicked child object 
        Child child = (Child) ExpAdapter.getChild(groupPosition, childPosition); 
        //System.err.println("child clicked"); 
        Intent intent = new Intent(MainActivity.this, jai_ganesh.class); 
        intent.putExtra("message", child.getName());// get the child name and sent to next activity 
        startActivity(intent); 
        // Toast.makeText(getApplicationContext(), child.getName()+" child clicked", Toast.LENGTH_SHORT).show(); 
        return true; 
       } 
      }); 
} 

在上面的代碼中,你得到孩子可擴展列表視圖 調用的子元素的值通過創建它的對象來創造價值,因爲它具有許多價值。

Child child = (Child) ExpAdapter.getChild(groupPosition, childPosition); 

如果你的孩子是唯一的字符串或TextView的,在這種情況下,你可以得到價值像

final String selected = (String) expListAdapter.getChild(
groupPosition, childPosition); 

我希望這能解決這個問題。

0

嘗試......

mExpandableListView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int groupPosition, 
      long id) { 
     // TODO Auto-generated method stub 
     // add this code 
     String child = (String) listAdapter.getChild(groupPosition, childPosition); 
     int itemType = ExpandableListView.getPackedPositionType(groupPosition); 
     Log.i("item type",""+itemType); 
     if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
       childPosition = ExpandableListView.getPackedPositionChild(id); 
       groupPosition = ExpandableListView.getPackedPositionGroup(id); 
       Log.i("child",""+ childPosition); 
       Log.i("child Group",""+ groupPosition); 
       // to get value of child 
       Log.i("child Name",+ child.toString()); 
      } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
       groupPosition = ExpandableListView.getPackedPositionGroup(id); 
       Log.i("Group",""+ groupPosition); 
      } 
    } 
}); 

希望這可以幫助..!

相關問題