سؤال

I have an expandable list in android and I can fill it with string easily but now I want to add a drawable on each row of the list (a different one on each row), what's the easiest way to do this with the code I already have? Thanks.

public class Physical extends ExpandableListActivity {
public static final int GROUPS = 1;
public SimpleExpandableListAdapter expListAdapter;
private boolean expanded = true;

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

    String [] cats = { "phy1", "phys2" };

    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    for (int i = 0; i < GROUPS; i++) {
        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put( "group", "Categories" );

        List<Map<String, String>> children = new ArrayList<Map<String, String>>();
        for( int n = 0 ; n < cats.length ; n++ ) {
            Map<String, String> curChildMap = new HashMap<String, String>();
            curChildMap.put( "child", cats[n] );
            children.add(curChildMap);
        }
        childData.add(children);
    }

    expListAdapter =
            new SimpleExpandableListAdapter(
                    /* context */
                    this,
                    /* creates Group list */
                    groupData, 
                    /*Group item layout XML */
                    R.layout.group_row,    
                    /* the key of each group element (not child's) */
                    new String[] { "group" },
                    /* data under the key goes into this TextView */
                    new int[] { R.id.group_text }, 
                    /* creates Child List (sub-level entries) */
                    childData,
                    /* layout for children in list */
                    R.layout.child_sub,
                    /* the key of each child element */
                    new String[] { "child" },
                    /* data under the child keys go into this textView */
                    new int[] { R.id.child_text }
                );
            /* sets up and initializes the adapter for the list */
            setListAdapter(expListAdapter);

            getExpandableListView().setOnGroupExpandListener( new OnGroupExpandListener() {
                 public void onGroupExpand(int groupPosition) {
                     expanded = true;
                   }
               });
            getExpandableListView().setOnGroupCollapseListener( new OnGroupCollapseListener() {
                public void onGroupCollapse(int groupPosition) {
                    expanded = false;
                }
            });
            if (expanded != false ) {
                getExpandableListView().expandGroup(0);
            }
}
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, final int childPosition, long id) {
    switch (childPosition) {
    case 0:
        Intent spine_plus_intent = new Intent(Physical.this, SpinePlus.class);
        startActivity(spine_plus_intent);
        finish();
        break;
    }
    return true;
}

}

هل كانت مفيدة؟

المحلول

you'd define a custom expandable list adapter and redefine the getchildview / getgroupview.

From there you have 2 routes to follow: one, you can programmatically add the imageviews into the child/group view (i'd discourage you from this approach), or you define an xml layout file for the child/group view and inflate it (and possibly edit the content dynamically)

Simple adapters aren't flexible enough to achieve good results. Expecially when you want custom views for their elements. (Not to mention scalability: always project and code keeping in mind that your requirements may enlarge in the future and a flexible base allows more freedom and prevents you from scratching too much code)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top