2015-10-16 4 views
0

Я использовал этот код, чтобы получить выбранный элемент в расширяемом списке и получить его текст и распечатать его в журнале. но log показать это [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(...) имеет тип объекта возврата. Когда вы печатаете объект, он будет печатать стандартную реализацию toString() класса Object. Object.toString()

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

Ответ: закиньте getChild(...) к типу адаптера товара и использовать его.

+0

как применить его к адаптеру Тип элемента? –

+0

Проверьте это: [Развернуть список просмотра учебника] (http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/) – krrish

+0

Вы должны переопределить getchild (...) в ваш пользовательский ExpandListAdapter. – krrish

Смежные вопросы