CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: megthebest con ben 1 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: carotix con ben 4 Thanks ricevuti negli ultimi sette giorni
Utente del mese: megthebest con ben 21 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 9 su 9
Discussione:

Utilizzare un'immagine nella ExpandableList

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Androidiano


    Registrato dal
    Apr 2011
    Località
    Bologna
    Messaggi
    239
    Smartphone
    Huawei Ascend Y300

    Ringraziamenti
    9
    Ringraziato 95 volte in 36 Posts
    Post

    Utilizzare un'immagine nella ExpandableList

    Risolto un problema se ne presenta un altro Ho una ExpandableList e vorrei inserire un'immagine diversa per ogni child solo che non ci riesco. Forse perchè devo impostare un file xml specifico per il child e uno per il group?

     
    1
    <?xml version="1.0" encoding="utf-8"?>
    2
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3
    android:orientation="vertical"
    4
    android:layout_width="fill_parent"
    5
    android:layout_height="fill_parent"
    6
    android:background="@drawable/secondapagina" 
    7
    android:scrollbars="none">
    8
    <ExpandableListView android:id="@+id/expandableListView1"
    9
    android:layout_width="fill_parent"
    10
    android:background="@drawable/secondapagina" 
    11
    android:layout_height="fill_parent" 
    12
    android:divider="@drawable/separatore" 
    13
    android:paddingLeft="25dip" 
    14
    android:paddingRight="4dip" 
    15
    android:scrollbars="none"/>
    16
    </LinearLayout>



    x
     
    1
    package it.gogle.com;
    2
    3
    4
    import it.gogle.com.R;
    5
    import android.app.Activity;
    6
    import android.os.Bundle;
    7
    import android.view.*;
    8
    import android.widget.*;
    9
    10
    11
    public class sedactivity extends Activity {
    12
        ExpandableListAdapter mAdapter;
    13
        @Override
    14
        public void onCreate(Bundle savedInstanceState) {
    15
        super.onCreate(savedInstanceState);
    16
        setContentView(R.layout.main);
    17
       
    18
        // Set up our adapter
    19
        ExpandableListAdapter mAdapter;
    20
        ExpandableListView epView = (ExpandableListView) findViewById(R.id.expandableListView1);
    21
        mAdapter = new MyExpandableListAdapter();
    22
        epView.setAdapter(mAdapter);
    23
            
    24
            
    25
        }
    26
        public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    27
        // Sample data set. children[i] contains the children (String[]) for
    28
        //groups[i].
    29
        private String[] groups = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", 
    30
        "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#", };
    31
        private String[][] children = {
    32
        //A     
    33
        { "A Girl's Best Friend", "Absolutely Fabulous", "Amadeus", },
    34
        //B
    35
        { "Bellini", "Blu Champagne", },
    36
        //C
    37
        { "Champagne Cocktail", "Champagne Fizz", "Champagne J", "Champagne Cooler", "Champagne Pick Me Up", },
    38
        //D
    39
        { "", },
    40
        //E
    41
        { "", },
    42
        //F
    43
        { "French '75", "French Revolution", },
    44
        //G
    45
        { "Golden Lady", "Golden Mimosa", },
    46
        //H
    47
        { "Hillary Wallbanger", },
    48
        //I
    49
        { "", },
    50
        //J
    51
        { "", },
    52
        //K
    53
        { "King's Peg", "Kir", "Kir Imperial", "Kir Royal",  },
    54
        //L
    55
        { "", },
    56
        //M
    57
        { "Midori Fizz", "Mimosa", },
    58
        //N
    59
        { "Nelson's Blood", },
    60
        //O
    61
        { "", },
    62
        //P
    63
        { "Pear In It", "Puccini", },
    64
        //Q
    65
        { "Queen's Cousin", },
    66
        //R
    67
        { "Rossini", "Royal Screw", "Royal Touch", },
    68
        //S
    69
        { "San Balila", "Sweet Blu", },
    70
        //T
    71
        { "Typhoon", },
    72
        //U
    73
        { "", },
    74
        //V
    75
        { "Verdi", },
    76
        //W
    77
        { "White Wine Cooler", },
    78
        //X
    79
        { "", },
    80
        //Y
    81
        { "Yak", "Yap", },
    82
        //Z
    83
        { "", },
    84
        //#
    85
        { "",},
    86
        };
    87
        public Object getChild(int groupPosition, int childPosition) {
    88
        return children[groupPosition][childPosition];
    89
        }
    90
        public long getChildId(int groupPosition, int childPosition) {
    91
        return childPosition;
    92
        }
    93
        public int getChildrenCount(int groupPosition) {
    94
            int i = 0;
    95
            try {
    96
            i = children[groupPosition].length;
    97
    98
            } catch (Exception e) {
    99
            }
    100
    101
            return i;
    102
        }
    103
        public TextView getGenericView() {
    104
        // Layout parameters for the ExpandableListView
    105
        AbsListView.LayoutParams dp = new AbsListView.LayoutParams(
    106
        ViewGroup.LayoutParams.FILL_PARENT, 80);
    107
        TextView textView = new TextView(sedactivity.this);
    108
        textView.setLayoutParams(dp);
    109
        textView.setTextSize(25);
    110
        
    111
        // Center the text vertically
    112
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    113
        // Set the text starting position
    114
        textView.setPadding(60, 0, 0, 0);
    115
        return textView;
    116
        }
    117
        public View getChildView(int groupPosition, int childPosition, 
    118
        boolean isLastChild,View convertView, ViewGroup parent) {
    119
        TextView textView = getGenericView();
    120
        textView.setText(getChild(groupPosition, childPosition).toString());
    121
        return textView;
    122
        }
    123
        public Object getGroup(int groupPosition) {
    124
        return groups[groupPosition];
    125
        }
    126
        public int getGroupCount() {
    127
        return groups.length;
    128
        }
    129
        public long getGroupId(int groupPosition) {
    130
        return groupPosition;
    131
        }
    132
        public View getGroupView(int groupPosition, boolean isExpanded, 
    133
        View convertView, ViewGroup parent) {
    134
        TextView textView = getGenericView();
    135
        textView.setText(getGroup(groupPosition).toString());
    136
        return textView;
    137
        }
    138
        public boolean isChildSelectable(int groupPosition, int childPosition) {
    139
        return true;
    140
        }
    141
        public boolean hasStableIds() {
    142
        return true;
    143
        }
    144
        }
    145
    }
    146



    Un'ultima cosa, io vorrei far scrollare l'immagine di background solo che questa sta ferma sullo sfondo mentre scrolla solo la ExpandableList, è possibile farlo oppure devo usare qualche altra cosa? Grazie.

  2.  
  3. #2
    Androidiano L'avatar di Jordano


    Registrato dal
    Nov 2010
    Località
    Reggio Emilia
    Messaggi
    263
    Smartphone
    Nexus S / HTC Magic 32a

    Ringraziamenti
    1
    Ringraziato 48 volte in 46 Posts
    Predefinito

    puoi, basta fare un layout con l'immagine per il child della ExpandableListView e poi mappare il tutto in modo corretto.



    per lo sfondo potresti provare a impostare l'immagine come background del LinearLayout che contiene la ExpandableListView, e impostare il background della ExpandableListView come semitrasparente o trasparente.

  4. #3
    Androidiano


    Registrato dal
    Apr 2011
    Località
    Bologna
    Messaggi
    239
    Smartphone
    Huawei Ascend Y300

    Ringraziamenti
    9
    Ringraziato 95 volte in 36 Posts
    Predefinito

    Le ExpandableList non vengono molto menzionate, sia nei libri cartacei per Android sia nelle guide che si trovano su internet, non ne so molto su questo argomento. Leggendo quel poco avevo intuito che dovevo fare un layout per il child e quindi di conseguenza tutto quel codice java non serve a nulla, andrebbe riscritto vero?

  5. #4
    Androidiano L'avatar di Jordano


    Registrato dal
    Nov 2010
    Località
    Reggio Emilia
    Messaggi
    263
    Smartphone
    Nexus S / HTC Magic 32a

    Ringraziamenti
    1
    Ringraziato 48 volte in 46 Posts
    Predefinito

    qua puoi trovare forse parzialmente ispirazione per come fare a mettere nei child un layout con un immagine che presumo rappresenti il cocktail e il nome del cocktail.

    [android-developers] Re: Customize layout of ExpandableListView

    non ho letto nel dettaglio perchè ora non ho tempo, ma ho visto che estende l'adapter e ho visto che nel layout del child c'è una ImageView, quindi sicuramente qualche spunto puoi prenderlo.

  6. Il seguente Utente ha ringraziato Jordano per il post:

    Carontes (14-04-11)

  7. #5
    Androidiano


    Registrato dal
    Apr 2011
    Località
    Bologna
    Messaggi
    239
    Smartphone
    Huawei Ascend Y300

    Ringraziamenti
    9
    Ringraziato 95 volte in 36 Posts
    Predefinito

    Questo è il file di logcat. Mi da errore appena avvio l'app: Spiacenti, interruzione imprevista dell'applicazione sed (processoit.gogle.com).Riprova.



    04-14 23:47:54.243: INFO/ActivityManager(498): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=it.gogle.com/.sedactivity }
    04-14 23:47:54.319: INFO/ActivityManager(498): Start proc it.gogle.com for activity it.gogle.com/.sedactivity: pid=1168 uid=10085 gids={1015}
    04-14 23:47:54.949: WARN/dalvikvm(1168): threadid=1: thread exiting with uncaught exception (group=0x400287f0)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): FATAL EXCEPTION: main
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): java.lang.RuntimeException: Binary XML file line #11: You must supply a layout_height attribute.
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.content.res.TypedArray.getLayoutDimension( TypedArray.java:491)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.ViewGroup$LayoutParams.setBaseAttribu tes(ViewGroup.java:3593)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.ViewGroup$MarginLayoutParams.<init>(V iewGroup.java:3672)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout$LayoutParams.<init>(Li nearLayout.java:1395)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout.generateLayoutParams(L inearLayout.java:1321)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout.generateLayoutParams(L inearLayout.java:45)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.LayoutInflater.rInflate(LayoutInflate r.java:620)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.LayoutInflater.inflate(LayoutInflater .java:407)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.LayoutInflater.inflate(LayoutInflater .java:320)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.SimpleExpandableListAdapter.newGrou pView(SimpleExpandableListAdapter.java:289)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.SimpleExpandableListAdapter.getGrou pView(SimpleExpandableListAdapter.java:274)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.ExpandableListConnector.getView(Exp andableListConnector.java:445)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.AbsListView.obtainView(AbsListView. java:1315)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.ListView.makeAndAddView(ListView.ja va:1727)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.ListView.fillDown(ListView.java:652 )
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.ListView.fillFromTop(ListView.java: 709)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.ListView.layoutChildren(ListView.ja va:1580)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.AbsListView.onLayout(AbsListView.ja va:1147)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.View.layout(View.java:7035)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout.setChildFrame(LinearLa yout.java:1249)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout.layoutVertical(LinearL ayout.java:1125)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout.onLayout(LinearLayout. java:1042)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.View.layout(View.java:7035)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.FrameLayout.onLayout(FrameLayout.ja va:333)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.View.layout(View.java:7035)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout.setChildFrame(LinearLa yout.java:1249)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout.layoutVertical(LinearL ayout.java:1125)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.LinearLayout.onLayout(LinearLayout. java:1042)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.View.layout(View.java:7035)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.widget.FrameLayout.onLayout(FrameLayout.ja va:333)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.View.layout(View.java:7035)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.ViewRoot.performTraversals(ViewRoot.j ava:1045)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.view.ViewRoot.handleMessage(ViewRoot.java: 1727)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.os.Handler.dispatchMessage(Handler.java:99 )
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.os.Looper.loop(Looper.java:123)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at android.app.ActivityThread.main(ActivityThread.jav a:4627)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at java.lang.reflect.Method.invokeNative(Native Method)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at java.lang.reflect.Method.invoke(Method.java:521)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:868)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626)
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): at dalvik.system.NativeStart.main(Native Method)
    04-14 23:47:54.989: WARN/ActivityManager(498): Force finishing activity it.gogle.com/.sedactivity
    04-14 23:47:55.489: WARN/ActivityManager(498): Activity pause timeout for HistoryRecord{445838c8 it.gogle.com/.sedactivity}
    04-14 23:48:05.677: WARN/ActivityManager(498): Activity destroy timeout for HistoryRecord{445838c8 it.gogle.com/.sedactivity}
    04-14 23:48:54.239: DEBUG/QCRIL_MSC(79): "RIL"=>"AMSS" [ label = "cm_mm_client_ss_reg()"];
    04-14 23:48:54.239: DEBUG/QCRIL_MSC(79): "RIL"=>"AMSS" [ label = "cm_mm_client_ss_reg()"];
    04-14 23:48:59.379: DEBUG/StatusBar(498): DISABLE_EXPAND: yes
    04-14 23:48:59.509: DEBUG/GoogleLoginService(885): onBind: Intent { act=android.accounts.AccountAuthenticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
    04-14 23:48:59.579: WARN/InputManagerService(498): Starting input on non-focused client android.view.inputmethod.InputMethodManager$1@441b 7a50 (uid=1000 pid=498)
    04-14 23:52:55.059: INFO/WindowManager(498): WIN DEATH: Window{44589538 it.gogle.com/it.gogle.com.sedactivity paused=false}
    04-14 23:52:55.059: INFO/ActivityManager(498): Process it.gogle.com (pid 1168) has died.

  8. #6
    Androidiano L'avatar di Jordano


    Registrato dal
    Nov 2010
    Località
    Reggio Emilia
    Messaggi
    263
    Smartphone
    Nexus S / HTC Magic 32a

    Ringraziamenti
    1
    Ringraziato 48 volte in 46 Posts
    Predefinito

    Quote Originariamente inviato da Carontes Visualizza il messaggio
    04-14 23:47:54.969: ERROR/AndroidRuntime(1168): java.lang.RuntimeException: Binary XML file line #11: You must supply a layout_height attribute.
    sistema il layout...

  9. #7
    Androidiano


    Registrato dal
    Apr 2011
    Località
    Bologna
    Messaggi
    239
    Smartphone
    Huawei Ascend Y300

    Ringraziamenti
    9
    Ringraziato 95 volte in 36 Posts
    Predefinito

    Sistemato. Ora però mi compare questo errore:

    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): FATAL EXCEPTION: main
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): java.lang.ArrayIndexOutOfBoundsException
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at it.gogle.com.sedactivity$MyExpandableListAdapter.b indView(sedactivity.java:167)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at it.gogle.com.sedactivity$MyExpandableListAdapter.g etChildView(sedactivity.java:150)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.ExpandableListConnector.getView(Exp andableListConnector.java:450)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.AbsListView.obtainView(AbsListView. java:1315)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.ListView.makeAndAddView(ListView.ja va:1727)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.ListView.fillDown(ListView.java:652 )
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.ListView.fillSpecific(ListView.java :1284)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.ListView.layoutChildren(ListView.ja va:1558)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.AbsListView.onLayout(AbsListView.ja va:1147)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.view.View.layout(View.java:7035)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.LinearLayout.setChildFrame(LinearLa yout.java:1249)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.LinearLayout.layoutVertical(LinearL ayout.java:1125)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.LinearLayout.onLayout(LinearLayout. java:1042)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.view.View.layout(View.java:7035)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.FrameLayout.onLayout(FrameLayout.ja va:333)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.view.View.layout(View.java:7035)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.LinearLayout.setChildFrame(LinearLa yout.java:1249)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.LinearLayout.layoutVertical(LinearL ayout.java:1125)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.LinearLayout.onLayout(LinearLayout. java:1042)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.view.View.layout(View.java:7035)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.widget.FrameLayout.onLayout(FrameLayout.ja va:333)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.view.View.layout(View.java:7035)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.view.ViewRoot.performTraversals(ViewRoot.j ava:1045)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.view.ViewRoot.handleMessage(ViewRoot.java: 1727)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.os.Handler.dispatchMessage(Handler.java:99 )
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.os.Looper.loop(Looper.java:123)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at android.app.ActivityThread.main(ActivityThread.jav a:4627)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at java.lang.reflect.Method.invokeNative(Native Method)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at java.lang.reflect.Method.invoke(Method.java:521)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:868)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626)
    04-15 01:25:06.789: ERROR/AndroidRuntime(2332): at dalvik.system.NativeStart.main(Native Method)
    04-15 01:25:06.809: WARN/ActivityManager(498): Force finishing activity it.gogle.com/.sedactivity
    04-15 01:25:07.319: WARN/ActivityManager(498): Activity pause timeout for HistoryRecord{444460d0 it.gogle.com/.sedactivity}

  10. #8
    Androidiano L'avatar di Jordano


    Registrato dal
    Nov 2010
    Località
    Reggio Emilia
    Messaggi
    263
    Smartphone
    Nexus S / HTC Magic 32a

    Ringraziamenti
    1
    Ringraziato 48 volte in 46 Posts
    Predefinito

    leggi bene, hai un errore che esci dagli indice del vettore

  11. #9
    Androidiano


    Registrato dal
    Apr 2011
    Località
    Bologna
    Messaggi
    239
    Smartphone
    Huawei Ascend Y300

    Ringraziamenti
    9
    Ringraziato 95 volte in 36 Posts
    Predefinito

    Si alla fine ho trovato l'errore, era solo un numero sbagliato. Adesso però non riesco a mettere un'immagine diversa per ogni child. Con il codice che ho posso solo settare la stessa immagine per tutti i child oppure dirgli di mettere una certa immagine per tutti i child presenti nella posizione x di un gruppo ed il resto avranno un'altra immagine. Ti ringrazio infinitamente se riesci a risolvermi questo problema altrimenti ti ringrazio lo stesso per l'aiuto che mi hai dato finora



    208
     
    1
    package it.gogle.com;
    2
    3
    4
    import java.util.ArrayList;
    5
    import java.util.HashMap;
    6
    import java.util.List;
    7
    import java.util.Map;
    8
    9
    10
    import android.app.ExpandableListActivity;
    11
    import android.content.Context;
    12
    import android.content.res.Resources;
    13
    import android.graphics.drawable.Drawable;
    14
    import android.os.Bundle;
    15
    import android.view.View;
    16
    import android.view.ViewGroup;
    17
    import android.widget.ExpandableListAdapter;
    18
    import android.widget.ImageView;
    19
    import android.widget.SimpleExpandableListAdapter;
    20
    import android.widget.TextView;
    21
    22
    /**
    23
     * Demonstrates expandable lists using a custom {...@link
    24
    ExpandableListAdapter}
    25
     * from {...@link BaseExpandableListAdapter}.
    26
     */
    27
    public class sedactivity extends ExpandableListActivity { ExpandableListAdapter mAdapter;
    28
    29
           
    30
            private final static String NAME = "NAME";
    31
            private final static String SURNAME = "SURNAME";
    32
            private Resources res;
    33
            private Drawable photo, photo2, photo3, photo4;
    34
            private List<Drawable> albumCovers;
    35
           
    36
           
    37
            @Override
    38
            public void onCreate(Bundle savedInstanceState) {
    39
                    super.onCreate(savedInstanceState);
    40
                    setContentView(R.layout.list_main);
    41
    42
                    // Create a List of Drawables to insert into the Expandable List
    43
                    // This can be completely dynamic
    44
                    res = this.getResources();
    45
                    photo = (Drawable) res.getDrawable(R.drawable.bosco);
    46
                    photo2 = (Drawable) res.getDrawable(R.drawable.albero);
    47
                    photo3 = (Drawable) res.getDrawable(R.drawable.bomba);
    48
                    photo4 = (Drawable) res.getDrawable(R.drawable.astronave);
    49
                    albumCovers = new ArrayList<Drawable>();
    50
                    albumCovers.add(photo);
    51
                    albumCovers.add(photo2);
    52
    53
                    // The following code simply generates the Expandable Lists content (Strings)
    54
                    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    55
                    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    56
                    Map<String, String>
    57
                   
    58
                   
    59
                    //Lista A
    60
                    curGroupMap = new HashMap<String, String>();
    61
                    groupData.add(curGroupMap);
    62
                   
    63
                    curGroupMap.put(NAME, "A");
    64
                    curGroupMap.put(SURNAME, "(2 Foto)");
    65
    66
    67
    68
                    List<Map<String, String>> children = new ArrayList<Map<String, String>>();
    69
                    Map<String, String> curChildMap = new HashMap<String, String>();
    70
                    children.add(curChildMap);
    71
    72
                   
    73
                    curChildMap.put(NAME, "Albero");
    74
                   
    75
                   
    76
                    curChildMap = new HashMap<String, String>();
    77
                    children.add(curChildMap);
    78
    79
                    curChildMap.put(NAME, "Astronave");
    80
                   
    81
                    curChildMap = new HashMap<String, String>();
    82
                    children.add(curChildMap);
    83
    84
                   
    85
                    childData.add(children);
    86
    87
                   
    88
                    //Lista B
    89
                    curGroupMap = new HashMap<String, String>();
    90
                    groupData.add(curGroupMap);
    91
    92
                   
    93
                    curGroupMap.put(NAME, "B");
    94
                    curGroupMap.put(SURNAME, "(2 Foto)");
    95
    96
                    children = new ArrayList<Map<String, String>>();
    97
    98
                    curChildMap = new HashMap<String, String>();
    99
                    children.add(curChildMap);
    100
    101
                    curChildMap.put(NAME, "Bosco");
    102
                   
    103
                    curChildMap = new HashMap<String, String>();
    104
                    children.add(curChildMap);
    105
    106
                    curChildMap.put(NAME, "Bomba");
    107
                   
    108
                    curChildMap = new HashMap<String, String>();
    109
                    children.add(curChildMap);
    110
    111
    112
                    childData.add(children);
    113
                   
    114
                   
    115
                    //Lista C
    116
                    curGroupMap = new HashMap<String, String>();
    117
                    groupData.add(curGroupMap);
    118
    119
                   
    120
                    curGroupMap.put(NAME, "C");
    121
                    curGroupMap.put(SURNAME, "(0 Foto)");
    122
                   
    123
                    children = new ArrayList<Map<String, String>>();
    124
    125
                   
    126
                    childData.add(children);
    127
                   
    128
                    // Set up our adapter
    129
                    mAdapter = new MyExpandableListAdapter(
    130
                                    this,
    131
                                    groupData,
    132
                                    R.layout.list_parent,
    133
                                    new String[] { NAME, SURNAME },
    134
                                    new int[] { R.id.rowText1, R.id.rowText2, R.id.photoAlbumImg },
    135
                                    childData,
    136
                                    R.layout.list_child,
    137
                                    new String[] { NAME, SURNAME },
    138
                                    new int[] { R.id.rowText1, R.id.photoAlbumImg }
    139
                            );
    140
                    setListAdapter(mAdapter);
    141
                    registerForContextMenu(getExpandableListView());
    142
            }
    143
    144
    145
            /**
    146
             * A simple adapter which allows you to bind data to specific
    147
             * Views defined within the layout of an Expandable Lists children
    148
             * (Implement getGroupView() to define the layout of parents)
    149
             */
    150
           
    151
           
    152
            public class MyExpandableListAdapter extends SimpleExpandableListAdapter {
    153
    154
                    private List<? extends List<? extends Map<String, ?>>> mChildData;
    155
                    private String[] mChildFrom;
    156
                    private int[] mChildTo;
    157
    158
                    public MyExpandableListAdapter(Context context,
    159
                                    List<? extends Map<String, ?>> groupData, int groupLayout,
    160
                                    String[] groupFrom, int[] groupTo,
    161
                                    List<? extends List<? extends Map<String, ?>>> childData,
    162
                                    int childLayout, String[] childFrom, int[] childTo) {
    163
                            super(context, groupData, groupLayout, groupFrom, groupTo,
    164
                                  childData, childLayout, childFrom, childTo);
    165
    166
                            mChildData = childData;
    167
                            mChildFrom = childFrom;
    168
                            mChildTo = childTo;
    169
    170
                    }
    171
    172
                    public View getChildView(int groupPosition, int childPosition,
    173
                            boolean isLastChild, View convertView, ViewGroup parent) {
    174
    175
                    View v;
    176
                    if (convertView == null) {
    177
                            v = newChildView(isLastChild, parent);
    178
                    } else {
    179
                            v = convertView;
    180
                    }
    181
                    bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom,
    182
                                    mChildTo, groupPosition, childPosition);
    183
                    return v;
    184
                   
    185
                    }
    186
    187
                 // This method binds my data to the Views specified in the child xml layout
    188
                    private void bindView(View view, Map<String, ?> data, String[] from, int[] to, int groupPosition, int childPosition) {
    189
                            int len = to.length - 1;
    190
                            // Apply TextViews
    191
                            for (int i = 0; i < len; ++i) {
    192
                                    TextView v = (TextView) view.findViewById(to[i]);
    193
                                    if (v != null) {
    194
                                            v.setText((String) data.get(from[i]));
    195
                                    }
    196
                                    // Apply ImageView
    197
                                    ImageView imgV = (ImageView) view.findViewById(to[1]);
    198
                                    if (imgV != null) {
    199
                                        if(childPosition % 1 == 0) imgV.setImageDrawable(albumCovers.get(0));
    200
                                        else imgV.setImageDrawable(albumCovers.get(1));
    201
                                }
    202
     
    203
                                   
    204
                            }
    205
                           
    206
                    }
    207
            }
    208
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy