安卓:自定义适配器的getView方法不叫 [英] Android: getView method of custom adapter not called

查看:136
本文介绍了安卓:自定义适配器的getView方法不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了很多其他的答案同样的问题的搜查,但没有发现适合我的任何解决方案。这个问题,正如标题所说,是从我的自定义适配器getView方法不会被调用。

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.

这里的code(第一个片段):

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);
}

这里的适配器:

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>

<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>

我认为在OnCreate设置适配器可能是一个问题,因为它是onCreateView之前调用,并且在该时间片段尚未与ListView的关联。让我感动从的onCreate的code到OnStart方法,但没有改变。

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.

此外,利用getCount()正确返回我6,从数据库元素红色的precise数量。

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

任何帮助将是非常美联社preciated!
谢谢你。

Any help would be really appreciated!! Thanks.

修改结果
解决了!结果
问题是,在活动中,我有以下code:


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

fragmentTransaction.add(0, categoryListFragment);

这是我改变了

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

如果不指定视图id该片段应重视它从未吸引了!结果
此外,我不得不从这个改变。

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);

这样:

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

在getView方法。结果
PS。我编辑这个原因到晚上8小时过去了,我不能回答我的问题。

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

推荐答案

我觉得在你的 R.layout.category_list 文件,你需要给ListView控件以下属性:

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

机器人:ID =@机器人:ID /列表

ListFragment(和ListActivity)寻找这个id来找到的ListView 当你调用像 setListAdapter方法​​()

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

另外,如果你想要的是一个ListView,您不必提供一个布局文件。根本就没有覆盖onCreateView(),系统会自动提供一个的ListView 为您服务。只有当你想自定义布局你需要充气的,如果你这样做,在ListView应的ID上述。

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.

这篇关于安卓:自定义适配器的getView方法不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆