2015-04-16 24 views
2

我試圖在Android中創建一個ExpandableListView,其中顯示問題列表。當我運行應用程序時,屏幕上不顯示任何內容。我不確定爲什麼會發生這種情況。誰能幫我這個。Android ExpandableListView在應用程序中不可見

我不想說清楚,我從服務器獲取數據,將其保存在對象(又名問題)中。我已經測試了很多,這段代碼沒有任何問題。所以要顯示的數據是可用的,但不會顯示在應用程序中。

我用下面的代碼:

PlaceHolderFragment:

public static class PlaceholderFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

     // Progress dialog 
     private ProgressDialog pDialog; 

     private static String TAG = Problems.class.getSimpleName(); 

     private ArrayList<Problem> myProblems = new ArrayList<Problem>(); 

     private SparseArray<Group> groups = new SparseArray<Group>(); 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 

      //Here comes code that handles data input from server (which correctly works btw) 

      for(int i = 0; i < myProblems.size(); i++){ 
       Group group = new Group(myProblems.get(i).getTitle()); 
       group.children.add(myProblems.get(i).domainsToString()); 
       group.children.add(myProblems.get(i).getDescription()); 
       groups.append(i, group); 
      } 

      View rootView = inflater.inflate(R.layout.fragment_my_problems, container, false); 

      mMyProblems = (ExpandableListView) rootView.findViewById(R.id.my_problems); 
      //mMyProblems.setAdapter(adapterMyP); 
      MyProblemExpandableListViewAdapter adapter = new MyProblemExpandableListViewAdapter(this.getActivity(), groups); 
      mMyProblems.setAdapter(adapter); 

      return rootView; 
     } 

     private void showpDialog() { 
      if (!pDialog.isShowing()) 
       pDialog.show(); 
     } 

     private void hidepDialog() { 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
     } 

    } 

適配器:

public class MyProblemExpandableListViewAdapter extends BaseExpandableListAdapter { 
    private final SparseArray<Group> groups; 
    public LayoutInflater inflater; 
    public Activity activity; 

    public MyProblemExpandableListViewAdapter(Activity act, SparseArray<Group> groups) { 
     activity = act; 
     this.groups = groups; 
     inflater = act.getLayoutInflater(); 
    } 

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

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

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 
     final String children = (String) getChild(groupPosition, childPosition); 
     TextView text = null; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.listrow_details, null); 
     } 
     text = (TextView) convertView.findViewById(R.id.textView1); 
     text.setText(children); 
     convertView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(activity, children, 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     return convertView; 
    } 

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

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

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

    @Override 
    public void onGroupCollapsed(int groupPosition) { 
     super.onGroupCollapsed(groupPosition); 
    } 

    @Override 
    public void onGroupExpanded(int groupPosition) { 
     super.onGroupExpanded(groupPosition); 
    } 

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.listrow_group, null); 
     } 
     Group group = (Group) getGroup(groupPosition); 
     ((CheckedTextView) convertView).setText(group.string); 
     ((CheckedTextView) convertView).setChecked(isExpanded); 
     return convertView; 
    } 

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

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

} 

和組類:

public class Group { 
    public String string; 
    public final List<String> children = new ArrayList<String>(); 

    public Group(String string) { 
     this.string = string; 
    } 
} 

及相應的XML文件:

fragementMyProblems.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.wsi.wesupportit.Problems$PlaceholderFragment" > 

    <ExpandableListView 
     android:id="@+id/my_problems" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ExpandableListView> 

</LinearLayout> 

listrow_group.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:layout_marginLeft="8dp" 
    android:gravity="left" 
    android:paddingLeft="32dp" 
    android:paddingTop="8dp" 
    android:text="Test" 
    android:textSize="14sp" 
    android:textStyle="bold" /> 

listrow_details.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:clickable="true"android:orientation="vertical" 
    android:paddingLeft="40dp"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawablePadding="5dp" 
     android:gravity="center_vertical" 
     android:text="@string/hello_world" 
     android:textSize="14sp" 
     android:textStyle="bold" > 
    </TextView> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="@android:color/black" /> 

</LinearLayout> 

在此先感謝。

+0

根據複製的代碼,myProblems是空的,因爲它是空的,所以傳遞給適配器的值是空的。顯然可擴展的列表視圖爲空 –

+0

@HarshaVardhan正如我在我的文章中指出的那樣,填充myProblems的代碼不在發佈的代碼中,它是從服務器獲取jsonObject並使用Problems填充myProblems的代碼。此代碼應該正常工作(在其他地方測試過)。我用字符串測試過,沒有顯示出來。 – Jonathan

回答

1

讓你的MyProblemExpandableListViewAdapter最終並把它放在課堂上。並且確保你每次當你改變你的內容通知你的適配器groups

+0

感謝您的提示。我已將MyProblemExpandableListViewAdapter更改爲final,並在PlaceHolderFragment的onCreateView()方法的開頭聲明瞭它。每次向適配器添加一個組時,我都使用了方法notifyDataSetChanged(),並最終使用它! – Jonathan

相關問題