我想使用不同的图像代替文本字符串作为可扩展列表视图的子代。并且在这些图像的单击事件上,应打开不同的子布局。任何人都可以告诉我如何实现这一目标?我已经通过了很多代码但无法找到正确的答案。我不明白如何从可绘制文件夹中获取可扩展列表视图中的图像。我知道如何从字符串中获取文本,但我使用了数组用于图像,并且不知道如何添加这些文本。我是android的新手,所以不知道正确的代码格式。我正在尝试这个代码,但它给出了logcat中显示的错误。
MainActivity.java
public class MainActivity extends Activity {
protected static final String TAG = null;
ExpandableListView expListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListAdapter adapter=new ExpandableListAdapter(this);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
expListView.setAdapter(adapter);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " expanded.");
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " collapsed.");
}
});
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.i(TAG, "item " + childPosition + " of group " + groupPosition + " clicked.");
return false;
}
});
int count = adapter.getGroupCount();
for (int position = 1; position <= count; position++)
expListView.expandGroup(position - 1);
}
}
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter
{
private final String TAG = "ExpAdapter";
private Context context;
static final int[] arrGroupelements= {R.drawable.header_myphotos,R.drawable.header_aboutme,R.drawable.header_privacy,R.drawable.header_connect,R.drawable.header_sharing};
static final String arrChildelements[][] = { {"Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" },
{"Ponting", "Adam Gilchrist", "Michael Clarke"},
{"Andrew Strauss", "kevin Peterson", "Nasser Hussain"},
{"Graeme Smith", "AB de villiers", "Jacques Kallis"} };
public ExpandableListAdapter(Context context){
this.context=context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.lblListItem);
tvItem.setText(arrChildelements[groupPosition][childPosition]);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return arrChildelements[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return arrGroupelements.length;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_group, null);
}
ImageButton ib = (ImageButton) convertView.findViewById(R.id.lblListHeader);
ib.setImageResource(arrGroupelements[groupPosition]);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
logCat错误:
09-18 11:27:23.465: E/AndroidRuntime(1688): FATAL EXCEPTION: main
09-18 11:27:23.465: E/AndroidRuntime(1688): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.settingsui/com.example.settingsui.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.os.Looper.loop(Looper.java:137)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-18 11:27:23.465: E/AndroidRuntime(1688): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 11:27:23.465: E/AndroidRuntime(1688): at java.lang.reflect.Method.invoke(Method.java:525)
09-18 11:27:23.465: E/AndroidRuntime(1688): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-18 11:27:23.465: E/AndroidRuntime(1688): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-18 11:27:23.465: E/AndroidRuntime(1688): at dalvik.system.NativeStart.main(Native Method)
09-18 11:27:23.465: E/AndroidRuntime(1688): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
09-18 11:27:23.465: E/AndroidRuntime(1688): at com.example.settingsui.ExpandableListAdapter.getChildrenCount(ExpandableListAdapter.java:58)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.widget.ExpandableListView.expandGroup(ExpandableListView.java:748)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.widget.ExpandableListView.expandGroup(ExpandableListView.java:732)
09-18 11:27:23.465: E/AndroidRuntime(1688): at com.example.settingsui.MainActivity.onCreate(MainActivity.java:49)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.app.Activity.performCreate(Activity.java:5133)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-18 11:27:23.465: E/AndroidRuntime(1688): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-18 11:27:23.465: E/AndroidRuntime(1688): ... 11 more
09-18 11:32:23.823: I/Process(1688): Sending signal. PID: 1688 SIG: 9
答案 0 :(得分:0)
试试这个示例代码,
我是这么认为的,可能你需要改变listview的行布局。