English 中文(简体)
• 如何在安的安乐斯孤立一个可扩展的清单?
原标题:How to inflate a expandable list in Android?

i 如果使用<代码>albumCovers.get(i),而不是getResources(R.drawable.icon),则每一行各有某些不同的文字和不同的图像,就不必填写一份可扩展的清单。 页: 1

public class ExpandActivity extends ExpandableListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Construct Expandable List
    final String NAME = "name";
    final String IMAGE = "image";
    final LayoutInflater layoutInflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();

    final HashMap<String, String> group1 = new HashMap<String, String>();
    group1.put(NAME, "Group 1");
    headerData.add(group1);

    final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();

    final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
    childData.add(group1data);

    Resources res = this.getResources();
    Drawable photo = (Drawable) res.getDrawable(R.drawable.icon);
    ArrayList<Drawable> albumCovers = new ArrayList<Drawable>();
    albumCovers.add(photo);
    // Set up some sample data in both groups
    for (int i = 0; i < 10; ++i) {
        final HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(NAME, "Child " + i);
        map.put(IMAGE, getResources().getDrawable(R.drawable.icon));
        (group1data).add(map);
    }

    setListAdapter(new SimpleExpandableListAdapter(this, headerData,
            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);

            // Populate your custom view here
            ((TextView) v.findViewById(R.id.name))
                    .setText((String) ((Map<String, Object>) getChild(
                            groupPosition, childPosition)).get(NAME));
            ((ImageView) v.findViewById(R.id.image))
                    .setImageDrawable((Drawable) ((Map<String, Object>) getChild(
                            groupPosition, childPosition)).get(IMAGE));

            return v;
        }

        @Override
        public View newChildView(boolean isLastChild, ViewGroup parent) {
            return layoutInflater.inflate(
                    R.layout.expandable_list_item_with_image, null, false);
        }
    });
}
}
最佳回答

页: 1 更改<代码>albumCovers.get(i)至albumCovers.get(0),供委员会工作。

关键地点是:

Drawable photo = (Drawable) res.getDrawable(R.drawable.icon); 
ArrayList<Drawable> albumCovers = new ArrayList<Drawable>();
albumCovers.add(photo); // HERE: photo added only once!

for (int i = 0; i < 10; ++i) {
    final HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(NAME, "Child " + i);
    map.put(IMAGE, getResources().getDrawable(R.drawable.icon)); // HERE: you wanted to use albumCovers.get(i) here. There is only 1 photo in albumCovers but you try to get images 0...9 from it using index i. So, change this to albumCovers.get(0) OR add 10x photo into albumCovers.
    (group1data).add(map);
}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签