0

我已經嘗試過與捆綁。我不知道它是如何工作的。請給我一些建議。它不會在列表視圖中顯示任何內容。 我也試過了,沒有使用FragmentStatePagerAdapter。 請檢查代碼。有兩個選項卡。我想要在列表視圖中將字符串從tab1傳遞給tab2並填充它。如何將字符串數據從一個片段添加到另一個片段中的列表視圖?

package com.example.salman.q85; 

import android.os.Bundle; 
import android.support.design.widget.TabLayout; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     tabLayout.addTab(tabLayout.newTab().setText("Tab1")); 
     tabLayout.addTab(tabLayout.newTab().setText("Tab2")); 
     tabLayout.setTabGravity(tabLayout.GRAVITY_FILL); 

     final ViewPager viewPager= (ViewPager)findViewById(R.id.pager); 
     final FAdapter adapter= new FAdapter(getSupportFragmentManager(),tabLayout.getTabCount()); 
     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
     viewPager.setAdapter(adapter); 
     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 


package com.example.salman.q85; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 

/** 
* Created by Salman on 11/23/2015. 
*/ 
public class Fragment1 extends android.support.v4.app.Fragment { 

    private static EditText editText; 

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

     View view = inflater.inflate(R.layout.frag1, container, false); 

     Button button = (Button) view.findViewById(R.id.button); 
     editText = (EditText) view.findViewById(R.id.editText); 


     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       /*String data = editText.getText().toString(); 
*//* 
       Fragment2 frag = new Fragment2(); 
       Bundle bundle = new Bundle(); 
       bundle.putString("n", data);*//* 
*/ 
       getdata(); 
       editText.setText(""); 

/* 
       String data = text.getText().toString(); 
       Fragment2 frag = (Fragment2)getFragmentManager().findFragmentById(R.id.fragment2); 
       frag.addIten(data); 
       text.setText("");; 

*/ 

      } 
     }); 
     return view; 
    } 

    public String getdata() { 
     String s; 
     s = editText.getText().toString(); 

     return s; 
    } 
} 

package com.example.salman.q85; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.util.ArrayList; 

/** 
* Created by Salman on 11/23/2015. 
*/ 
public class Fragment2 extends Fragment { 
    ListView listView; 
    ArrayList<String> items; 
    View view; 


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

     view = inflater.inflate(R.layout.frag2, container, false); 

     listView = (ListView) view.findViewById(R.id.listView); 
     items = new ArrayList<String>(); 
     listView=(ListView)view.findViewById(R.id.listView); 
     listView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items)); 

     Fragment1 f1= new Fragment1(); 
     String data=f1.getdata(); 


     items.add(data); 
     return view; 
    } 
    /* public void adddata(String s){ 

     ArrayAdapter<String> adapter= (ArrayAdapter<String>)listView.getAdapter(); 
     adapter.notifyDataSetChanged(); 

    }*/ 
} 

XML

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

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_weight="0" 
     android:hint="Name" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TransfertoanotherFragment" 
     android:id="@+id/button" 
     android:layout_weight="0" /> 
</LinearLayout> 

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

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView" 
     android:layout_weight="1" /> 
</LinearLayout> 

>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/tab_layout" /> 
</LinearLayout> 
+0

你在哪裏添加/替換片段1與片段2 ...? –

+0

可能重複的[如何傳遞片段之間的數據](http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments) – Bonatti

+0

@SarithG我不想更換片段。它有兩個選項卡..我只是想在列表視圖中將字符串從tab1傳遞給tab2。 –

回答

0

在Fragment2類添加構造

String data; 

public Fragment2(String data) { 
    this.data = data; 
} 

,並在片段1通過該字符串在構造

Fragment1 f1= new Fragment1(data); 
0

在您的片段2

Fragment newFragment = new Fragment1(); 
    Bundle bundle = new Bundle(); 
    bundle.putStringArrayList("strings", getData()); 
    fragment.setArguments(bundle); 
    FragmentManager fragmentManager = getActivity(); 
    fragmentManager.beginTransaction().replace(R.id.activity_main, newFragment).commit(); 

要接收的包在片段一個

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     view = inflater.inflate(R.layout.fragment1, container, false);false); 
     if (savedInstanceState == null) 
     { 
      Bundle extras = getArguments(); 
      dataStrings = extras.getStringArrayList("strings"); 
     } 
    return view; 
} 
相關問題