展開可能なリスト ビューの子として、テキスト文字列の代わりに別の画像を使用したいと考えています。これらの画像のクリックイベントで、さまざまな子レイアウトが開くはずです。どうすればこれを達成できるか誰にもわかりますか? 多くのコードを試しましたが、正しい答えが見つかりません。ドローアブル フォルダーから展開可能なリスト ビューの画像を取得する方法がわかりません。文字列からテキストを取得する方法は知っていますが、画像に配列を使用しており、これらを追加する方法がわかりません。私はアンドロイドが初めてなので、適切なコード形式について知りません。このコードを試していますが、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