2015-10-16 40 views
0

我使用此代碼獲取Expandable List中的選定項並獲取其文本並將其打印在日誌中。但日誌顯示此[email protected]和我點擊的項目包含其他文字1。我想訪問此文本1這是以列表視圖顯示的。獲取對可擴展列表視圖中的子內容的訪問

ExpandList.setOnChildClickListener(new OnChildClickListener() 
{ 
    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, int group_position, int child_position, long id) 
    { 
     if(group_position==0 && child_position==0){ 

      startActivity(intent); 
      ExpandableListAdapter itemAdapter=parent.getExpandableListAdapter(); 
      String selectedItem=""+itemAdapter.getChild(group_position, child_position); 
      // String country = ""+ExpAdapter.getChild(group_position, child_position); 


      Log.i("ddd", ""+selectedItem); 




     } else if(group_position==2 && child_position==2){ 
     } 
     return false; 
    } 
}); 

所有代碼:

public class MainActivity extends Activity 
{ 
     /** Called when the activity is first created. */ 
    private ExpandListAdapter ExpAdapter; 
    private ArrayList<ExpandListGroup> ExpListItems; 
    private ExpandableListView ExpandList; 
    ArrayList<ExpandListGroup> list; 

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

     ExpandList = (ExpandableListView) findViewById(R.id.ExpList); 
     ExpListItems = SetStandardGroups(); 
     ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems); 
     ExpandList.setAdapter(ExpAdapter); 

     final Intent intent= new Intent (this, zero.class); 

     ExpandList.setOnChildClickListener(new OnChildClickListener() 
     { 
      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, int group_position, int child_position, long id) 
      { 
       if(group_position==0 && child_position==0){ 
        startActivity(intent); 
        ExpandableListAdapter itemAdapter=parent.getExpandableListAdapter(); 
        String selectedItem=""+itemAdapter.getChild(group_position, child_position); 
        //String country = ""+ExpAdapter.getChild(group_position, child_position); 


        Log.i("ddd", ""+selectedItem); 
       } 
       else if(group_position==2 && child_position==2){ 
       } 
       return false; 
      } 
     }); 

     ExpandList.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_GROUP) { 

        int groupPosition = ExpandableListView.getPackedPositionGroup(id); 

        Log.i("s", "removepos"); 

        if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
         int childPosition = ExpandableListView.getPackedPositionChild(id); 
         } 
        list.remove(groupPosition); 

        ExpAdapter.notifyDataSetChanged();} 
        return true; 
       } 
      }); 
     } 

     public boolean onContextItemSelected1(MenuItem item) { 
      ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); 
      //String title = ((TextView) info.targetView).getText().toString(); 
      int type = ExpandableListView.getPackedPositionType(info.packedPosition); 

      if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
       int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
       removeGroup(groupPos); 
       ExpAdapter.notifyDataSetChanged(); 
      } 
      return false; 
     } 


     public ArrayList<ExpandListGroup> SetStandardGroups() { 
      list = new ArrayList<ExpandListGroup>(); 
      String[] listlist= new String[] {"1","2","3","4","5","6","7","8","9"}; 
      String[] names= new String[] {"one","two","three"}; 
      for (int i=0 ; i<3 ;i++) 
      { 
       ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>(); 

       ExpandListGroup gru1 = new ExpandListGroup(); 
       gru1.setName(names[i]); 

       for (int k=i*3 ; k<(i+1)*3 ;k++){ 
        ExpandListChild ch1_1 = new ExpandListChild(); 
        ch1_1.setName(listlist[i]); 
        ch1_1.setTag(null); 
        list2.add(ch1_1); 
       } 
       gru1.setItems(list2); 
       list.add(gru1); 
      } 
      return list; 
     } 


     public void dialog (long id) 
     { 
      final int position=(int) id; 
      //String number=list.get(position); 
      list.remove(position); 
      ExpAdapter.notifyDataSetChanged(); 
     } 

     public void removeGroup(int group) { 
      //TODO: Remove the according group. Dont forget to remove the children aswell! 
      Log.v("Adapter", "Removing group"+group); 
     } 
    } 
} 

回答

2

我認爲錯誤是在這裏

String selectedItem=""+itemAdapter.getChild(group_position, child_position); 

itemAdapter.getChild(...)有返回對象的類型。當你打印一個對象時,它將打印Object類的默認toString()實現。 Object.toString()

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

答:getChild(...)到適配器項目類型和使用它。

+0

如何將它轉換爲Adapter Item類型? –

+0

看看這個:[展開列表視圖教程](http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/) – krrish

+0

你必須覆蓋getchild(...)in您的自定義ExpandListAdapter。 – krrish

相關問題