2013-07-10 103 views
0

編輯:已修復空異常。自定義ExpandableListView未完全顯示

試圖按照教程http://en.wikicode.org/index.php/Custom_ExpandableListView只有組1標題顯示沒有孩子。沒有其他孩子的標題顯示出來。爲什麼它會被切斷?

這裏是我的代碼:

public class WelcomeFragment extends Fragment { 

    private UiLifecycleHelper uiHelper; 
    WebView mWebView; 
    TextView mName, mother, mlblName, mlblOther; 
    ImageView mImage; 
    LoginButton mAuthButton; 


    final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0; 
    String[] permissions = { "user_relationships" }; 

    private ArrayList<String> groups; 
    private ArrayList<ArrayList<ArrayList<String>>> childs; 


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

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     View view = inflater.inflate(R.layout.fragment_welcome, null); 

     uiHelper = new UiLifecycleHelper(getActivity(), callback); 
     uiHelper.onCreate(savedInstanceState); 

     mName = (TextView) view.findViewById(R.id.txtName); 

     mother = (TextView) view.findViewById(R.id.txtOther); 
     mImage = (ImageView) view.findViewById(R.id.profilepicture); 
     mAuthButton = (LoginButton) view.findViewById(R.id.authButton); 

     // Get the data 
     Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null); 

     if((c.moveToFirst()) && (c.getCount()>0)) 
     { 
      mAuthButton.setVisibility(View.GONE); 
      mName.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_NAME))); //name 


      //new ImageDownloader().execute(c.getString(c.getColumnIndex(StatusData.KEY_USER_EMAIL)); //call asynctask 
      mother.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_OTHER))); //significant_other 


      AQuery aq = new AQuery(view); 

      //returns the cached file by url, returns null if url is not cached 
      File file = aq.getCachedFile(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE))); 

      if (file == null) { 
       Log.i("PROJECTCARUSO", "Did not find user picture on file"); 
       //load an image to an ImageView from network, cache image to file and memory 
       aq.id(R.id.profilepicture).image(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE))); 
      } else { 
       Log.i("PROJECTCARUSO", "Found user picture on file"); 
       Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 
       mImage.setImageBitmap(myBitmap); 
      } 

      Log.i("PROJECTCARUSO", "Found user information in database"); 
     } else { 

      mlblName = (TextView) view.findViewById(R.id.lblName); 

      mlblOther = (TextView) view.findViewById(R.id.lblOther); 

      //Hide everything 
      mName.setVisibility(View.GONE); 
      mother.setVisibility(View.GONE); 
      mImage.setVisibility(View.GONE); 
      mlblName.setVisibility(View.GONE); 
      mlblOther.setVisibility(View.GONE); 

      Log.i("PROJECTCARUSO", "Did not find user information in database"); 
     } 

     mAuthButton.setFragment(this); 
     mAuthButton.setReadPermissions(Arrays.asList("email")); 

     ExpandableListView l = (ExpandableListView) getActivity().findViewById(R.id.ExpandableListView01); 

     loadData(); 

     myExpandableAdapter adapter = new myExpandableAdapter(getActivity(), groups, childs); 
     l.setAdapter(adapter);  
     return view; 
    } 

    public void refreshView(){ 

     // Get the data 
     Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null); 

     if((c.moveToFirst()) && (c.getCount()>0)) 
     { 
      mAuthButton.setVisibility(View.GONE); 


      //Show everything 
      mName.setVisibility(View.VISIBLE); 

      mother.setVisibility(View.VISIBLE); 
      mImage.setVisibility(View.VISIBLE); 
      mlblName.setVisibility(View.VISIBLE); 
      mlblOther.setVisibility(View.VISIBLE); 

      mName.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_NAME))); //name 


      //new ImageDownloader().execute(c.getString(c.getColumnIndex(StatusData.KEY_USER_EMAIL)); //call asynctask 
      mother.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_OTHER))); //significant_other 


      AQuery aq = new AQuery(getActivity()); 

      //returns the cached file by url, returns null if url is not cached 
      File file = aq.getCachedFile(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE))); 

      if (file == null) { 
       Log.i("PROJECTCARUSO", "Did not find user picture on file"); 
       //load an image to an ImageView from network, cache image to file and memory 
       aq.id(R.id.profilepicture).image(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE))); 
      } else { 
       Log.i("PROJECTCARUSO", "Found user picture on file"); 
       Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 
       mImage.setImageBitmap(myBitmap); 
      } 

      Log.i("PROJECTCARUSO", "Found user information in database"); 
     } else { 
      Log.i("PROJECTCARUSO", "Did not find user information in database"); 
     } 


    } 

    private void onSessionStateChange(Session session, SessionState state, Exception exception) { 
     if (state.isOpened()) { 

      // Request user data and show the results 
      Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { 

       @Override 
       public void onCompleted(GraphUser user, Response response) { 
        if (user != null) { 
         Utility.userUID = user.getId(); 
         final String name = user.getName(); 
         final String fname = user.getFirstName(); 
         final String username = user.getUsername(); 
         final String email = (String) user.getProperty("email"); 
         String picURL = null; 
         String significant_other = null; 
         try { 
          String inputLine = user.getProperty("significant_other").toString(); 
          JSONObject json = new JSONObject(inputLine.toString()); 
          significant_other = json.get("name").toString(); 
         } catch (JSONException e1) { 
          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
         } 

         picURL = "http://graph.facebook.com/"+Utility.userUID+"/picture"; 
         Log.i("PROJECTCARUSO", "Logged in..."); 

         try{    
           ContentValues values = new ContentValues(); 
           values.put(StatusData.KEY_USER_ROWID, Utility.userUID); 
           values.put(StatusData.KEY_USER_NAME, name); 
           values.put(StatusData.KEY_USER_FNAME, fname); 
           values.put(StatusData.KEY_USER_USERNAME, username); 
           values.put(StatusData.KEY_USER_EMAIL, email); 
           values.put(StatusData.KEY_USER_PICTURE, picURL); 
           values.put(StatusData.KEY_USER_OTHER, significant_other); 


           StatusData StatusData = new StatusData(getActivity()); 

           StatusData.insertOrReplaceUser(values); 

           Log.i("PROJECTCARUSO", "Insert of facebook information was successfull "); 
           refreshView(); 
         } catch (Exception e) { 
          Log.i("PROJECTCARUSO", "Insert of facebook information was not successfull "); 
         } 
        } 
       } 
      }); 

     } else if (state.isClosed()) { 
      Log.i("PROJECTCARUSO", "Logged out..."); 
      Log.i("PROJECTCARUSO", "Exception: " + exception); 
     } 
    } 

    private Session.StatusCallback callback = new Session.StatusCallback() { 
     @Override 
     public void call(Session session, SessionState state, Exception exception) { 
      onSessionStateChange(session, state, exception); 
     } 
    }; 

    @Override 
    public void onResume() { 
     super.onResume(); 

     // For scenarios where the main activity is launched and user 
     // session is not null, the session state change notification 
     // may not be triggered. Trigger it if it's open/closed. 
     Session session = Session.getActiveSession(); 
     if (session != null && 
       (session.isOpened() || session.isClosed())) { 
      onSessionStateChange(session, session.getState(), null); 
     } 

     uiHelper.onResume(); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     uiHelper.onActivityResult(requestCode, resultCode, data); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     uiHelper.onSaveInstanceState(outState); 
    } 

    public class myExpandableAdapter extends BaseExpandableListAdapter { 

     private ArrayList<String> groups; 

     private ArrayList<ArrayList<ArrayList<String>>> children; 

     private Context context; 

     public myExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) { 
      this.context = context; 
      this.groups = groups; 
      this.children = childs; 
     } 


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


     @Override 
     public ArrayList<String> getChild(int groupPosition, int childPosition) { 
      return children.get(groupPosition).get(childPosition); 
     } 

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


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

      String child = (String) ((ArrayList<String>)getChild(groupPosition, childPosition)).get(0); 

      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.expandablelistview_child, null); 
      } 

      TextView childtxt = (TextView) convertView.findViewById(R.id.TextViewChild01); 

      childtxt.setText(child); 

      return convertView; 
     } 

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

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

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

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

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

      String group = (String) getGroup(groupPosition); 

      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.expandablelistview_group, null); 
      } 

      TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGroup); 

      grouptxt.setText(group); 

      return convertView; 
     } 

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

     @Override 
     public boolean isChildSelectable(int arg0, int arg1) { 
      return true; 
     } 

    } 

    private void loadData(){ 
     groups= new ArrayList<String>(); 
     childs= new ArrayList<ArrayList<ArrayList<String>>>(); 

     groups.add("Group 1"); 
     groups.add("Group 2"); 
     groups.add("Group 3"); 

     childs.add(new ArrayList<ArrayList<String>>()); 
     childs.get(0).add(new ArrayList<String>()); 
     childs.get(0).get(0).add("Child 1 group 1"); 
     childs.get(0).add(new ArrayList<String>()); 
     childs.get(0).get(1).add("Child 2 group 1"); 
     childs.get(0).add(new ArrayList<String>()); 
     childs.get(0).get(2).add("Child 3 group 1"); 

     childs.add(new ArrayList<ArrayList<String>>()); 
     childs.get(1).add(new ArrayList<String>()); 
     childs.get(1).get(0).add("Child 1 group 2"); 
     childs.get(1).add(new ArrayList<String>()); 
     childs.get(1).get(1).add("Child 2 group 2"); 
     childs.get(1).add(new ArrayList<String>()); 
     childs.get(1).get(2).add("Child 3 group 2"); 

     childs.add(new ArrayList<ArrayList<String>>()); 
     childs.get(2).add(new ArrayList<String>()); 
     childs.get(2).get(0).add("Child 1 group 3"); 
     childs.get(2).add(new ArrayList<String>()); 
     childs.get(2).get(1).add("Child 2 group 3"); 
     childs.get(2).add(new ArrayList<String>()); 
     childs.get(2).get(2).add("Child 3 group 3"); 
    } 
} 

XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="6dip" 
     android:layout_marginTop="20dp" 
     android:background="#ffcccccc" 
     android:textIsSelectable="false" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#ffcccccc" > 

     <ImageView 
      android:id="@+id/profilepicture" 
      android:layout_width="100sp" 
      android:layout_height="100sp" 
      android:layout_alignParentLeft="true" 
      android:contentDescription="@string/lblPicture" 
      android:src="@drawable/photofemale" /> 

     <!-- Name --> 

     <TextView 
      android:id="@+id/txtName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/lblName" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="20dp" 
      android:layout_toRightOf="@+id/profilepicture" 
      android:text="@string/txtname" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <!-- significant_other --> 

     <TextView 
      android:id="@+id/lblOther" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/txtName" 
      android:layout_below="@+id/txtName" 
      android:text="@string/lblother" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <!-- Logged in Credentials --> 

     <TextView 
      android:id="@+id/lblName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/txtName" 
      android:layout_below="@+id/textView1" 
      android:text="@string/lblname" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/txtOther" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/lblOther" 
      android:layout_below="@+id/lblOther" 
      android:text="@string/txtother" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <!-- Logged in Credentials --> 
     <com.facebook.widget.LoginButton 
      android:id="@+id/authButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/textView1" /> 

    </RelativeLayout> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="fill_parent" 
     android:layout_height="6dip" 
     android:layout_marginBottom="5dp" 
     android:background="#ffcccccc" 
     android:textIsSelectable="false" /> 


    <ExpandableListView 
     android:id="@+id/ExpandableListView01" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
    > 
    </ExpandableListView> 

</LinearLayout> 
</ScrollView> 

對於理智: expandablelistview_child.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#666666" 
> 

<TextView 
    android:id="@+id/TextViewChild01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="30px" 
> 
</TextView> 

<TextView 
    android:id="@+id/TextViewChild02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10px" 
> 
</TextView> 

<TextView 
    android:id="@+id/TextViewChild03" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10px" 
> 
</TextView> 

</LinearLayout> 

expandablelistview_group.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<TextView 
    android:id="@+id/TextViewGroup" 
    android:layout_width="wrap_content" 
    android:layout_height="50px" 
    android:layout_marginLeft="50px" 
    android:gravity="center_vertical" 
> 
</TextView> 

</LinearLayout> 
+0

你對線143一個NullPointerException該行什麼對象可以是空? (我們在這裏看不到行號) – Karakuri

+0

該信息被放置在敘述中。 – jcaruso

回答

1

有你的錯誤的兩個可能的原因:

  • 有一個在你的看法與ID ExpandableListView01沒有元素
  • ID爲ExpandableListView01的看法是類型ExpandableListView

的不這種情況下,我的賭注是第一個原因。事實上,你正在調用getViewById()的活動,尚未加載佈局。通過這條線

ExpandableListView l = (ExpandableListView) getActivity().findViewById(R.id.ExpandableListView01); 

:替換此行

ExpandableListView l = (ExpandableListView) view.findViewById(R.id.ExpandableListView01); 
+0

你絕對正確的看法,而getActivity我怎麼會這麼盲目!我現在唯一遇到的一個問題是,組1出現向下箭頭,沒有別的。如果我點擊它,除了使它成爲向上箭頭外,它什麼也不做。 我添加了主XML,但其他人完全相同。 – jcaruso

+0

@jcaruso代碼看起來不錯。我可以建議你在'childtxt.setText(child)'和'grouptxt.setText(group)'裏面添加一些日誌來查看'Textviews'是否設置正確?在附註中,請考慮使用ViewHolder模式,如[這裏](http://sriramramani.wordpress.com/2012/07/25/infamous-viewholder-pattern/) – verybadalloc

+0

解釋的那樣,其'ExpandableListView'的' layout_height'這是問題。它切斷它。你會建議什麼? – jcaruso