2013-09-25 52 views
0

我在Android的世界新的,我有以下情形:ExpandableListView及子活動

我有一個活動的main.xml的RelativeLayout和ExpandableListView內。

當我展開一個小組時,它會向我顯示小孩的內容:這是example.xml。

現在我已經在example.xml-layout 1中使用id btn_send,我的問題是我如何訪問按鈕?

我試圖通過Example.java訪問按鈕我得到一個錯誤,無法找到btn_send。我嘗試了通過Main.java我得到了一些錯誤。

謝謝你在前進,

BR 阿里

public class Main extends Activity { 

ExpandableListView exv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    exv = (ExpandableListView) findViewById(R.id.exv_main); 
    exv.setAdapter(new MainAdapter(this)); 
    exv.setOnGroupExpandListener(new OnGroupExpandListener() { 

     int previousGroup = -1; 

     @Override 
     public void onGroupExpand(int groupPosition) { 
      if(groupPosition != previousGroup) 
       exv.collapseGroup(previousGroup); 
      previousGroup = groupPosition; 
     } 
    }); 

} 
} 

在MainAdapter類

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    RelativeLayout layout = new RelativeLayout(context); 
    ImageView iv = new ImageView(context); 


    RelativeLayout.LayoutParams llpImage = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 

    layout.setGravity(Gravity.CENTER | Gravity.TOP); 


     switch (groupPosition){ 

      case 0: 

       layout = (RelativeLayout) inflater.inflate(childList[groupPosition][childPosition], null); 


      break; 

      case 1: 

      break; 
      case 2: 

       break; 
     } 
     return layout; 
} 

和main.xml中

<ExpandableListView 
    android:id="@+id/exv_main" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp" 
    android:layout_marginTop="20dp" 
    android:background="#CDCDD0" 
    android:divider="@android:color/transparent" 
    android:listSelector="#CDCDD0" 
    android:dividerHeight="1dip" 
    android:cacheColorHint="#CDCDD0" 
    android:childDivider="#CDCDD0" 
    > 
</ExpandableListView> 

,並在的example.xml

<Button 
    android:id="@+id/btn_send" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="send" /> 

,並在Example.java

公共類實例擴展活動{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.example); 

    final Button btn = (Button) findViewById(R.id.btn_send);  

    btn.setOnClickListener(new OnClickListener() {  
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      btn.setText("huhu"); 
     } 
    }); 
} 
} 
+1

請張貼您的代碼片段和XML佈局。 –

+0

@AndreaCarrer感謝你的回答。但我不知道如何粘貼我的代碼片段:-(它要長! – Andy

+0

粘貼代碼中最相關的部分,與btn_send對象連接的部分。 –

回答

0

你就必須將onClickListener在getChildView方法設置爲您的按鈕:

public class MyExpandableAdapter extends BaseExpandableListAdapter { 

    private final SparseArray<Group> groups; 
    public Activity activity; 
    public LayoutInflater inflater; 

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

    @Override 
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.rowlayout_details, null); 
     } 
     TextView text = (TextView) convertView.findViewById(R.id.textView1); 
     text.setText("content"); 

     Button b = (Button) convertView.findViewById(R.id.button); 
     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       // implement functionality 

      } 
     }); 

     return convertView; 
    }  

} 
+0

謝謝你的回答,但我在這裏有一個以上的開關櫃佈局,所以我怎麼能做到這一點?謝謝 – Andy

+0

@Ali哦,是的,我明白了,在你發佈代碼之前,我回答了它。然而,我現在正在上課,所以我不能馬上回答你的評論。 –

+0

謝謝你的幫助,沒問題,我會嘗試如果我解決它我會告訴你,否則你可以建議你的解決方案: - ) – Andy

0

@Kuba Spatny謝謝你的幫助,我解決了我的問題,如下面的代碼...也許有其他這也是我的解決方案:

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

    LayoutInflater inflater; 

    switch (groupPosition) { 
    case 0: 
     if (convertView == null) { 
      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(childLayout[groupPosition][childPosition], null); 
      Button btn1 = (Button) convertView.findViewById(R.id.btn_send); 
      btn1.setOnClickListener(new OnClickListener() { 

          @Override 
          public void onClick(View v) { 
           v.getContext().startActivity(new Intent(context, ExampTest1.class)); 
          } 
         }); 
      } 



     break; 
    case 1: 
      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(childLayout[groupPosition][childPosition], null); 
      Button btn1 = (Button) convertView.findViewById(R.id.btn_send); 
      btn1.setOnClickListener(new OnClickListener() { 

          @Override 
          public void onClick(View v) { 
           v.getContext().startActivity(new Intent(context, ExampTest2.class)); 
          } 
         }); 
     break; 
    case 2: 
     if (convertView == null) { 
      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(childLayout[groupPosition][childPosition], null); 
      Button btn1 = (Button) convertView.findViewById(R.id.btn_send); 
      btn1.setOnClickListener(new OnClickListener() { 

          @Override 
          public void onClick(View v) { 
           v.getContext().startActivity(new Intent(context, ExampTest3.class)); 
          } 
         }); 
     break; 
    } 

    return convertView; 
}