ArrayAdapter仅显示一条记录

时间:2012-05-29 23:12:34

标签: android

下午好,

之前我曾使用SimpleAdapter等其他适配器问过这个问题,但简单适配器的解决方案不适用于ArrayAdapter。在SimpleAdapter中,我获得了屏幕宽度和宽度。高度然后传递给我的RelativeLayout数据行。结果是,一次只显示一个填满整个屏幕的记录。这对静态数据来说很好,但现在我想使用可变数据。总之,我真的不想通过高度和宽度。我的xml布局使用dip,因此屏幕尺寸可以改变他们想要的一切。我真正想要的是一次控制getView调用到一个记录的方法。

无论如何使用SimpleAdapter的解决方法我认为像以前一样传递高度和宽度会起作用但是出于某种原因这次我得到了一个我无法弄清楚的错误。以下是设置宽度和高度的代码:

RelativeLayout.LayoutParams szParms = new RelativeLayout.LayoutParams(mWidth, mHeight);
vw_BigRow.setLayoutParams(szParms);

高度和宽度在别处确定,并且在我测试过的所有设备上都是准确的。 不确定还有什么要发布,所以请告诉我,我将该代码/信息添加到问题中。

E/AndroidRuntime(404): FATAL EXCEPTION: main
E/AndroidRuntime(404): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams

修改 正如已经正确指出我的错误发生在CastClassException但是我仍然没有正确的分配,因为我继续得到错误。我已尝试在活动中使用以下内容设置高度/宽度:

AbsListView.LayoutParams szParms = new AbsListView.LayoutParams(mWidth, mHeight);
lstvw_LiftData.setLayoutParams(szParms);

E/AndroidRuntime(1886): FATAL EXCEPTION: main
E/AndroidRuntime(1886): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams

ListView.LayoutParams szParms = new ListView.LayoutParams(mWidth, mHeight);
lstvw_LiftData.setLayoutParams(szParms);

E/AndroidRuntime(1886): FATAL EXCEPTION: main
E/AndroidRuntime(1886): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams

lstvw_LiftData是一个有效的ref而不是null。

在ArrayAdapter的getView()重写中,我也尝试使用相同的结果设置值     @覆盖     public View getView(int position,View convertView,ViewGroup parent)     {...}

我唯一可以上班的就是这个,然后是一对,不是全部,但是我的一些元素忽略了它们的布局定位并且走向了底层。

View vw_BigRow = convertView;

if (vw_BigRow == null) 
{
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    vw_BigRow = inflater.inflate(R.layout.liftdatalayout, null);
}

vw_BigRow.setMinimumHeight(mHeight);
vw_BigRow.setMinimumWidth(mWidth);

1 个答案:

答案 0 :(得分:1)

1.如果你想要call to just one record at a time,只需控制你实现的适配器的getCount返回1.

2. java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams例外是因为要添加到列表视图的视图应使用 AbsListView.LayoutParams 而不是其他LayoutParams,例如RelativeLayout$LayoutParams

修改

请参阅以下谷歌推荐的getView代码。

/**
 * Make a view to hold each row.
 *
 * @see android.widget.ListAdapter#getView(int, android.view.View,
 *      android.view.ViewGroup)
 */
public View getView(int position, View convertView, ViewGroup parent) {
    // A ViewHolder keeps references to children views to avoid unneccessary calls
    // to findViewById() on each row.
    ViewHolder holder;

    // When convertView is not null, we can reuse it directly, there is no need
    // to reinflate it. We only inflate a new View when the convertView supplied
    // by ListView is null.
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

        // Creates a ViewHolder and store references to the two children views
        // we want to bind data to.
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.icon = (ImageView) convertView.findViewById(R.id.icon);

        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();
    }

    // Bind the data efficiently with the holder.
    holder.text.setText(DATA[position]);
    holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

    return convertView;
}

static class ViewHolder {
    TextView text;
    ImageView icon;
}