2016-05-04 3 views
0

Я хочу показать ExpandableListAdapter в моей активности Android. Так что я построить этот код в main_activity:Как установить onClickListener на ExpandibleListAdapter

protected void onCreate(Bundle savedInstanceState) { 
     try{ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.results_activity); 

     expListView = (ExpandableListView) findViewById(R.id.lvExp); 

     ResultDAO manager = new ResultDAO(this); 

     listAdapter = new ResultExpandableListAdapter(this, lista, listChildData); 

    expListView.setAdapter(listAdapter); 

    expListView.setOnGroupClickListener(new OnGroupClickListener() { 

     @Override 
    public boolean onGroupClick(ExpandableListView parent, View v, 
         int groupPosition, long id) { 
      Toast.makeText(getApplicationContext(), 
      "Group Clicked " + lista.get(groupPosition), 
      Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    }); 

     // Listview Group expanded listener 
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() { 

     @Override 
     public void onGroupExpand(int groupPosition) { 
      Toast.makeText(getApplicationContext(), 
       lista.get(groupPosition) + " Expanded", 
       Toast.LENGTH_SHORT).show(); 
     } 
    }); 

      // Listview Group collasped listener 
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { 

       @Override 
       public void onGroupCollapse(int groupPosition) { 
        Toast.makeText(getApplicationContext(), 
          lista.get(groupPosition) + " Collapsed", 
          Toast.LENGTH_SHORT).show(); 

       } 
    }); 

      // Listview on child click listener 
    expListView.setOnChildClickListener(new OnChildClickListener() { 

       @Override 
       public boolean onChildClick(ExpandableListView parent, View v, 
              int groupPosition, int childPosition, long id) { 
        // TODO Auto-generated method stub 
        Toast.makeText(
          getApplicationContext(), 
          lista.get(groupPosition) 
            + " : " 
            + listChildData.get(
            "a").get(
            childPosition), Toast.LENGTH_SHORT) 
          .show(); 
        return false; 
       } 
    }); 
     }catch(Exception e){ 
      System.out.println("pippo"); 
     } 
    } 

Теперь, если я пытаюсь запустить мое приложение, я могу видеть всю группу, но если я пытаюсь нажать на одной детали, никогда не вызывается метод, то я могу» см. дочернюю строку.

EDIT Это ExpandableListAdapter

public class ResultExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<Result> _listDataHeader; // header titles 

    private HashMap<String, List<ResultXResult>> _listDataChild; 

    public TextView startDate, endDate,examination; 
    public TextView parameter, value,interpretation,description,minMax; 

    public ResultExpandableListAdapter(Context context, List<Result> listDataHeader, 
           HashMap<String, List<ResultXResult>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 

     final ResultXResult result = (ResultXResult) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.results_x_result_list_item, null); 
     } 

     parameter = (TextView) convertView.findViewById(R.id.parameter); 
     value = (TextView) convertView.findViewById(R.id.valueUm); 
     interpretation = (TextView) convertView.findViewById(R.id.interpretation); 
     minMax= (TextView) convertView.findViewById(R.id.minMax); 
     description = (TextView) convertView.findViewById(R.id.description); 

     parameter.setText(result.getInfo().getDisplayName()); 
     value.setText(result.getValue()+" "+result.getValueRanceDescription()); 
     interpretation.setText(result.getInterpretationCode().getDisplayName()); 
     minMax.setText(result.getValueRangeMin()+"/"+result.getValueRangeMax()); 
     description.setText(result.getValueRanceDescription()); 

     //txtListChild.setText(childText); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     Result result = (Result) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.results_list_row, null); 
     } 

     startDate = (TextView) convertView.findViewById(R.id.startDate); 
     startDate.setText(result.getDateStart()); 
     endDate = (TextView) convertView.findViewById(R.id.endDate); 
     examination = (TextView) convertView.findViewById(R.id.examination); 

     startDate.setText(result.getDateStart()!=null ? result.getDateStart() : ""); 
     endDate.setText(result.getDateEnd()!=null ? result.getDateEnd() : ""); 
     examination.setText(result.getInfo().getDisplayName()); 

     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
} 
+0

Вы уверены, что getChildrenCount() возвращает значение, отличное от 0 – manolodewiner

+0

Нет, и это странно, потому что у меня вставить контрольную точку в каком-то методе в качестве getChildId, getChildView, getChildrenCount, и когда я запускаю приложение, этот метод не называется – bircastri

ответ

0

Для расширяет BaseExpandableListAdapter в вашем ExpandableAdapter

// that your ResultExpandableListAdapter 

private class ExpandableAdapter extends BaseExpandableListAdapter { 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return childrenData.get(groupPosition).get(childPosition); 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return 0; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    TextView text = null; 
    if (convertView != null) { 
     text = (TextView)convertView; 
     text.setText(childrenData.get(groupPosition).get(childPosition)); 
    } else { 
     text = createView(childrenData.get(groupPosition).get(childPosition)); 
    } 
    return text; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return childrenData.get(groupPosition).size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return groupData.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return groupData.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return 0; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    TextView text = null; 
    if (convertView != null) { 
     text = (TextView)convertView; 
     text.setText(groupData.get(groupPosition)); 
    } else { 
     text = createView(groupData.get(groupPosition)); 
    } 
    return text; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

private TextView createView(String content) { 
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( 
      ViewGroup.LayoutParams.FILL_PARENT, 38); 
    TextView text = new TextView(list_tempat.this); 
    text.setLayoutParams(layoutParams); 
    text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
    text.setPadding(40, 0, 0, 0); 
    text.setText(content); 
    return text; 
    } 
} 

private void showMessage(String message) { 
Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 
} 

более подробную информацию refer

0

Вы звоните getAdapter. Изменение, что в onCreate для:

expListView.getExpandableListAdapter(listAdapter); 
+0

, метод getExpandableListAdapter не принимает никаких входных параметров. – bircastri

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