Android: getView method of custom adapter not called

筅森魡賤 提交于 2019-12-11 14:53:18

问题


I've searched through a lot of other answer for the same problem, but didn't found any solution that works for me. The problem, as the title says, is that the getView method from my custom adapter doesn't get called.

Here's the code (first the fragment):

public class CategoryListFragment extends ListFragment
                            implements NewCategoryDialogListener {

private GestoreAttivitaDbHelper mDbHelper;
private CategoryListAdapter mAdapter;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setHasOptionsMenu(true);        

    mAdapter = new CategoryListAdapter(getActivity());
    setListAdapter(mAdapter);

    CategoryLoader categoryLoader = new CategoryLoader();

    if (mDbHelper == null) {
        mDbHelper = new GestoreAttivitaDbHelper(getActivity().getApplicationContext());
    }

    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    mAdapter.addAll(categoryLoader.getAllCategories(db));
    mAdapter.notifyDataSetChanged();
    mAdapter.getCount();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.category_list, container);
}

Here's the adapter:

public class CategoryListAdapter extends ArrayAdapter<CategoryElement> {

private LayoutInflater mInflater;

public CategoryListAdapter(Context ctx) {
    super(ctx, R.layout.category_element);

    mInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

    View view;

    Log.d("Adapter", "Restituisco la view per l'elemento");

    if (convertView == null) {
        view = mInflater.inflate(R.layout.category_element, null);
    } else {
        view = convertView;
    }

    //((TextView) view.findViewById(R.id.category_element_text)).setText(getItem(position).getName());
    TextView textView = (TextView) view.findViewById(R.id.category_element_text);
    textView.setText(getItem(position).getName());

    return view;
}

}

And here's my two layout files:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/category_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >  

</ListView>

and:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/category_element"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

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

</LinearLayout>

I thought that setting the adapter in the onCreate could be a problem, since it is called before onCreateView, and at that time the fragment isn't already associated with the ListView. So I moved the code from the onCreate to the onStart method, but nothing changed.

Also, the getCount() correctly returns me 6, the precise number of element red from the database.

Any help would be really appreciated!! Thanks.

Edit:
Solved!
Problem was in the activity, I had the following code:

fragmentTransaction.add(0, categoryListFragment);

that I changed in

fragmentTransaction.add(R.id.activity_main, categoryListFragment);

Without specifying the View id to which the fragment should be attached it never draws it!
In addition, I had to change from this

view = mInflater.inflate(R.layout.category_element, parent);

to this:

view = mInflater.inflate(R.layout.category_element, null);

in the getView method.
PS. I'm editing this cause I can't answer my own question until 8 hours have passed..


回答1:


I think in your R.layout.category_list file you need to give the ListView the following attribute:

android:id="@android:id/list"

ListFragment (and ListActivity) look for this id to find the ListView when you call methods like setListAdapter().

Also, if all you want is a ListView, you don't have to supply a layout file. Simply do not override onCreateView() and the system will provide a ListView for you automatically. Only if you want a custom layout do you need to inflate one, and if you do, the ListView should have the id stated above.



来源:https://stackoverflow.com/questions/14032718/android-getview-method-of-custom-adapter-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!