android:在另一个类中使用一个视图处理程序

时间:2016-09-08 10:42:56

标签: android

我想将一个HeaderView添加到ListView,但HeaderView是在另一个类中处理的

view.xml用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
 >
<krelve.view.Kanner
        android:id="@+id/kanner"
        android:layout_width="match_parent"
        android:layout_height="250dp" />


</RelativeLayout>

处理类

public class Pic extends Activity{

    int[] imagesRes = { R.drawable.a, R.drawable.b, R.drawable.c,
            R.drawable.d, R.drawable.e };
     public View getPic(){
        Kanner kanner =  (Kanner) LayoutInflater.from(this).inflate(
                R.layout.pic, null).findViewById(R.id.kanner);
        kanner.setImagesRes(imagesRes);
        return kanner;


    }
}

ListView所在的类:

private void listAdd() {

    HashMap<String, Object> map;
    ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();
    for (int i = 0; i < 10; i++) {
        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.ic_launcher);
        map.put("ItemTitle", "This is Title.....");
        map.put("ItemText", "This is text.....");
        mylist.add(map);
    }
    SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist,
            R.layout.list_item,


            new String[] { "ItemImage", "ItemTitle", "ItemText" },


            new int[] { R.id.item_Image, R.id.item_Title, R.id.item_Text });

    //listView1.addHeaderView(mSchedule);
    listView.setAdapter(mSchedule);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup lf = (ViewGroup) inflater.inflate(R.layout.news_list, null);
    listView = (ListView) (lf.findViewById(R.id.list_main));
    listView.addHeaderView(new Pic().getPic());     
    listAdd();
    lf.removeAllViews();
    return listView;

}

ps:Kanner是一个自定义组件 我认为错误是 listView.addHeaderView(new Pic().getPic());

但是我无法解决这个错误。

0 个答案:

没有答案