2017-03-16 67 views
0

我對我的特定程序有些懷疑,因爲它失敗了,我不明白爲什麼,我有一個ExpandableListView對象,它執行「Adapter」和一個調用Fragment這個對象。當顯示列表元素的作品時,問題出現在onChildClick中,我的意圖是,當按下列表中的某個項目時,它將保存在TextView中並顯示爲稍後使用。由於這是我的程序,現在它只在按下另一個元素時按下列表中的第一個元素,應用程序停止工作而不會給我一個錯誤。我能做些什麼來解決這個問題?非常感謝你。如何使用setOnChildClickListener顯示元素

這是ExpandibleList的目的,其中我打電話getChild

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader ; 
    private HashMap<String, List<String>> _listDataChild; 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
           HashMap<String, List<String>> 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 String childText = (String) getChild(groupPosition, childPosition); 

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

     TextView txtListChild = (TextView) convertView 
       .findViewById(R.id.lblListItem); 

     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) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.lblListHeader); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 

     return convertView; 
    } 

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

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

而這正是我想從列表中的項目。

public class MainActivityFragment extends Fragment { 

//Expandable list Adapter 
    ExpandableListAdapter listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader; 
    HashMap<String, List<String>> listDataChild; 

    //Users 
    private List <User> items = new ArrayList<>(); 

    //Obejeto a pasar 
    User user = new User(); 
    String password; 

@Override 
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, 
          Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.fragment_main, container, false); 

     //Botones 
     TextPass = (EditText) view.findViewById(R.id.IdpasswordUser); 
     TextName = (TextView) view.findViewById(R.id.text1); 

     // get the listview 
     expListView = (ExpandableListView) view.findViewById(R.id.lvExp); 

     // preparing list data 
     prepareListData(); 

     listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 

     ExpandableListView ExList =(ExpandableListView) view.findViewById(R.id.lvExp) ; 



       //Llamamos al boton fichar 
     fichar = (Button) view.findViewById(R.id.fichar); 

//Call ExpandibleList view child on pressed 

     ExList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 

       ShowItem(listDataHeader.get(childPosition)); 

       TextName.setText(listAdapter.getChild(groupPosition,childPosition).toString()); 

       return false; 
      } 
     }); 

     fichar.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       password = TextPass.getText().toString(); 
       //SuperUsuario 
       if(key.equalsIgnoreCase("cesi.tic")&&password.equalsIgnoreCase("123456789")){ 
        ((MainActivity) getActivity()).EditarUsuario(); 
        fichar.setVisibility(view.INVISIBLE); 
        TextPass.setVisibility(view.INVISIBLE); 
        TextDNI.setVisibility(view.INVISIBLE); 
       }else{ 
        LeerFirebase(); 
       } 

      } 
     }); 
     return view; 
    } 
    public void ShowItem(String id) 
    { 
     // do something 
    } 
} 

我怎樣才能與setOnChildClickListener

回答

0

好吧顯示的元素,我已經解決了這個問題。我只需要刪除 // ShowItem(listDataHeader.get(childPosition));

所以onChildClick是這樣的:

expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 

       TextName.setText(listAdapter.getChild(groupPosition,childPosition).toString()); 

       return false; 
      } 
     });