我只是使用Baseadapter在listview上创建,但我突然得到错误 “java.lang.ClassCastException:java.lang.Integer无法强制转换为com.play.findnearplace.Md $ Holder”问题出在getView()中,否则条件持有者得到整数vlue就像0。
public class Md extends BaseAdapter implements Filterable {
public List<FD> itemsData;
public List<FD> orig;
OnClickListener onclick;
Context context;
int id;
public Md(List<FD> itemsData, OnClickListener onclick, int id,MainActivity main) {
this.itemsData = itemsData;
this.onclick = onclick;
this.id = id;
this.context=main;
}
@Override
public int getCount() {
return itemsData.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class Holder
{
public ImageView ui;
public TextView tt;
public RelativeLayout rl_main_child;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder;
Log.i("Second", "1");
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
{
Log.i("Second", "2");
convertView=mInflater.inflate(R.layout.main_child, parent,false);
holder = new Holder();
Log.i("Second", "3");
holder.ui = (ImageView) convertView.findViewById(R.id.imgunit);
holder.tt = (TextView) convertView.findViewById(R.id.txttitle);
convertView.setTag(holder);
Log.i("Second", ""+holder);
}else
{
Log.i("Second", ""+convertView.getTag());
holder=(Holder) convertView.getTag();
}
Log.i("Second", "5");
FD m=itemsData.get(position);
holder.rl_main_child=(RelativeLayout) convertView.findViewById(R.id.rl_main_child);
holder.rl_main_child.setTag(position);
holder.tt.setText(m.gettitle());
holder.rl_main_child.setOnClickListener(onclick);
return convertView;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final List<FD> results = new ArrayList<FD>();
if (orig == null)
orig = itemsData;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final FD g : orig) {
if (g.gettitle().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
itemsData = (ArrayList<FD>) results.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
错误.....
05-18 23:43:20.434: E/AndroidRuntime(14417): FATAL EXCEPTION: main
05-18 23:43:20.434: E/AndroidRuntime(14417): Process: com.play.findnearplace, PID: 14417
05-18 23:43:20.434: E/AndroidRuntime(14417): java.lang.ClassCastException: java.lang.Integer cannot be cast to com.play.findnearplace.Md$Holder
05-18 23:43:20.434: E/AndroidRuntime(14417): at com.play.findnearplace.Md.getView(Md.java:80)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.widget.AbsListView.obtainView(AbsListView.java:2303)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.widget.ListView.measureHeightOfChildren(ListView.java:1273)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.widget.ListView.onMeasure(ListView.java:1182)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.view.View.measure(View.java:16834)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1621)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.widget.LinearLayout.measureVertical(LinearLayout.java:742)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.widget.LinearLayout.onMeasure(LinearLayout.java:607)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.view.View.measure(View.java:16834)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.view.View.measure(View.java:16834)
05-18 23:43:20.434: E/AndroidRuntime(14417): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
05-18 23:43:20.434: E/AndroidRuntime(14417):
我的日志.....
05-19 00:28:14.646: I/Second(17516): 1
05-19 00:28:14.646: I/Second(17516): 2
05-19 00:28:14.655: I/Second(17516): 3
05-19 00:28:14.655: I/Second(17516): com.play.findnearplace.Md$Holder@42544570
05-19 00:28:14.655: I/Second(17516): 5
05-19 00:28:14.660: I/Second(17516): 1
05-19 00:28:14.660: I/Second(17516): 0
main_child
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_main_child"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginLeft="35dp"
android:background="@color/white" >
<com.play.findnearplace.T
android:id="@+id/txttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="35dp"
android:layout_marginTop="3dp"
android:textColor="@color/secondary"
android:textSize="15sp"
android:visibility="visible" />
</LinearLayout>
<ImageView
android:id="@+id/imgunit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/rounded_corner"
android:src="@drawable/ic_launcher" />
</FrameLayout>
</RelativeLayout>
答案 0 :(得分:1)
问题在于这两行:
holder.rl_main_child=(RelativeLayout) convertView.findViewById(R.id.rl_main_child);
holder.rl_main_child.setTag(position);
在第一个中,您将convertView
分配给rl_main_child
,第二个是您覆盖标记。因此,第二次调用getView
时,您会尝试将标记转换为Holder
,但由于您使用整数position
覆盖它,因此会得到ClassCastException
。摆脱holder.rl_main_child.setTag(position);
,它将起作用
答案 1 :(得分:0)
行FD m=itemsData.get(position);
导致问题。
您正在尝试将int转换为FD类型
答案 2 :(得分:0)
试试这个:
if(convertView==null) {
// your existing code
holder.rl_main_child=(RelativeLayout) convertView.findViewById(R.id.rl_main_child);
convertView.setTag(holder);
}
else {
// your existing code
}
// no need to do below statement, hence commented
//holder.rl_main_child.setTag(position);
holder.tt.setText(m.gettitle());
holder.rl_main_child.setOnClickListener(onclick);