Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在列表中组合imageView和TextView_Android_Android Listview - Fatal编程技术网

Android 在列表中组合imageView和TextView

Android 在列表中组合imageView和TextView,android,android-listview,Android,Android Listview,您好,我正在android中开发一个应用程序,我使用一个子类扩展BaseExpandableListAdapter。现在我无法在显示的列表中组合ImageView和TextView。昨天我在stackoverflow上找到了这个链接,它帮助我实现了这个组合 所以它很好用直到我点击一个列表项。应用程序chrash和logcat告诉我 “android.widget.RelativeLayout”不能强制转换为android.widget.ImageView。此异常来自扩展BaseExpandab

您好,我正在android中开发一个应用程序,我使用一个子类扩展
BaseExpandableListAdapter
。现在我无法在显示的列表中组合
ImageView
TextView
。昨天我在stackoverflow上找到了这个链接,它帮助我实现了这个组合

所以它很好用直到我点击一个列表项。应用程序chrash和logcat告诉我
“android.widget.RelativeLayout”不能强制转换为android.widget.ImageView。
此异常来自扩展
BaseExpandableListAdapter
的类中的
getGroupView()
。为什么会发生这种情况?(
RelativeLayout
扩展
视图

当我试图返回一个
RelativeLayout
而不是
ImageView
时,我是否完全走错了路

以下是我在
getGroupView
中的代码(我有点乱,因为我处于测试状态):


语句
ImageView行=(ImageView)convertView
给出异常,因为您正在尝试将
RelativeLayout
转换为
ImageView

您必须在
getViewGroup()
方法中返回
convertView
,但您正在返回相对值,即
return rLayout

解决方案:


相对视图
中添加
图像视图
文本视图
。通过在getView()方法中膨胀ListItem来设置该布局,并将膨胀的布局分配给convertView。在getView()方法中返回convertView

谢谢,我也在考虑这个方向,但我认为有可能以某种方式投下一票。。也许会反对???但我现在通过使用LayoutInflater并将imageview和textview放在xml文件中解决了这个问题。
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

  ImageView row = (ImageView) convertView;

RelativeLayout rLayout = new RelativeLayout(mContext);
  LayoutParams rlParams = new LayoutParams(LayoutParams.FILL_PARENT
            ,LayoutParams.FILL_PARENT); 
    rLayout.setLayoutParams(rlParams);


if(row == null) {

    row = new ImageView(mContext);
}

row.setImageResource(R.drawable.ic_launcher);

row.setScaleType(ImageView.ScaleType.FIT_START);


RelativeLayout.LayoutParams tParams = new RelativeLayout.LayoutParams
        (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
tParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
TextView text=new TextView(mContext); 
text.setText("GOLDEN Gate"); 
text.setTextColor(Color.WHITE);                            
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setLayoutParams(tParams);



rLayout.addView(row);
rLayout.addView(text);

return rLayout;
}