自定义适配器失败,没有错误

时间:2013-05-17 16:27:29

标签: android listview android-arrayadapter

问题是这样的: arraylist中有49个项目,适配器在第3项停止,没有任何错误通知。该应用程序不会崩溃,也不会显示任何文本视图。这是适配器:

public class ExhibitorObjectAdapter extends ArrayAdapter<ExhibitorObject> {

public ArrayList<ExhibitorObject> exhibitors;
public Activity act;
public TextView tvExhibitorName;
public ImageView ivExhibitorLogo;

public ExhibitorObjectAdapter(Activity a, int layoutResourceId, ArrayList<ExhibitorObject> ex) {
    super(a, layoutResourceId,ex);
    this.exhibitors = ex;
    this.act = a;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return exhibitors.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.single_exhibitor, null);        
    }

    ExhibitorObject ex = exhibitors.get(position);

    if (ex != null) {

         //System.out.println(ex.companyName);
        tvExhibitorName = (TextView) v.findViewById(R.id.textViewListExhibitorName);

        tvExhibitorName.setText( ex.companyName );

    }

    return v;
}

}

编辑:这是包含listview的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp" 
    android:background="#134882"
    android:paddingBottom="15sp"
    android:paddingTop="5sp" 
    android:paddingLeft="5sp" 
    android:textColor="#FFFFFF" 
    android:text="Exhibitors" />


<ListView android:id="@+id/listViewExhibitors"
          android:layout_height="match_parent"
          android:layout_width="match_parent"/>


</LinearLayout>

我还有其他几个与此类似的适配器,它们工作得很好。我浪费了整整一天(​​这可能是一个愚蠢的问题),任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

将tvExhibitorName保存在适配器范围内是不好的。在getView()中使其成为本地,或者让您自己承受各种泄漏。那很好可能是你问题的根源。

另外,您不会在父母下面夸大新视图,这很奇怪。可能正在使用inflate(布局,,false)会有所帮助。