1

私の既存の RSS リーダー アプリを、SDK で提供されるマスター ディテール サンプルに変換したいと考えています。カスタム ListAdapter を使用して ListFragment に ListView を入力していますが、アプリを実行してもリストが表示されません。なぜあなたがアイデアを持っているのか、私にはわかりません。エラーは表示されません。

私のItemListActivity:

public class ItemListActivity extends FragmentActivity
        implements ItemListFragment.Callbacks {

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

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

        Log.d("debug","Hallo in der ItemListActivity");

        setContentView(R.layout.activity_item_list);

        //wichtig!
        if (findViewById(R.id.item_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            ((ItemListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.item_list))
                    .setActivateOnItemClick(true);
        }

        // TODO: If exposing deep links into your app, handle intents here.
    }

    //ActionBar
    public void onCreateOptionsMenu(
          Menu menu, MenuInflater inflater) {
       inflater.inflate(R.menu.main, menu);
    }

    /**
     * Callback method from {@link ItemListFragment.Callbacks}
     * indicating that the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(String id) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
            ItemDetailFragment fragment = new ItemDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.item_detail_container, fragment)
                    .commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, ItemDetailFragment.class);
            detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }


}

私のItemListFragment:

public class ItemListFragment extends ListFragment {

    Application myApp;
    RSSFeed feed;
    ListView lv;
    CustomListAdapter adapter;

    /**
     * The serialization (saved instance state) Bundle key representing the
     * activated item position. Only used on tablets.
     */
    private static final String STATE_ACTIVATED_POSITION = "activated_position";

    /**
     * The fragment's current callback object, which is notified of list item
     * clicks.
     */
    private Callbacks mCallbacks = sDummyCallbacks;

    /**
     * The current activated item position. Only used on tablets.
     */
    private int mActivatedPosition = ListView.INVALID_POSITION;

    /**
     * A callback interface that all activities containing this fragment must
     * implement. This mechanism allows activities to be notified of item
     * selections.
     */
    public interface Callbacks {
        /**
         * Callback for when an item has been selected.
         */
        public void onItemSelected(String id);
    }

    /**
     * A dummy implementation of the {@link Callbacks} interface that does
     * nothing. Used only when this fragment is not attached to an activity.
     */
    private static Callbacks sDummyCallbacks = new Callbacks() {
        @Override
        public void onItemSelected(String id) {
        }
    };

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemListFragment() {
    }

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

        Log.d("debug","Hallo in ItemListFragment");

        //show ActionBar
        setHasOptionsMenu(true);

        myApp = getActivity().getApplication();

        // Get feed form the file
        feed = (RSSFeed) getActivity().getIntent().getExtras().get("feed");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            //This layout contains your list view 
            View view = inflater.inflate(R.layout.feed_list, container, false);

            // Initialize the variables:
            lv = (ListView) view.findViewById(android.R.id.list);
            lv.setVerticalFadingEdgeEnabled(true);

            Log.d("debug","Bin hier: on Create View, listview aus Layout, ItemListFragment");

         return view;
    }

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

        // Set an Adapter to the ListView        
        adapter = new CustomListAdapter(getActivity(), feed);
        lv.setAdapter(adapter);

        Log.d("debug","Bin hier: onActivityCreated, add CustomListAdapter, ItemListFragment");
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Restore the previously serialized activated item position.
        if (savedInstanceState != null
                && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // Activities containing this fragment must implement its callbacks.
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mCallbacks = (Callbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();

        // Reset the active callbacks interface to the dummy implementation.
        mCallbacks = sDummyCallbacks;
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);

        // Notify the active callbacks interface (the activity, if the
        // fragment is attached to one) that an item has been selected.
        mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mActivatedPosition != ListView.INVALID_POSITION) {
            // Serialize and persist the activated item position.
            outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
        }
    }

    /**
     * Turns on activate-on-click mode. When this mode is on, list items will be
     * given the 'activated' state when touched.
     */
    public void setActivateOnItemClick(boolean activateOnItemClick) {
        // When setting CHOICE_MODE_SINGLE, ListView will automatically
        // give items the 'activated' state when touched.
        getListView().setChoiceMode(activateOnItemClick
                ? ListView.CHOICE_MODE_SINGLE
                : ListView.CHOICE_MODE_NONE);
    }

    private void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(mActivatedPosition, false);
        } else {
            getListView().setItemChecked(position, true);
        }

        mActivatedPosition = position;
    }

私の activity_item_list.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="com.example.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:context=".ItemListActivity"
    tools:layout="@android:layout/list_content" />

マイ feed_list.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
android:id="@android:id/list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>

</LinearLayout>

私の CustomListAdapter クラス:

public class CustomListAdapter extends BaseAdapter  {

        private LayoutInflater layoutInflater;
        public ImageLoader imageLoader;
        public RSSFeed _feed;

        public CustomListAdapter(Activity activity, RSSFeed feed) {

            _feed = feed;

            layoutInflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader = new ImageLoader(activity.getApplicationContext());
        }

        @Override
        public int getCount() {

            // Set the total list item count
            return _feed.getItemCount();
        }

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

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

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

            // Inflate the item layout and set the views
            View listItem = convertView;
            int pos = position;
            if (listItem == null) {
                listItem = layoutInflater.inflate(R.layout.list_item, null);
            }

            // Initialize the views in the layout
            ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
            TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
            TextView tvDate = (TextView) listItem.findViewById(R.id.date);

            // Set the views in the layout
            imageLoader.DisplayImage(_feed.getItem(pos).getImage(), iv);
            tvTitle.setText(_feed.getItem(pos).getTitle());
            tvDate.setText(_feed.getItem(pos).getDate());

            return listItem;
        }
    }

重要なファイルを忘れていないことを願っています。教えて頂ければ追加します。よろしくお願いします1

4

3 に答える 3

2

カスタム アダプタの getCount() メソッドを確認してください。

于 2013-11-09T18:17:20.633 に答える
2

getCount メソッドが原因でリストが表示されなかったとは思いません。ドキュメントを詳しく見ると、以下のように書かれています。

リストをアダプターに関連付けるには、ListFragment.setListAdapter()を使用する必要があります。ListView.setAdapter()を直接呼び出さないでください。重要な初期化がスキップされます。

ここを参照してください: http://developer.android.com/reference/android/app/ListFragment.html

したがって、 onActivityCreated 内で、リストを次のように設定します。

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

    // Set an Adapter to the ListView        
    adapter = new CustomListAdapter(getActivity(), feed);
   // lv.setAdapter(adapter);
    setListAdapter(adapter)
    Log.d("debug","Bin hier: onActivityCreated, add CustomListAdapter, ItemListFragment");
}
于 2014-10-13T20:23:27.737 に答える
0

アダプターをリストビュー コードに設定することは、onActivityCreated() メソッドでコメント化されます。以下のコード行のコメントを外してください。

// ListView にアダプタを設定します

adapter = new CustomListAdapter(getActivity(), feed);
lv.setAdapter(adapter);

onCreate()また、 の代わりにフラグメントのメソッドにコードを入れてみてくださいonActivityCreated()

于 2013-11-09T18:31:28.523 に答える