我在我的应用中使用了一个简单的列表视图。 在我的adpater中,我正在调整元素的大小。当我在Nexus5(Lollipop)上运行它但在Moto E(Kitkat)上崩溃时它工作正常。
我的adpater是
package com.hj.app.adapters;
import java.io.IOException;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout.LayoutParams;
import com.hj.app.R;
import com.hj.app.holders.HJFeedItemViewHolder;
import com.hj.app.objects.HJFeedItemObject;
public class FeedListViewAdapter extends BaseAdapter {
public ArrayList<HJFeedItemObject> objects;
Context mContext;
public FeedListViewAdapter(Context context) {
this.mContext = context;
this.objects = new ArrayList<HJFeedItemObject>();
}
@Override
public int getCount() {
return objects == null ? 0 : objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.hj_feed_recycle_item_layout, null);
HJFeedItemViewHolder holder = new HJFeedItemViewHolder(convertView);
convertView.setTag(holder);
}
HJFeedItemViewHolder holder = (HJFeedItemViewHolder) convertView
.getTag();
holder.feedTitle.setText(objects.get(position).getFeedTitle());
holder.feedPostTime.setText(objects.get(position).getFeedGenTime());
holder.sourceUserName.setText(objects.get(position).getFeedSourceObj()
.getUserName());
holder.sourceImage.setImageResource(R.drawable.ic_launcher);
try {
Bitmap bitmap;
if (position % 3 == 0) {
bitmap = BitmapFactory.decodeStream(mContext.getAssets().open(
"houzz1.jpg"));
} else if (position % 3 == 1) {
bitmap = BitmapFactory.decodeStream(mContext.getAssets().open(
"houzz2.jpg"));
} else {
bitmap = BitmapFactory.decodeStream(mContext.getAssets().open(
"houzz3.png"));
}
LayoutParams lp;
if (bitmap.getHeight() < 900) {
lp = new LayoutParams(LayoutParams.MATCH_PARENT, 900);
} else {
lp = new LayoutParams(LayoutParams.MATCH_PARENT,
bitmap.getHeight());
}
holder.feedPicFrame.setLayoutParams(lp);
holder.feedPic.setImageBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
lp1.bottomMargin = (int) mContext.getResources().getDimension(
R.dimen.hj_margin_large);
if (position == 0) {
lp1.topMargin = (int) mContext.getResources().getDimension(
R.dimen.hj_margin_large);
}
holder.feedParent.setLayoutParams(lp1);
if (objects.get(position).isFeedFav()) {
holder.favorite.setSelected(true);
} else {
holder.favorite.setSelected(false);
}
holder.favorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (v.isSelected()) {
v.setSelected(false);
objects.get(position).setFeedFav(false);
} else {
v.setSelected(true);
objects.get(position).setFeedFav(true);
}
}
});
return convertView;
}
/**
* @param itemObjs
*/
public void addData(ArrayList<HJFeedItemObject> itemObjs) {
objects.addAll(itemObjs);
}
}
我收到以下错误
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
请帮忙
答案 0 :(得分:0)
检查
android:minSdkVersion="##"
android:targetSdkVersion="##"
清单上的
对于kitkat,SDKVersion是21
答案 1 :(得分:0)
而不是导入android.widget.LinearLayout.LayoutParams;将导入更改为=&gt; import android.view.ViewGroup.LayoutParams;
无论你在哪里设置布局参数都是这样的:
ViewGroup.LayoutParams lp1 = new ViewGroup.LayoutParams
(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
使用以下链接正确使用视图持有者模式 http://ricston.com/blog/optimising-listview-viewholder-pattern/ http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html