我有一项活动,显示用户喜欢的汽车(在汽车经销商应用程序中)。汽车可以是不同类型的,所以我使用ExpandableListView对小组中的汽车进行分类,这些小组在点击时展开,并显示该组中的儿童(汽车),如果有的话。
我想在组名旁边显示一个数字,以便可以看到(在点击之前)该特定组/类别中有多少个孩子/汽车。
我似乎无法找到任何现有方法来执行此操作?尝试使用groupIndicator,但我不想弄乱它,而是保留它并在组名之后(右侧)显示一个数字。
是否可能?
这是负责的活动
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();
}
}
}
和相应的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" />
答案 0 :(得分:0)
您应该扩展BaseExpandableListAdapter
并为组视图和子视图创建自己的布局。在这里,您将找到一个很好的例子:ExpandableListView with Custom Adapter