Pergunta

I have an activity that displays a users favorited cars (in a car dealership app). Cars can be of different types so I am using an ExpandableListView to categorize the cars in groups that upon a tap expand and displays the children (cars) of that group if there are any.

I would like to display a number next to the group name so that it is possible to see (before tapping) how many children/cars there are in that particular group/category.

I can't seem to find any existing methods to do this? Tried with the groupIndicator but I do not want to mess with that, rather keep it and display a number after (to the right) of the group name.

Is it at all possible?

Here is the Activity responsible

public class FavActivity extends ExpandableListActivity {
private GetFavsTask carsTask;

private String[] groups = {
        "Sedan",
        "Convertible",
        "SUV",
        "Bus",
        "Lynbud"
};

private List<? extends List<? extends Map<String, ?>>> childList;

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

    setContentView(R.layout.activity_fav);

    ((Button)findViewById(R.id.btnBack)).setVisibility(View.VISIBLE);

    Typeface tf = Typeface.createFromAsset(getAssets(), "font.ttf");
    ((TextView)findViewById(R.id.title)).setTypeface(tf);
    ((TextView)findViewById(R.id.title)).setText("website");

    carsTask = new GetFavsTask();
    carsTask.execute();

    getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            HashMap child = (HashMap) childList.get(groupPosition).get(childPosition);
            Log.i("List", "id "+child.get("id")+" "+child.get("child"));

            Intent i = new Intent(FavActivity.this, CarActivity.class);
            i.putExtra("id", 
                    Integer.parseInt(""+child.get("id"))
                    );
            FavActivity.this.startActivity(i);
            return true;
        }
    });
    App.loadAd(this);
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnBack: onBackPressed();
    break;
    case R.id.btnHome:
        App.showMenu(this);
        break;
    }
}

@Override
public void onBackPressed() {
    /*Intent login = new Intent(this, LoginActivity.class);
    startActivityForResult(login, 1);*/
    super.onBackPressed();
}

private List getGroupList() {
    ArrayList result = new ArrayList();
    for( int i = 0 ; i < groups.length ; ++i ) {
        HashMap m = new HashMap();
        m.put( "group",groups[i] );
        result.add( m );
    }
    return result;
}

public class GetFavsTask extends AsyncTask<Void, Void, List> {
    private ProgressDialog pd;

    private GetFavsTask.Callback cb;

    public void setCallback(GetFavsTask.Callback c) {
        cb = c;
    }

    @Override
    protected void onPostExecute(List children) {
        if (children.size()==0) {
            Toast.makeText(FavActivity.this,
                    "Du har ingen favoritter endnu",
                    Toast.LENGTH_LONG).show();
        }
        childList = children;
        setListAdapter(new SimpleExpandableListAdapter(
                FavActivity.this,
                getGroupList(),
                R.layout.group_row,
                new String[] { "group" },
                new int[] { R.id.tv_group },
                children,
                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);

                    String lb = (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("lb");
                    if (lb.matches("1")) {
                        ((TextView) v.findViewById(R.id.tv_bid)).setVisibility(View.VISIBLE);
                    } else {
                        ((TextView) v.findViewById(R.id.tv_bid)).setVisibility(View.GONE);
                    }

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

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

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

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

                    Bitmap thumb = (Bitmap) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("thumb");
                    if (thumb!=null) {
                        ((ImageView) v.findViewById(R.id.iv)).setImageBitmap(thumb);
                    } else {
                        ((ImageView) v.findViewById(R.id.iv)).setImageResource(android.R.drawable.ic_menu_report_image);
                    }

                    return v;
                }

                @Override
                public View newChildView(boolean isLastChild, ViewGroup parent) {
                     return FavActivity.this.getLayoutInflater().inflate(R.layout.child_row, null, false);
                }
            }
        );

        if (pd!=null && pd.isShowing()) {
            pd.dismiss();
        }


        if (cb!=null) {
            cb.onFinish();
        }
    }

    @Override
    protected void onPreExecute() {
        pd = ProgressDialog.show(FavActivity.this, "ebb.dk", "Henter Favoritter...", true, false);
    }

    @Override
    protected List doInBackground(Void... params) {  
        NumberFormat nf = NumberFormat.getInstance();

        if (App.getCars(FavActivity.this)==null){
            Server.getCars(FavActivity.this);
        }

        ArrayList children = new ArrayList();
        ArrayList type1 = new ArrayList();
        ArrayList type2 = new ArrayList();
        ArrayList type3 = new ArrayList();
        ArrayList type4 = new ArrayList();
        ArrayList type7 = new ArrayList();
        ArrayList type8 = new ArrayList();
        ArrayList type9 = new ArrayList();
        ArrayList lb = new ArrayList();

        List<Car> favs = new ArrayList<Car>();
        JSONArray a = App.getFavs(FavActivity.this);

        for (int i=0;i<a.length();i++){
            favs.add(App.getCar(FavActivity.this, a.optInt(i)));
        }

        for (Car c : favs) {    
            if (c==null) { continue; }

            String km = c.km;
            String price = c.price;

            try {
                km = nf.format(nf.parse(c.km));
                price = nf.format(nf.parse(c.price));
            } catch (ParseException e) {

            }

            HashMap child = new HashMap();
            child.put("lb", c.LB);
            child.put("make_model", c.make+" "+c.model);
            child.put("motor_year", c.motor+" - "+c.year);
            child.put("km", km+" km");
            if (c.LB.matches("1")) {
                Server.getBids(FavActivity.this, c);
                if (c.bids!=null && c.bids.size()>0) {
                    String p = c.bids.get(c.bids.size()-1).getKey();

                    try {
                        p = nf.format(nf.parse(p));
                    } catch (ParseException e) {

                    }

                    child.put("price", p+" kr.");
                }
            } else {
                child.put("price", price+" kr.");
            }
            child.put("id", c.id);

            if (c.picture!=null && c.picture.length()>0) {
                String thumbUrl = App.IMG_URL+c.id+"/thumbs/"+c.picture;
                child.put("thumb", Server.getBitmap(FavActivity.this, thumbUrl, false));
            }

            if (c.LB.matches("1")) {
                lb.add(child);
            } else {
                switch (c.type) {
                case 1: type1.add(child);
                    break;
                case 2: type2.add(child);
                    break;
                case 3: type3.add(child);
                    break;
                case 4: type4.add(child);
                    break;
                case 7: type7.add(child);
                break;
                case 8: type8.add(child);
                break;
                case 9: type9.add(child);
                break;
                default:
                }
            }


        }

        children.add(type1);
        children.add(type2);
        children.add(type3);
        children.add(type4);
        children.add(type7);
        children.add(type8);
        children.add(type9);
        children.add(lb);

        return children;
    }

    public abstract class Callback {
        public abstract void onFinish();
    }
}

}

And the corresponding XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d6d6d6" >

<include
    android:layout_width="match_parent"
    android:layout_height="50dp"
    layout="@layout/title_bar" />

<TextView
    android:id="@+id/android:empty1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_below="@+id/title_bar"
    android:text="@string/favs"
    android:textColor="#000000" />

<ExpandableListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/webView1"
    android:layout_below="@+id/android:empty1"
    android:cacheColorHint="#d6d6d6"
    android:drawSelectorOnTop="true"
    android:fastScrollEnabled="true" >

</ExpandableListView>

<TextView
    android:id="@+id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/no_cars"
    android:textColor="#000000" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/bg" />
<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true" />

Foi útil?

Solução

You should extend BaseExpandableListAdapter and create your own layouts for group view and child views. Here you'll find good example: ExpandableListView with Custom Adapter

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top