我正在开发expandablelistview,但是我无法获得可扩展的listView,请提前感谢我帮助我在代码中出错
CodeMainActicity.java
public class CodeMainActicity extends Activity {
private ExpandListAdapter ExpAdapter;
private ArrayList<ExpandListGroup> ExpListItems;
private ExpandableListView ExpandList;
public String[] groups = null;
public String[][] headers = null;
DisplayMetrics metrics;
int width;
public int GetDipsFromPixel(float paramFloat) {
return (int) (0.5F + paramFloat
* getResources().getDisplayMetrics().density);
}
public ArrayList<ExpandListGroup> SetStandardGroups() {
ArrayList<ExpandListGroup> localArrayList1 = new ArrayList<ExpandListGroup>();
int i = 0;
if (i >= groups.length) {
return localArrayList1;
}
ExpandListGroup localExpandListGroup = new ExpandListGroup();
localExpandListGroup.setName(groups[i]);
ArrayList<ExpandListChild> localArrayList2 = new ArrayList<ExpandListChild>();
for (int j = 0; j < 10; j++) {
while (headers[i][j] == null) {
localExpandListGroup.setItems(localArrayList2);
localArrayList1.add(localExpandListGroup);
i++;
break;
}
ExpandListChild localExpandListChild = new ExpandListChild();
localExpandListChild.setName(headers[i][j]);
localExpandListChild.setTag(null);
localArrayList2.add(localExpandListChild);
}
return localArrayList1;
}
public void onCreate(Bundle paramBundle) {
groups = new String[11];
headers = ((String[][]) Array.newInstance(String.class, new int[] { 14,
10 }));
groups[0] = "Data Types";
groups[1] = "Date Time";
groups[2] = "Regular Expressions";
groups[3] = "Class";
groups[4] = "Collections";
groups[5] = "Generics";
groups[6] = "Reflection";
groups[7] = "Security";
groups[8] = "Design Patterns";
groups[9] = "Thread";
groups[10] = "File Stream";
headers[0][0] = "Boxing and Unboxing";
headers[0][1] = "Complex Type";
headers[0][2] = "Enum";
headers[0][3] = "Tuple type";
headers[0][4] = "Hex to Int";
headers[0][5] = "Nullable Integer";
headers[0][6] = "Custom ToString()";
headers[0][7] = "Checked() and Unchecked()";
headers[0][8] = "Overflow";
headers[0][9] = "Char methods";
headers[1][0] = "Get between Days";
headers[1][1] = "Add one week to current date";
headers[1][2] = "DateTime Compare";
headers[2][0] = "Email Validation";
headers[2][1] = "URL validation";
headers[2][2] = "Zipcode validation";
headers[3][0] = "Access modifiers";
headers[3][1] = "Inheritance";
headers[3][2] = "Interface";
headers[3][3] = "Method Overloading";
headers[3][4] = "Overriding";
headers[3][5] = "Abstract class";
headers[4][0] = "Array List";
headers[4][1] = "Hash Table";
headers[4][2] = "Dictionary";
headers[5][0] = "Simple Generic hierarchy";
headers[5][1] = "Generic IEnumerable";
headers[5][2] = "Generic Collection class";
headers[6][0] = "AppDomain Setup";
headers[6][1] = "Type.GetMethods";
headers[6][2] = "Assembly Version Information";
headers[7][0] = "MD5 encode";
headers[7][1] = "File Checksum";
headers[7][2] = "Password Encryption";
headers[8][0] = "Facade Pattern";
headers[8][1] = "Factory Pattern";
headers[8][2] = "Observer Pattern";
headers[8][3] = "Singleton Pattern";
headers[8][4] = "Adapter Pattern";
headers[9][0] = "Creating Thread";
headers[9][1] = "Asynchronous Calls";
headers[9][2] = "Mutex";
headers[10][0] = "Display File contents";
headers[10][1] = "Write to a file";
headers[10][2] = "Append to File";
super.onCreate(paramBundle);
requestWindowFeature(1);
setContentView(R.layout.codemainscreen);
Button localButton = (Button) findViewById(R.id.btnhome);
ExpandList = ((ExpandableListView) findViewById(R.id.ExpList));
ExpListItems = SetStandardGroups();
ExpAdapter = new ExpandListAdapter(this, ExpListItems);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
ExpandList.setIndicatorBounds(width - GetDipsFromPixel(50.0F), width
- GetDipsFromPixel(10.0F));
ExpandList.setAdapter(ExpAdapter);
ExpandList
.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
public boolean onChildClick(
ExpandableListView paramAnonymousExpandableListView,
View paramAnonymousView, int paramAnonymousInt1,
int paramAnonymousInt2, long paramAnonymousLong) {
Bundle localBundle = new Bundle();
localBundle.putString("group",
Integer.toString(paramAnonymousInt1));
localBundle.putString("item",
Integer.toString(paramAnonymousInt2));
Intent localIntent = new Intent(CodeMainActicity.this
.getApplicationContext(),
CodeDisplayAcitvity.class);
localIntent.putExtras(localBundle);
CodeMainActicity.this.startActivity(localIntent);
return false;
}
});
localButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramAnonymousView) {
Intent localIntent = new Intent(CodeMainActicity.this
.getApplicationContext(), MainActivity.class);
CodeMainActicity.this.startActivity(localIntent);
}
});
}
}
ExpandListAdapter:
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ExpandListGroup> groups;
public ExpandListAdapter(Context paramContext,
ArrayList<ExpandListGroup> paramArrayList) {
this.context = paramContext;
this.groups = paramArrayList;
}
public void addItem(ExpandListChild paramExpandListChild,
ExpandListGroup paramExpandListGroup) {
if (!this.groups.contains(paramExpandListGroup))
this.groups.add(paramExpandListGroup);
int i = this.groups.indexOf(paramExpandListGroup);
ArrayList<ExpandListChild> localArrayList = ((ExpandListGroup) this.groups
.get(i)).getItems();
localArrayList.add(paramExpandListChild);
((ExpandListGroup) this.groups.get(i)).setItems(localArrayList);
}
public Object getChild(int paramInt1, int paramInt2) {
return ((ExpandListGroup) this.groups.get(paramInt1)).getItems().get(
paramInt2);
}
public long getChildId(int paramInt1, int paramInt2) {
return paramInt2;
}
public View getChildView(int paramInt1, int paramInt2,
boolean paramBoolean, View paramView, ViewGroup paramViewGroup) {
ExpandListChild localExpandListChild = (ExpandListChild) getChild(
paramInt1, paramInt2);
if (paramView == null)
paramView = ((LayoutInflater) this.context
.getSystemService("layout_inflater")).inflate(
R.layout.expandlist_child_item, null);
TextView localTextView = (TextView) paramView
.findViewById(R.id.tvChild);
localTextView.setText(localExpandListChild.getName().toString());
localTextView.setTag(localExpandListChild.getTag());
return paramView;
}
public int getChildrenCount(int paramInt) {
return ((ExpandListGroup) this.groups.get(paramInt)).getItems().size();
}
public Object getGroup(int paramInt) {
return this.groups.get(paramInt);
}
public int getGroupCount() {
return this.groups.size();
}
public long getGroupId(int paramInt) {
return paramInt;
}
public View getGroupView(int paramInt, boolean paramBoolean,
View paramView, ViewGroup paramViewGroup) {
ExpandListGroup localExpandListGroup = (ExpandListGroup) getGroup(paramInt);
if (paramView == null) {
paramView = ((LayoutInflater) this.context
.getSystemService("layout_inflater")).inflate(
R.layout.expandlist_group_item, null);
}
((TextView) paramView.findViewById(R.id.btngo))
.setText(localExpandListGroup.getName());
return paramView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int paramInt1, int paramInt2) {
return true;
}
}
ExpandListChild:
public class ExpandListChild {
private String Name;
private String Tag;
public String getName() {
return this.Name;
}
public String getTag() {
return this.Tag;
}
public void setName(String paramString) {
this.Name = paramString;
}
public void setTag(String paramString) {
this.Tag = paramString;
}
}
ExpandListGroup:
public class ExpandListGroup {
private ArrayList<ExpandListChild> Items;
private String Name;
public ArrayList<ExpandListChild> getItems() {
return this.Items;
}
public String getName() {
return this.Name;
}
public void setItems(ArrayList<ExpandListChild> paramArrayList) {
this.Items = paramArrayList;
}
public void setName(String paramString) {
this.Name = paramString;
}
}
答案 0 :(得分:6)
SetStandardGroups() - 您只创建一个ExpandListGroup实例并向该组添加项目。您没有将第二个组项添加到结果ArrayList(localArrayList1)。尝试使用以下代码...
public ArrayList<ExpandListGroup> SetStandardGroups()
{
ArrayList<ExpandListGroup> resExpList = new ArrayList<ExpandListGroup>();
if(groups != null && groups.length > 0)
{
for(int i=0; i<groups.length; i++)
{
ExpandListGroup expListGroup = new ExpandListGroup();
expListGroup.setName(groups[i]);
Log.e("ExpList", "Group "+i+" Name="+groups[i]);
ArrayList<ExpandListChild> expListItems = new ArrayList<ExpandListChild>();
for(int j=0; j<headers[i].length; j++)
{
ExpandListChild item = new ExpandListChild();
item.setName(headers[i][j]);
Log.e("ExpList", "Item "+j+" ="+headers[i][j]);
item.setTag(null);
expListItems.add(item);
}
expListGroup.setItems(expListItems);
Log.e("ExpList", "Group "+i+" items size="+expListGroup.size());
resExpList.add(expListGroup);
}
}
Log.e("ExpList", "Total groups size="+resExpList.size());
return resExpList;
}