0

Android/Java プログラミングの初心者。タブアプリケーションの一部である Fragment 内に ListView と Button があります。私が抱えている問題は、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>
4

1 に答える 1

0

さて、onCreateView でビューをインフレートして返しますが、アダプターをその ListView に設定しません。代わりに、onActivityCreated でレイアウトを再度インフレートしますが、これは新しい別のビューであるため、アダプターをその階層のリストビューに設定しても、2 番目のビューは実際にはどこにもアタッチされていないため、何もしません。

onCreateView でアダプタを設定する必要があります。この時点でカーソルの準備ができていない場合は、null カーソルを使用してアダプターを作成し、後で (おそらく onActivityCreated で) CursorAdapter.swapCursor() を呼び出します。swapCursor() によって返された古いカーソルを常に閉じることを忘れないでください (null でない場合)。

于 2013-07-09T18:01:19.593 に答える