【问题标题】:Android: Issue trying to pass data between fragmentsAndroid:尝试在片段之间传递数据的问题
【发布时间】:2014-10-21 03:12:10
【问题描述】:

我试图将一些数据从 ListFragment 传递到另一个 Fragment。该列表显示正常,但是当我单击列表中的某个项目时,没有任何反应,并且日志中没有任何内容显示错误。我是 android/java 开发的新手,并试图围绕不同的概念(活动、片段、捆绑包等)展开我的工作。来自 iOS/objective 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;
}

}

【问题讨论】:

标签: java android xml android-fragments


【解决方案1】:

如果你使用 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
        }
    });
}

【讨论】:

    【解决方案2】:

    你基本上需要四样东西来得到你需要的东西:

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

    如前所述,您不应该直接在片段之间进行通信。这是一个关于如何执行上述所有步骤的分步教程:

    Communicating between Fragments and Activities

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-21
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      相关资源
      最近更新 更多