繁体   English   中英

Android:尝试在片段之间传递数据的问题

[英]Android: Issue trying to pass data between fragments

我试图将一些数据从ListFragment传递到另一个Fragment。 列表显示正常,但是当我单击列表中的项目时,没有任何反应,并且日志中也没有任何显示错误的内容。 我是android / java开发的新手,正在尝试围绕不同的概念(活动,片段,捆绑包等)进行总结。 来自iOS /目标C世界,我看到了在“视图”之间传递数据所涉及的概念的相似之处,但我想我还没有完全理解android的概念。

无论如何,下面是片段和布局。 有人可以帮我看看我要去哪里错吗? 非常感激。

主(fragment_ehschedule.xml)和列表视图(schedule_info.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" 
 android:orientation="vertical">

 <EditText android:id="@+id/myFilter" 
  android:layout_width="match_parent"
  android:layout_height="wrap_content" 
  android:ems="10" 
  android:hint="@string/some_hint">
  <requestFocus />
 </EditText>

 <ListView android:id="@android:id/list" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/frameLayout"
    android:padding="6dip" >

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textViewSessionTime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:text="sessiontime" />

        <TextView
            android:id="@+id/textViewScheduleDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="scheduledate"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textViewSessionName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="sessionname"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

</FrameLayout>

片段详细信息(fragment_ehschedule_detail.xml)的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="10dp">

  <TextView android:id="@+id/sessionname_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="25dip"
            android:textStyle="bold"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:textColor="#43bd00"/>

  <TextView android:id="@+id/scheduledate_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#acacac"/>

  <TextView android:id="@+id/sessiontime_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"/>

</LinearLayout>

主视图的类文件(EhallSchedFragment2.java)如下:

 public class EhallSchedFragment2 extends ListFragment{

     public static String strDate = null;

     private SQLiteDB sqlite_obj;
     private SimpleCursorAdapter dataAdapter;

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {
             super.onCreateView(inflater, container, savedInstanceState);
             final View v = inflater.inflate(R.layout.fragment_ehschedule, container, false);

    sqlite_obj = new SQLiteDB(getActivity());

    sqlite_obj.open();

    Cursor cursor = sqlite_obj.fetchAllSchedules();

     // The desired columns to be bound
     String[] columns = new String[] {
       SQLiteDB.KEY_SCHEDULEDATE,
       SQLiteDB.KEY_SESSIONNAME,
       SQLiteDB.KEY_SESSIONTIME,
       SQLiteDB.KEY_DESC
     };

     // the XML defined views which the data will be bound to
     int[] to = new int[] { 
       R.id.textViewScheduleDate,
       R.id.textViewSessionName,
       R.id.textViewSessionTime,
       R.id.textViewDesc,
     };

     // create the adapter using the cursor pointing to the desired data 
     //as well as the layout information
     dataAdapter = new SimpleCursorAdapter(
             getActivity(), R.layout.schedule_info, 
               cursor, 
               columns, 
               to,
               0);

     ListView listView = (ListView)v. findViewById(android.R.id.list);
     // Assign adapter to ListView
     listView.setAdapter(dataAdapter);

     listView.setOnItemClickListener(new OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> listView, View view, 
         int position, long id) {
       // Get the cursor, positioned to the corresponding row in the result set
       Cursor cursor = (Cursor) listView.getItemAtPosition(position);

       String exHallScheduleDate = 
                cursor.getString(cursor.getColumnIndexOrThrow("scheduleDate"));
       String exHallSessionName = 
                        cursor.getString(cursor.getColumnIndexOrThrow("sessionName"));
       String exHallSessionTime = 
                        cursor.getString(cursor.getColumnIndexOrThrow("sessionTime"));

       EhallSchedDetailFragment2 myDetailFragment = new EhallSchedDetailFragment2();

       Bundle bundle = new Bundle();

       bundle.putString("scheduleDate", exHallScheduleDate);
       bundle.putString("sessionName", exHallSessionName);
       bundle.putString("sessionTime", exHallSessionTime);

           myDetailFragment.setArguments(bundle);

           FragmentManager fragmentManager = getFragmentManager();

            myDetailFragment.setArguments(bundle);
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.frameLayout, myDetailFragment);
            transaction.remove(EhallSchedFragment2.this);

           /*
                * Add this transaction to the back stack. 
                * This means that the transaction will be remembered after it is 
                * committed, and will reverse its operation when later popped off 
                * the stack.
           */
               transaction.addToBackStack(null);

               transaction.commit();

       }
      });


     EditText myFilter = (EditText)v. findViewById(R.id.myFilter);
      myFilter.addTextChangedListener(new TextWatcher() {

       public void afterTextChanged(Editable s) {
       }

       public void beforeTextChanged(CharSequence s, int start, 
         int count, int after) {
       }

       public void onTextChanged(CharSequence s, int start, 
         int before, int count) {
        dataAdapter.getFilter().filter(s.toString());
       }
      });

      dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
             public Cursor runQuery(CharSequence constraint) {
                 return sqlite_obj.fetchScheduleByDate(constraint.toString());
             }
         });

    return v;
}

}

详细信息视图的类文件(EhallSchedDetailFragment2.java)如下:

 public class EhallSchedDetailFragment2 extends Fragment {

TextView scheduleDateDetail;
TextView sessionNameDetail;
TextView sessionTimeDetail;

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.fragment_ehschedule_detail, null);

   scheduleDateDetail = (TextView)view.findViewById(R.id.scheduledate_label);
   sessionNameDetail = (TextView)view.findViewById(R.id.sessionname_label);
   sessionTimeDetail = (TextView)view.findViewById(R.id.sessiontime_label);

       Bundle bundle = getArguments();
          if(bundle != null){
           String schedDateDet = bundle.getString("scheduleDate");
           scheduleDateDetail.setText(schedDateDet);

           String sessNameDet = bundle.getString("sessionName");
           sessionNameDetail.setText(sessNameDet);

           String sessTimeDet = bundle.getString("sessionTime");
           sessionTimeDetail.setText(sessTimeDet);
          }
          return view;
}

}

您基本上需要四件事来获得所需的东西:

  1. 您需要片段-称为FragmentA
  2. 然后,您可以在该片段内或其自己的类文件中定义一个接口。 该接口将具有您必须调用的方法来通知活动。
  3. 然后在您的活动中,实现接口并覆盖上面第2步中的方法。
  4. 要通知FragmentB,您真正需要做的就是在步骤3中覆盖的方法内部调用其方法。

如前所述,您不应直接在片段之间进行通信。 这是一个很好的循序渐进教程,介绍如何执行上述所有步骤:

在片段与活动之间进行沟通

我希望这可以帮助您解决或逐步解决您的问题。

如果您使用的是ListFragment,则可以执行以下操作:

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mListView = getListView();
    SetClicklListener(mListView);  
  }
 public void SetClicklListener(ListView listView){
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
            //Here you can catch the clicked item
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM