Fragment中的ListView没有调用newView

时间:2013-07-05 18:35:16

标签: android listview fragment loading android-cursoradapter

android / java编程新手。 我在Fragment中有一个ListView和一个Button,它是一个tab选项卡的一部分。 我遇到的问题是,即使我将CursorAdapter附加到listView,即使CursorAdapter中的getCount()返回0以外的东西,也不会调用Adapter中的newView()方法。

我可以看到按钮,我可以使用它。如果我改变列表的背景,我会看到背景颜色。

我正在使用支持库 - API级别11。

public class MyListFragment extends Fragment {
private static final String TAG = "FragmentTabs";

private String mTag ;
private MainLogCursorAdapter mainAdapter;
private Cursor cursor;
private MainLogSource mainLogSource;
private LayoutInflater mInflater;
private ListView listView;
private Button newLogButton;
public MyListFragment() {
}

public MyListFragment(String tag) {
    mTag = tag;
    Log.d(TAG, "Constructor: tag=" + tag);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
mInflater = LayoutInflater.from(getActivity());

    mainLogSource = new MainLogSource(getActivity().getApplicationContext());

    cursor = mainLogSource.getCursor();

    mainAdapter = new MainLogCursorAdapter(getActivity   ().getApplicationContext    (), cursor);

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

    listView = (ListView) view.findViewById(R.id.listView1);
    listView.setAdapter(mainAdapter);

    System.out.println("Adapter set in onActivityCreated");

    if (mTag.equals("Log")) {
        loadMainLog();

    } else {
        if (mTag.equals("Rem")) {
            loadReminderLog();

        } else
            loadConfig();

    }
    }
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment

    if (mTag==null) 

        mTag = new String("Log");

    if (mTag.equals("Log")) {
return inflater.inflate(R.layout.listview1, container, false);

    }

    return null;
}

适配器:

public class MainLogCursorAdapter extends CursorAdapter implements Filterable{
private LayoutInflater mLayoutInflater;
private Context mContext;
private MainLogSource dbh;
private String mainTypeDesc;

public MainLogCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
    mLayoutInflater = LayoutInflater.from(context);
    dbh = new MainLogSource(context);
    mContext = context;
    mCursor = c;

    public void bindView(View view, Context context, Cursor cursor) {
    String key = (String) cursor.getString(cursor
            .getColumnIndex(MainLogHelper.KEY));

some other code here ...

}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v = mLayoutInflater.inflate(R.layout.row, parent, false);
    System.out.println("newView");
    return v;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    System.out.println(super.getCount() + " count returned");
    return super.getCount();
}
}

和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="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="0.9" 
    android:transcriptMode="alwaysScroll"
    >

</ListView>

<Button 
    android:id="@+id/NewLogButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    android:onClick="createLog"
    android:text="@string/NewLogCreation" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

好的,所以你在onCreateView中充气并返回视图,但是你没有将适配器设置为该ListView。相反,你在onActivityCreated中再次膨胀布局,但这是一个新的,不同的视图,因此将适配器设置为该层次结构中的列表视图不会做任何事情,因为第二个视图从未实际附加到任何地方。 / p>

您需要在onCreateView中设置适配器。如果此时光标无法就绪,只需使用空游标创建适配器,然后稍后调用CursorAdapter.swapCursor()(可能在onActivityCreated中)。请记住始终关闭swapCursor()返回的旧游标(如果它不为null)。