Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在视图中包含ExpandableListActivity_Android_Android Intent_Expandablelistview - Fatal编程技术网

Android 如何在视图中包含ExpandableListActivity

Android 如何在视图中包含ExpandableListActivity,android,android-intent,expandablelistview,Android,Android Intent,Expandablelistview,我有一个文本视图,图像视图,。。。但我想使用可扩展列表包含一些数据。我不知道如何将此可扩展列表与其他数据一起包含在我的视图中 我的目标是: 我在一个视图中有所有组件,但在其他活动中有可扩展列表除外: OfferDetailsActivity(这是显示视图的活动,我想从这里调用ExpandableList) } 可扩展列表 公共类ExpandableList扩展了ExpandableListActivity{ @抑制警告(“未选中”) 创建时的公共void(Bundle savedInstanc

我有一个文本视图,图像视图,。。。但我想使用可扩展列表包含一些数据。我不知道如何将此可扩展列表与其他数据一起包含在我的视图中

我的目标是:

我在一个视图中有所有组件,但在其他活动中有可扩展列表除外:

OfferDetailsActivity(这是显示视图的活动,我想从这里调用ExpandableList)

}

可扩展列表

公共类ExpandableList扩展了ExpandableListActivity{
@抑制警告(“未选中”)
创建时的公共void(Bundle savedInstanceState){
试一试{
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_详细信息);
SimpleExpandableListAdapter解释适配器=
新的SimpleExpandableListAdapter(
这
createGroupList(),//正在创建组列表。
R.layout.group_行,//组项布局XML。
新字符串[]{“组项”},//组项的键。
new int[]{R.id.row_name},//每个组项的id。-键下的数据进入此文本视图。
createChildList(),//childData描述二级条目。
R.layout.child_行,//子级条目的布局(第二级)。
新字符串[]{“子项”},//要显示的childData映射中的键。
上面键下的新int[]{R.id.grp_child}//数据进入这些文本视图。
);
setListAdapter(expListAdapter);//在列表中设置适配器。
}捕获(例外e){
System.out.println(“Errrr++”+e.getMessage());
}
}
/*为行创建Hashmap*/
@抑制警告(“未选中”)
私有列表createGroupList(){
ArrayList结果=新建ArrayList();
对于(int i=0;i<15;++i){//15组。。。。。。。。
HashMap m=新的HashMap();
m、 put(“组项”,“组项”+i);//键及其值。
结果:添加(m);
}
返回(列表)结果;
}
/*为孩子们创建HashMap*/
@抑制警告(“未选中”)
私有列表createChildList(){
ArrayList结果=新建ArrayList();
对于(inti=0;i<15;++i){//this-15是组数(这里是15)
/*每个组都需要每个HashMap。对于每个组,我们有3个子组*/
ArrayList secList=新的ArrayList();
对于(int n=0;n<3;n++){
HashMap child=新的HashMap();
“子项”、“子项”+n);
secList.add(子项);
}
结果。添加(secList);
}        
返回结果;
}
内容更改时的公共无效(){
System.out.println(“onContentChanged”);
super.onContentChanged();
}
/*每次单击子项时都会调用此函数*/
公共布尔onChildClick(ExpandableListView父视图、视图v、int-groupPosition、int-childPosition、长id){
System.out.println(“在groupPosition=“+groupPosition+”子对象在位置“+childPosition”处单击的内部onChildClick”);
返回true;
}
/*此函数在组扩展时调用*/
public void onGroupExpand(int groupPosition){
试一试{
System.out.println(“组扩展侦听器=>groupPosition=“+groupPosition”);
}捕获(例外e){
System.out.println(“grouppositionerrrr++”+e.getMessage());
}
}  
}

提供详细信息.xml



将代码从可展开列表活动移动到其他qctivity,并将必要的位放入其他活动XML文件中,您应该可以。。。您是否有一些问题阻止您这样做?

可能是继承问题。。。其中一个活动扩展了Activity,另一个扩展了ExpandableListActivity。我会按照你说的去做。你可能需要做的唯一更改是找到可扩展列表视图(findViewById),因为Activity不使用id。并且你调用的设置适配器的方法不同(如果我没记错的话,setAdapter与setListAdapter)。如果我将代码从ExpandableList.class移动到OfferDetailsActivity.class,则会出现编译错误,因为例如,只有从ExpandableListActivity进行扩展时,“createGroupList()和“createChildList()”方法才可用。为了解决这个问题,我决定保留这两个类,并在OfferDetailsActivity.class中,在setContentView(view)之前写这行代码调用另一个类。无法正常工作:ExpandableListView oExpandableListView=new ExpandableListView(this.getBaseContext());setAdapter(新的ExpandableList().getExpandableListAdapter());
public class OfferDetailsActivity extends Activity{

final public static String OFERTA = "oferta";
private Oferta oferta = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.offer_details);             

    oferta = (Oferta)getIntent().getExtras().getParcelable(OFERTA);

    View view = new View(this.getBaseContext());

    LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = linflater.inflate(R.layout.offer_details, null);

    // poblamos la lista de elementos           
    TextView precio = (TextView)view.findViewById(R.id.precio_offer_details);
    precio.setText(oferta.getPrecio());

    ImageView imagen = (ImageView) view.findViewById(R.id.imagen_offer_details);
    imagen.setImageBitmap(new GetImages().downloadFile(oferta.getImagen()));

    TextView cabecera = (TextView)view.findViewById(R.id.cabecera_offer_details);
    cabecera.setText(oferta.getCabecera());

    TextView descripcion = (TextView)view.findViewById(R.id.descripcion_offer_details);
    descripcion.setText(oferta.getDescripcion());                                                   

    setContentView(view);

}                                           
public class ExpandableList extends ExpandableListActivity {

@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
    try{
         super.onCreate(savedInstanceState);
         setContentView(R.layout.offer_details);

    SimpleExpandableListAdapter expListAdapter =
        new SimpleExpandableListAdapter(
                this,
                createGroupList(),              // Creating group List.
                R.layout.group_row,             // Group item layout XML.           
                new String[] { "Group Item" },  // the key of group item.
                new int[] { R.id.row_name },    // ID of each group item.-Data under the key goes into this TextView.                   
                createChildList(),              // childData describes second-level entries.
                R.layout.child_row,             // Layout for sub-level entries(second level).
                new String[] {"Sub Item"},      // Keys in childData maps to display.
                new int[] { R.id.grp_child}     // Data under the keys above go into these TextViews.
            );
        setListAdapter( expListAdapter );       // setting the adapter in the list.

    }catch(Exception e){
        System.out.println("Errrr +++ " + e.getMessage());
    }
}

/* Creating the Hashmap for the row */
@SuppressWarnings("unchecked")
private List createGroupList() {
      ArrayList result = new ArrayList();
      for( int i = 0 ; i < 15 ; ++i ) { // 15 groups........
        HashMap m = new HashMap();
        m.put( "Group Item","Group Item " + i ); // the key and it's value.
        result.add( m );
      }
      return (List)result;
}

/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {

    ArrayList result = new ArrayList();
    for( int i = 0 ; i < 15 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
      /* each group need each HashMap-Here for each group we have 3 subgroups */
      ArrayList secList = new ArrayList(); 
      for( int n = 0 ; n < 3 ; n++ ) {
        HashMap child = new HashMap();
        child.put( "Sub Item", "Sub Item " + n );           
        secList.add( child );
      }
     result.add( secList );
    }        
    return result;
}
public void  onContentChanged  () {
    System.out.println("onContentChanged");
    super.onContentChanged();         
}
/* This function is called on each child click */
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
    System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
    return true;
}

/* This function is called on expansion of the group */
public void  onGroupExpand  (int groupPosition) {
    try{
         System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
    }catch(Exception e){
        System.out.println(" groupPosition Errrr +++ " + e.getMessage());
    }
}  
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TableRow>   
    <TextView android:id="@+id/precio_offer_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="12px" />

    <TextView android:id="@+id/cabecera_offer_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="12px" />                      
</TableRow>

<TableRow>        
    <ImageView android:id="@+id/imagen_offer_details"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 

    <TextView android:id="@+id/descripcion_offer_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="normal"
        android:textSize="10px" />                                          
</TableRow>                               

<TextView android:id="@+id/texto3"      
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="normal"
    android:textSize="10px" 
    android:text="texto3"/>

<TextView android:id="@+id/texto4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="normal"
    android:textSize="10px" 
    android:text="texto4"/>