2014-11-04 2 views
0

У меня вопрос, потому что если я нажму на дочерний элемент в ExpandableListView, щелкните по нему, чтобы открыть новую активность с тем же именем, то есть, если я нажму на элемент «Бутелка», ведет меня к классу деятельности «Бутелька» и по аналогии.ExpandableListView и нажмите на дочерний элемент в Android

public class MyListAdapter extends BaseExpandableListAdapter { 

     private Context context; 
     private ArrayList<Alphabet> alphabetList; 
     private ArrayList<Alphabet> originalList; 

     public MyListAdapter(Context context, ArrayList<Alphabet> alphabetList){ 
      this.context = context; 
      this.alphabetList = new ArrayList<Alphabet>(); 
      this.alphabetList.addAll(alphabetList); 
      this.originalList = new ArrayList<Alphabet>(); 
      this.originalList.addAll(alphabetList); 
     } 
     @Override 
     public int getGroupCount() { 
      // TODO Auto-generated method stub 
      return alphabetList.size(); 
     } 

     @Override 
     public int getChildrenCount(int groupPosition) { 
      // TODO Auto-generated method stub 
      ArrayList<Waste> wasteList = alphabetList.get(groupPosition).getWasteList(); 

      return wasteList.size(); 
     } 

     @Override 
     public Object getGroup(int groupPosition) { 
      // TODO Auto-generated method stub 
      return alphabetList.get(groupPosition); 
     } 

     @Override 
     public Object getChild(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 
      ArrayList<Waste> wasteList = alphabetList.get(groupPosition).getWasteList(); 
      return wasteList.get(childPosition); 
     } 

     @Override 
     public long getGroupId(int groupPosition) { 
      // TODO Auto-generated method stub 
      return groupPosition; 
     } 

     @Override 
     public long getChildId(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 
      return childPosition; 
     } 

     @Override 
     public boolean hasStableIds() { 
      // TODO Auto-generated method stub 
      return true; 
     } 

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, 
       View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      Alphabet alphabet = (Alphabet) getGroup(groupPosition); 
      if(convertView == null) 
      { 
       LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = layoutInflater.inflate(R.layout.group_row, null); 
      } 
      TextView heading = (TextView) convertView.findViewById(R.id.heading); 
      heading.setText(alphabet.getName().trim()); 

      return convertView; 
     } 

     @Override 
     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      Waste waste = (Waste) getChild(groupPosition, childPosition); 
      if(convertView == null){ 
       LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = layoutInflater.inflate(R.layout.child_row, null); 
      } 


      TextView name = (TextView) convertView.findViewById(R.id.name);  
      name.setText(waste.getName().trim()); 

      return convertView; 
     } 

     @Override 
     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      // TODO Auto-generated method stub 
      return true; 
     } 

     public void filterData(String query) 
     { 
      query = query.toLowerCase(); 
      Log.v("MyListAdapter", String.valueOf(alphabetList.size())); 
      alphabetList.clear(); 

      if(query.isEmpty()) 
      { 
       alphabetList.addAll(originalList); 
      } else { 
       for(Alphabet alphabet: originalList) 
       { 
        ArrayList<Waste> wasteList = alphabet.getWasteList(); 
        ArrayList<Waste> newList = new ArrayList<Waste>(); 
        for(Waste waste: wasteList) 
        { 
         if(waste.getName().toLowerCase().contains(query)){ 
          newList.add(waste); 
         } 
        } 
        if(newList.size() > 0) 
        { 
         Alphabet nAlphabet = new Alphabet(alphabet.getName(), newList); 
         alphabetList.add(nAlphabet); 
        } 
       } 
      } 
      Log.v("MyListAdapter", String.valueOf(alphabetList.size())); 
      notifyDataSetChanged(); 
     } 
    } 

Перед изменением я использовал этот код

String classNames[] = {"Aerozol"}; 
    ListView myList; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.segregate_waste_activity); 
      myList = (ListView)findViewById(android.R.id.list); 

      setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,classNames)); 
    } 

    @Override 
    protected void onListItemClick(ListView lv, View v, int position, long id){ 
     super.onListItemClick(lv, v, position, id); 
     String openClass = classNames[position]; 
     try{ 
      Class selected = Class.forName("com.odpad.odpadygdansk.waste." + openClass); 
      Intent selectedIntent = new Intent(this, selected); 
      startActivity(selectedIntent); 
     }catch(ClassNotFoundException e){ 
      e.printStackTrace(); 
     } 
    }    

У вас есть идея использовать этот код в текущей версии?

+0

В чем проблема с этим кодом? Это работает? – greenapps

+0

'Перед изменением'. Какое изменение? – greenapps

ответ

0

Вы можете установить слушателя для каждого ребенка прямо из адаптера. Что-то вроде этого:

@Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     Waste waste = (Waste) getChild(groupPosition, childPosition); 
     if(convertView == null){ 
      LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(R.layout.child_row, null); 
     } 


     TextView name = (TextView) convertView.findViewById(R.id.name); 
     name.setText(waste.getName().trim()); 

     // Click listener 
     convertView.setOnClickListener(new View.onClickListener() { 
      String openClass = waste.getName().trim(); 
      try{ 
       Class selected = Class.forName("com.odpad.odpadygdansk.waste." + openClass); 
       Intent selectedIntent = new Intent(this, selected); 
       ((Activity)getContext()).startActivity(selectedIntent); 
      }catch(ClassNotFoundException e){ 
       e.printStackTrace(); 
      } 
     }); 


     return convertView; 
    } 
Смежные вопросы