2016-08-28 27 views
0

私はAndroidアプリを怒鳴り、拡張可能なリストビューを使用することに決めました。私は必要なすべてを持って、リストが正常に動作しているが、私はそれを移入する場合、グループが異なるため、彼らが必要以上にあるように見えるが、ここでは例です: UnexpandedExpandableListViewが正しい順序で表示されない

Expanded

あなたは、子の値を見ることができるように親の値だけがランダムな順序で表示されます。ここで私が持っている完全なコードは次のとおりです。

マイフラグメントのコードは:

public class BuildingsFragment extends Fragment { 


public BuildingsFragment() { 
    // Required empty public constructor 
} 


public static BuildingsFragment newInstance(Context context) { 
    BuildingsFragment fragment = new BuildingsFragment(); 

    return fragment; 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_buildings, container, false); 


    final ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListViewBuildings); 
    HashMap<String, List<String>> expandableListDetail = ExpandableListDataPump.getData(); 
    List<String> expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 
    ExpandableListAdapter expandableListAdapter = new CustomExpandableListAdapter(getContext(), expandableListTitle, expandableListDetail); 
    expandableListView.setAdapter(expandableListAdapter); 


    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 


      Fragment fragment = null; 
      Class fragmentClass = BuildingDetailsFragment.class; 



      try { 
       fragment = (Fragment) fragmentClass.newInstance(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      FragmentTransaction trans = getFragmentManager().beginTransaction(); 


      Bundle args = new Bundle(); 
      args.putString("BUILDING_NAME", "test"); 
      fragment.setArguments(args); 


      trans.replace(R.id.frameLayoutForFragments, fragment); 


      trans.addToBackStack(null); 


      trans.commit(); 


      return false; 
     } 
    }); 


    return view; 
} 
} 

マイカスタムアダプタ:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter { 

private Context context; 
private List<String> expandableListTitle; 
private HashMap<String, List<String>> expandableListDetail; 

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) { 
    this.context = context; 
    this.expandableListTitle = expandableListTitle; 
    this.expandableListDetail = expandableListDetail; 
} 

@Override 
public Object getChild(int listPosition, int expandedListPosition) { 
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) 
      .get(expandedListPosition); 
} 

@Override 
public long getChildId(int listPosition, int expandedListPosition) { 
    return expandedListPosition; 
} 

@Override 
public View getChildView(int listPosition, final int expandedListPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 
    final String expandedListText = (String) getChild(listPosition, expandedListPosition); 
    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_view_buildings_row_layout, null); 
    } 
    TextView expandedListTextView = (TextView) convertView 
      .findViewById(R.id.expandedListItem); 
    expandedListTextView.setText(expandedListText); 
    return convertView; 
} 

@Override 
public int getChildrenCount(int listPosition) { 
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) 
      .size(); 
} 

@Override 
public Object getGroup(int listPosition) { 
    return this.expandableListTitle.get(listPosition); 
} 

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

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

@Override 
public View getGroupView(int listPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String listTitle = (String) getGroup(listPosition); 
    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_view_buildings_group_layout, null); 
    } 
    TextView listTitleTextView = (TextView) convertView 
      .findViewById(R.id.listGroup); 
    listTitleTextView.setTypeface(null, Typeface.BOLD); 
    listTitleTextView.setText(listTitle); 
    return convertView; 
} 

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

@Override 
public boolean isChildSelectable(int listPosition, int expandedListPosition) { 
    return true; 
} 
} 

ExpandableListDataPump私が使用しています:

public class ExpandableListDataPump { 
public static HashMap<String, List<String>> getData() { 

    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>(); 


    List<String> abuildings = new ArrayList<String>(); 
    abuildings.add("A-1"); 
    abuildings.add("A-2"); 
    abuildings.add("A-3"); 
    abuildings.add("A-5"); 
    abuildings.add("A-7"); 
    expandableListDetail.put("A", abuildings); 

    List<String> bbuildings = new ArrayList<String>(); 
    bbuildings.add("B-1"); 
    bbuildings.add("B-2"); 
    bbuildings.add("B-3"); 
    bbuildings.add("B-5"); 
    bbuildings.add("B-6"); 
    expandableListDetail.put("B", bbuildings); 

    List<String> cbuildings = new ArrayList<String>(); 
    cbuildings.add("C-1"); 
    cbuildings.add("C-3"); 
    cbuildings.add("C-4"); 
    cbuildings.add("C-5"); 
    cbuildings.add("C-6"); 
    expandableListDetail.put("C", cbuildings); 

    List<String> dbuildings = new ArrayList<String>(); 
    dbuildings.add("D-1"); 
    dbuildings.add("D-2"); 
    dbuildings.add("D-3"); 
    dbuildings.add("D-5"); 
    dbuildings.add("D-7"); 
    expandableListDetail.put("D", dbuildings); 

    return expandableListDetail; 
} 
} 

を、私はそれがすべてだと思いますExpandableListViewに関連しています。私は本当にHashMapを残したいと思いますが、それ以外の方法がない場合、TreeMapに変更して、それをソート関数(最後の手段)として使用できますか?手伝ってくれてありがとう。

P.S.私はこのチュートリアル(それが重要ならば、私は知りません)、次のされた私のExpandableListViewをしている間:HashMap用のJava仕様によるとTutorial

答えて

0

を、順序は保証されません。

...このクラスを地図の順番については保証しません。特に、注文が時間の経過とともに一定に保たれることを保証するものではありません。

したがって、グループの順序を保証する場合は、別のデータ構造を使用する必要があります。

同様の説明と、挿入順序を維持するLinkedHashMapの推奨される使用法については、this answerを参照してください。

+0

コード内で他のものを変更する必要がありますか、またはすべての "HashMap"を "LinkedHashMap"に置き換えることはできますか? – TheTechGuy96

+0

すべての "HashMap"を "LinkedHashMap"に置き換えることができます。少し抽象化し、HashMap宣言をMapに置き換えることもできます。そうすれば、実装タイプを変更するだけで、将来はTreeMapなどに変更する必要があります。さらに、ドキュメントによれば、LinkedHashMapはTreeMapよりも安価です(これはあなたの最後の手段です)。 – austinbruch

関連する問題