Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:试图在片段之间传递数据的问题_Java_Android_Xml_Android Fragments - Fatal编程技术网

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

Java Android:试图在片段之间传递数据的问题,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,我试图将一些数据从一个ListFragment传递到另一个片段。列表显示得很好,但当我单击列表中的某个项目时,什么也没有发生,日志中也没有显示任何表示错误的内容。我对android/java开发还不熟悉,并试图围绕不同的概念(活动、片段、捆绑包等)进行总结。来自iOS/objective C世界,我看到在“视图”之间传递数据的概念有相似之处,但我想我还没有完全理解android的概念 无论如何,片段和布局如下。有人能帮我看看哪里出了问题吗?非常感谢 主视图(fragment_ehschedule

我试图将一些数据从一个ListFragment传递到另一个片段。列表显示得很好,但当我单击列表中的某个项目时,什么也没有发生,日志中也没有显示任何表示错误的内容。我对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"?>
<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>
 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;
}
 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;
}

}

如果您使用的是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
        }
    });
}
@覆盖
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mListView=getListView();
设置ClickAllistener(mListView);
}
公共无效设置ClickListLener(ListView ListView){
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//在这里,您可以捕获单击的项目
}
});
}

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

  • 你需要你的碎片——称之为碎片
  • 然后在该片段内或在其自己的类文件中定义一个接口。此接口将有一个方法,您必须调用该方法来通知活动
  • 然后在您的活动中,实现接口并覆盖上面步骤2中的方法
  • 要通知FragmentB,您真正需要做的就是在步骤3中重写的方法中调用它的方法 如前所述,您不应该在片段之间直接通信。下面是一个很好的逐步教程,介绍如何执行上述所有步骤:


    我希望这有助于您解决问题或离解决问题越来越近。

    请发布您需要阅读的日志:
     @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
            }
        });
    }