片段不匹配数据绑定错误

时间:2013-04-15 16:27:36

标签: android

我在屏幕上有2个片段都扩展ListView ..我通过fragment1中的代码填充列表但数据绑定到fragment2 ..两个片段都在xml文件中声明。提前致谢

以下是片段1 的代码:

public class Fragment1 extends ListFragment implements OnFragmentUiUpdate {
    OnFragmentSelectedListener mCallback;
    private final int fragId = 1;
    // The container Activity must implement this interface so the frag can
    // deliver messages
    ListView list;


    /*@Override public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      se generateFragmentView(0,
      getActivity()); // We need to use a different list item layout for
      devices older than Honeycomb int layout =
      android.R.layout.simple_list_item_1;Build.VERSION.SDK_INT >=
      Build.VERSION_CODES.HONEYCOMB ?
      android.R.layout.simple_list_item_activated_1 :
      android.R.layout.simple_list_item_1;

      // Create an array adapter for the list view, using the Ipsum headlines
      array 
      int layout =
          android.R.layout.simple_list_item_1;Build.VERSION.SDK_INT >=
          Build.VERSION_CODES.HONEYCOMB ?
          android.R.layout.simple_list_item_activated_1 :
          android.R.layout.simple_list_item_1;
      setListAdapter(new ArrayAdapter<String>(getActivity(), layout,
      Ipsum.Headlines)); }*/

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.

        // Inflate the layout for this fragment
        View vi = inflater.inflate(R.layout.menu_1_list, container, false);

        return vi;
    }

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

        /*
         * // When in two-pane layout, set the listview to highlight the
         * selected list item // (We do this during onStart because at the point
         * the listview is available.) if
         * (getFragmentManager().findFragmentById(R.id.article_fragment) !=
         * null) { getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); }
         */
        generateFragmentView(0, getActivity());
    }

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

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnFragmentSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position,
            long id) {
        // Notify the parent activity of selected item
        mCallback.onFragmentSelected(listView, view, position, id, fragId);

        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }

    @Override
    public void generateFragmentView(int position, Activity mainScreen) {
        // TODO Auto-generated method stub

        DbAdapter dbAdapter = null;
        dbAdapter = new DbAdapter(mainScreen.getApplicationContext());
        List<BindObject> menu1 = null;
        dbAdapter.open();
        try {
            menu1 = (List<BindObject>) dbAdapter.selectAll(Menu1.class);
            System.out.println("menu");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dbAdapter.close();
        list = (ListView) mainScreen.findViewById(android.R.id.list);

        LazyAdapter adapter = new LazyAdapter(mainScreen, this,
                (List<BindObject>) menu1);
        list.setAdapter(adapter);
    }

    @Override
    public View updateFragmentView(int position, Activity activity,
            LayoutInflater inflater, List<BindObject> Data, View convertView,
            ViewGroup parent) {

        View vi = convertView;
        ViewHolder holder = null;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.menu1_item, null);

            holder = new ViewHolder();

            holder.item = (TextView) vi.findViewById(R.id.item);

            vi.setTag(holder);
        } else
            holder = (ViewHolder) vi.getTag();

        Menu1 menu = (Menu1) Data.get(position);

        holder.item.setText(menu.getItem());

        return vi;

    }

    public static class ViewHolder {

        public TextView item;

    }

}

以下是片段2 的代码:

public class Fragment1_1 extends ListFragment implements OnFragmentUiUpdate{
    OnFragmentSelectedListener mCallback;
    ListView   list;
    private final int fragId=2;
    // The container Activity must implement this interface so the frag can deliver messages


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

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = android.R.layout.simple_list_item_1;Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

        // Create an array adapter for the list view, using the Ipsum headlines array
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));


    }*/

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.menulist, container, false);
    }

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

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

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

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnFragmentSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        // Notify the parent activity of selected item
        mCallback.onFragmentSelected(l, v, position, id,fragId);

        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }

    public void updateArticleView(int position) {
        TextView article = (TextView) getActivity().findViewById(R.id.article);
        article.setText(Ipsum.Articles[position]);
        //mCurrentPosition = position;
    }



    @Override
    public void generateFragmentView(int position, Activity mainScreen) {
        // TODO Auto-generated method stub
        //updateArticleView( position);

        list = (ListView) mainScreen.findViewById(android.R.id.list);


        LazyAdapter adapter = new LazyAdapter(mainScreen,this, (List<BindObject>) Util.generateData());
        list.setAdapter(adapter);
    }

    @Override
    public View updateFragmentView(int position, Activity activity,LayoutInflater inflater,List<BindObject> Data, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder = null;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.menu1_1_item, null);

            holder = new ViewHolder();

            holder.item = (TextView) vi.findViewById(R.id.item);
            holder.image = (ImageView) vi.findViewById(R.id.image);

            vi.setTag(holder);
        } else
            holder = (ViewHolder) vi.getTag();



        Menu1_1 menu = (Menu1_1) Data.get(position);

        holder.item.setText(menu.getItem());
        String imagString=activity.getPackageName()+":drawable/"+"ic_launcher";
        int res = activity.getResources().getIdentifier(imagString, null, null);
        holder.image.setImageResource(res);
        return vi;



    }
    public static class ViewHolder {

        public TextView item;
        public ImageView image;

    }

}

main.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="horizontal" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true" >
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TableRow>
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <fragment
                android:id="@+id/Fragment1_1"
                android:name="com.example.android.fragments.Fragment1_1"
                android:layout_width="60dp"
                android:layout_height="100dp"
                android:layout_weight="0.25" />
            <fragment
                android:id="@+id/Fragment1_1_1"
                android:name="com.example.android.fragments.Fragment1_1_1"
                android:layout_width="90dp"
                android:layout_height="100dp"
                android:layout_weight="0.75" />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TableRow>
    </TableLayout>

    <SlidingDrawer
        android:id="@+id/SlidingDrawer"
        android:layout_width="wrap_content"
        android:layout_height="150dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:content="@+id/contentLayout"
        android:handle="@+id/slideButton"
        android:orientation="vertical"
        android:padding="10dip" >
        <Button
            android:id="@+id/slideButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/ic_launcher" >
        </Button>

        <TableLayout
            android:id="@+id/contentLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:gravity="center_vertical" >
                <ImageButton
                    android:id="@+id/feature"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_weight="0.2"
                    android:background="@drawable/ic_launcher"
                    android:onClick="displayFeature" />
                <ImageButton
                    android:id="@+id/feature"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_weight="0.2"
                    android:background="@drawable/ic_launcher"
                    android:onClick="displayFeature" />
                <ImageButton
                    android:id="@+id/feature"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_weight="0.2"
                    android:background="@drawable/ic_launcher"
                    android:onClick="displayFeature" />
                <ImageButton
                    android:id="@+id/feature"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_weight="0.2"
                    android:background="@drawable/ic_launcher"
                    android:onClick="displayFeature" />
                <ImageButton
                    android:id="@+id/feature"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_weight="0.2"
                    android:background="@drawable/ic_launcher"
                    android:onClick="displayFeature" />
                <ImageButton
                    android:id="@+id/feature"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_weight="0.2"
                    android:background="@drawable/ic_launcher"
                    android:onClick="displayFeature" />

                <TableLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >
                        <ImageButton
                            android:id="@+id/googlebtn"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_weight="0.2"
                            android:background="@drawable/ic_launcher"
                            android:onClick="displayFeature" />
                        <ImageButton
                            android:id="@+id/tweeterBtn"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_weight="0.2"
                            android:background="@drawable/ic_launcher"
                            android:onClick="displayFeature" />
                    </TableRow>
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >
                        <ImageButton
                            android:id="@+id/fb"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_weight="0.2"
                            android:background="@drawable/ic_launcher"
                            android:onClick="displayFeature" />
                        <ImageButton
                            android:id="@+id/fb2"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_weight="0.2"
                            android:background="@drawable/ic_launcher"
                            android:onClick="displayFeature" />
                    </TableRow>
                </TableLayout>
            </TableRow>
        </TableLayout>
    </SlidingDrawer>

    <fragment
        android:id="@+id/Fragment1"
        android:name="com.example.android.fragments.Fragment1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tableLayout1"
        android:layout_marginTop="106dp"
        android:tag="feature" />

    <ImageButton
        android:id="@+id/menu1Btn"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/Fragment1"
        android:background="@drawable/ic_launcher"
        android:onClick="displayFeature" />

</RelativeLayout>

Fragment1数据与fragent2绑定...同时调试..fragment1代码正常运行但generatefragnentView()函数数据转到fragment2,fragment1显示空白

0 个答案:

没有答案