我在android中创建了一个非常简单的expandableList,现在我想点击该列表的groupitems时应该更改它的颜色,我尝试了下面的代码,但是没有用。请建议我该怎么做。 。 的 main.java package com.example.expandablelistdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
ExpandableListView el;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
el=(ExpandableListView)findViewById(R.id.expandableListView1);
Child c;
Group group;
String[] grup={"fruit","vehicle","cedans","animals"};
ArrayList<Group> groups=new ArrayList<Group>();
ArrayList<Child> childrens=new ArrayList<Child>();
String[] fruit={"Apple","banana","Cherry","Orange"};
String[] vehicle={"cycle","scooter","chopper","truck"};
String[] cedans={"Bmw","jaguar","masserratti","Buggati","Karma"};
String[] planes={"AirBus","Mig42","jet"};
for(String str:grup){
if(str.equalsIgnoreCase("fruit")){
childrens =new ArrayList<Child>();
for(String cname:fruit){
Child child =new Child(cname, cname);
childrens.add(child);
}
}
else if(str.equalsIgnoreCase("vehicle")){
childrens =new ArrayList<Child>();
for(String cname:vehicle){
Child child =new Child(cname, cname);
childrens.add(child);
}
}
else if(str.equalsIgnoreCase("cedans")){
childrens =new ArrayList<Child>();
for(String cname:cedans){
Child child =new Child(cname, cname);
childrens.add(child);
}
}
else if(str.equalsIgnoreCase("planes")){
childrens =new ArrayList<Child>();
for(String cname:planes){
Child child =new Child(cname, cname);
childrens.add(child);
}
} group = new Group(str, str, childrens);
groups.add(group);
}
ExpandableListAdapter ela =new ExpandableListAdapter(getApplicationContext(), groups);
el.setAdapter(ela);
el.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
((TextView)v).setTextColor(Color.RED);
return false;
}
});
}
}