如何将片段中的字符串数据添加到另一个片段中的listview?

时间:2015-11-24 09:51:09

标签: android android-fragments android-listview android-arrayadapter

我已经尝试过捆绑了。我不知道它是如何工作的。请给我一些建议。它没有在列表视图中显示任何内容。 我也尝试过它而不使用FragmentStatePagerAdapter。 请检查代码。有两个选项卡。我想在列表视图中将String从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>

&GT;

<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>

2 个答案:

答案 0 :(得分:0)

在Fragment2类中添加构造函数

String data;

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

并在Fragment1中传递构造函数中的String

Fragment1 f1= new Fragment1(data);

答案 1 :(得分:0)

在你的片段中

    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( );

接收片段1中的包

    @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;
}