2011-01-04 11 views
2

アンドロイドのExpandableリストの各セクションに異なる子供を表示したい。だから私は書かれている次の行:アンドロイドで展開可能なリストビューの各セクションに異なる子供を追加するにはどうすればよいですか?

//SmplExpandable.java 
package com.bogotobogo.android.smplexpandable; 

import android.app.ExpandableListActivity; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.WindowManager; 
import android.widget.HeterogeneousExpandableList; 
import android.widget.ImageView; 
import android.widget.SimpleExpandableListAdapter; 
import android.widget.TextView; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

/** 
* Demonstrates expandable lists backed by a Simple Map-based adapter 
*/ 
public class SmplExpandable extends ExpandableListActivity implements HeterogeneousExpandableList{ 


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

     final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>(); 
     List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, Object>>>(); 

      Map<String, Object> curGroupMap = new HashMap<String, Object>(); 
      curGroupMap = new HashMap<String, Object>(); 
      curGroupMap.put("Name","Recipe"); 
      groupData.add(curGroupMap); 

      curGroupMap = new HashMap<String, Object>(); 
      curGroupMap.put("Name","Feedback"); 
      groupData.add(curGroupMap); 

      curGroupMap = new HashMap<String, Object>(); 
      curGroupMap.put("Name","Images"); 
      groupData.add(curGroupMap); 

      curGroupMap = new HashMap<String, Object>(); 
      curGroupMap.put("Name","Video"); 
      groupData.add(curGroupMap); 

      List<Map<String, Object>> children = new ArrayList<Map<String, Object>>(); 
      Map<String, Object> curChildMap = new HashMap<String, Object>(); 
      curChildMap = new HashMap<String, Object>(); 

      curChildMap.put("MyRecipe",getResources().getString(R.string.My_Recipe)); 
      children.add(curChildMap); 
      childData.add(children); 

      children = new ArrayList<Map<String, Object>>(); 
      curChildMap = new HashMap<String, Object>(); 
      curChildMap.put("MyFeedback", getResources().getString(R.string.My_Feedback)); 
      children.add(curChildMap); 
      childData.add(children); 

      children = new ArrayList<Map<String, Object>>(); 
      curChildMap = new HashMap<String, Object>(); 
      curChildMap.put("MyImage", getResources().getDrawable(R.drawable.icon)); 
      children.add(curChildMap); 
      childData.add(children); 

      children = new ArrayList<Map<String, Object>>(); 
      curChildMap = new HashMap<String, Object>(); 
      curChildMap.put("MyImage", getResources().getDrawable(R.drawable.icon)); 
      children.add(curChildMap); 

      childData.add(children); 

     // Set up our adapter 

      setListAdapter(new SimpleExpandableListAdapter(
        this, 
        groupData, 
        android.R.layout.simple_expandable_list_item_1, 
        new String[] {"Name"},   // the name of the field data 
        new int[] { android.R.id.text1 }, // the text field to populate with the field data 
        childData, 
        0, 
        null, 
        new int[] {} 
       ) 

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


         final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent); 
         if (groupPosition == 0) 
         { 
         // Populate your custom view here 

         ((TextView)v.findViewById(R.id.TextView01)).setText((String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyRecipe")); 
         } 

         else if (groupPosition == 1) 
         { 
         // Populate your custom view here 
          ((TextView)v.findViewById(R.id.TextView02)).setText((String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyFeedback")); 
         } 

         else if (groupPosition == 2) 
         { 
         // Populate your custom view here 
          ((ImageView)v.findViewById(R.id.ImageView01)).setImageDrawable((Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyImage")); 
         } 

         else if (groupPosition == 3) 
         { 
         // Populate your custom view here 
          ((ImageView)v.findViewById(R.id.ImageView01)).setImageDrawable((Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyImage")); 
         } 

         return v; 

        } 

        @Override 
        public View newChildView(boolean isLastChild, ViewGroup parent) { 
         { 

          return layoutInflater.inflate(R.layout.myrow, null, false); 
         } 
        } 
       } 

      ); 
     // Set the width and height of activity. 
     WindowManager.LayoutParams params = getWindow().getAttributes(); 
     params.height =400; 
     params.width = 400; 
     this.getWindow().setAttributes(params); 

    } 

} 

ファイルmyrow.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
     <ImageView android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    </ImageView> 

    <TextView android:id="@+id/TextView01" 
    android:layout_width="fill_parent" 
    android:background="#FFFFFF" 
    android:textColor="#FF0000" 
    android:textSize="10px" 
    android:layout_height="fill_parent"/> 

    <TextView android:id="@+id/TextView02" 
    android:layout_width="fill_parent" 
    android:background="#FFFFFF" 
    android:textColor="#FF0000" 
    android:textSize="10px" 
    android:layout_height="fill_parent"/> 



    <VideoView android:id="@+id/VideoView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    </VideoView> 

</LinearLayout> 

私の問題は、私が表示したいのに対し、すべての4つのセクションでは、テキストと画像の両方を見ることができるだということですテキストは最初の2つのセクションにのみあり、イメージは他の2つのセクションにあります。

誰でも私が間違っていることを教えてもらえますか?

おかげで、 ネハ

答えて

3

あなたの問題は簡単です。

あなたは、アダプタの

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

方法でビューを膨らませています。

しかし、この膨張したビューには、テキストビューとイメージビューの両方が含まれています。あなただけを表示したい場合は、もう一方を非表示にする必要があります。

あなたの問題を解決する必要がありますし、あなたはここからそれを終えることができるはず
if (groupPosition == 0) 
    {      
// set your text hide 
// HIDE YOUR IMAGE VIEW 
    ImageView iv = ... 
    iv.setVisibility(View.GONE); 
    } 

... 

if (groupPosition == 3) 
    {      
// set your image source 
// HIDE YOUR TEXT VIEW 
    TextView tv = ... 
    t.setVisibility(View.GONE); 
    } 

:)

+0

こんにちはmonkjack、ウル答えてくれてありがとう。私はそれを他の方法でやってみました。私は、展開可能なリストの各子セクションごとに別々の.xmlファイルを宣言しましたが、今はうまくいきます:)しかし、今は別の問題があります。今私は拡張可能なリストのセクションの1つにビデオを表示したい。どうやってするか?何か案が?おかげで、ネハ – Neha

関連する問題