SherlockFragment和BaseAdapter:从不调用getView和getItem

时间:2014-09-02 17:18:54

标签: android actionbarsherlock baseadapter getview

我已经看到了一堆问题,但我没有找到解决方案!我做错了什么?为什么getView永远不会被调用?!

这是我的适配器:

 public class ConteudoAdapter<T extends IProgramacao> extends BaseAdapter {
    private Context context;
    private List<T> list;
    private int resourceId;


    public ConteudoAdapter(Context context, int resourceId, List<T> list){
        this.context = context;
        this.list = list;
        this.resourceId = resourceId;
    }

    @Override
    public int getCount() {
        Log.v("test", "Returning count " + (this.list.size() != 0 ? this.list.size() : 0));
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

           View row = view;
           ViewHolder holder = null;
           T entidade = list.get(position);

            if(row == null)
            {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(resourceId, parent, false);

                holder = new ViewHolder();          

                holder.tvTitle = (TextView) row.findViewById(R.id.tvTitle);
                holder.tvTitle.setText(entidade.getTitulo());

                holder.tvConteudo = (TextView) row.findViewById(R.id.tvConteudo);
                holder.tvConteudo.setText(entidade.getChamada());

                row.setTag(holder);
            }

        //  holder.ivImg = (ImageView) layout.findViewById(R.id.nivImg);
        //  holder.ivImg.setImageResource(entidade.);


        // IMAGE VIEW
        //      holder.ivImg.setVisibility(View.VISIBLE);
        //  il.get(list.get(position).getUrlImage(), il.getImageListener(holder.ivImg, R.drawable.load, R.drawable.error));

        //tvTitle.setText(entidade.getTitulo());

        return view;
    }

    public static class ViewHolder{
        //public NetworkImageView nivImg;
        //public ImageView ivImg;
        public TextView tvTitle;
        public TextView tvConteudo;

    }
  }

这是我如何调用我的适配器(在asynctask内部,因为我从web获取值(JSON):

public class CategoriaEventoFragment extends SherlockFragment {

    private View rootView;
    private EventoBO evento;
    private ListView lv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

            rootView = (View) inflater.inflate(R.layout.categoria, container, false);

            CarregaDadosCategoriaEventoCaller carregaDadosCategoriaEventoCaller = new CarregaDadosCategoriaEventoCaller();
            carregaDadosCategoriaEventoCaller.execute((Void[]) null);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public class CarregaDadosCategoriaEventoCaller extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            evento = new EventoBO("http://urlhere");
            evento.popularListaImagemChamada("http://urlhere");

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub

            lv = (ListView) rootView.findViewById(R.id.listViewCategoria);

            lv.setAdapter(new ConteudoAdapter<Evento>(rootView.getContext(), R.layout.item, evento.getLista()));

        }
    }

    }


    }

XML文件:

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/home_bg_branco"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvConteudo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

categoria.xml

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

    <ListView
        android:id="@+id/listViewCategoria"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/home_bg_branco" >
    </ListView>

</LinearLayout>

我的LogCat为我的详细测试返回此信息:

09-02 17:13:27.322: V/test(24961): Returning count 5
09-02 17:13:27.322: V/test(24961): Returning count 5

提前致谢!!!

1 个答案:

答案 0 :(得分:2)

onCreateView()中,你应该返回rootView而不是super。