CERCA
PER MODELLO
FullScreen Chatbox! :)

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

Visualizzazione dei risultati da 1 a 1 su 1
Discussione:

Come inserire una activity in un textview

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
    Predefinito

    Come inserire una activity in un textview

    Salve, io ho questo codice


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



    Come faccio a far partire l'activity riguardante "Albero" "Bomba" e via dicendo? Il file xml è composto da un imageview e a destra c'è un textview. Spero di essere stato comprensibile. Grazie.

  2.  

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