如何从Bundle制作ListView?

时间:2016-08-30 12:25:53

标签: android listview arraylist bundle

我的登录活动需要LoginSuccess,我的片段在那里加载。 LoginActivity.java

Intent intent = new Intent(LoginActivity.this,LoginSuccess.class);

Bundle bundle = new Bundle()
bundle.putString("first_name",jsonObject.getString("first_name"));
bundle.putString("last_name",jsonObject.getString("last_name"));
bundle.putString("username",jsonObject.getString("username"));
bundle.putString("email",jsonObject.getString("email"));
bundle.putString("mobile",jsonObject.getString("mobile"));
bundle.putString("appointments",jsonObject.getString("appointments"));

intent.putExtras(bundle);
startActivity(intent);

我如何获取捆绑包"约会"在一个片段?提前谢谢。

AppointmentsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    super.onCreate(savedInstanceState);
    Bundle bundle = getActivity().getIntent().getExtras();
    View v = inflater.inflate(R.layout.fragment_appointments, container, false);
    String [] datasource = {"appointments", bundle.getString("appointments")};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.rowlayout, R.id.txtitem, datasource);
    setListAdapter(adapter);
    setRetainInstance(true);

    Log.d("appointments", bundle.getString("appointments"));
    View appointment_date = v.findViewById(R.id.appointment_date);
    ((TextView)appointment_date).setText(bundle.getString("appointment_date"));
    return v;
}

这里是LoginSuccess.java的代码

`public class LoginSuccess扩展AppCompatActivity {     工具栏工具栏;     TabLayout tabLayout;     ViewPager viewPager;     ViewPagerAdapter viewPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_success);
    toolbar = (Toolbar)findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    tabLayout = (TabLayout)findViewById(R.id.tabLayout);
    viewPager = (ViewPager)findViewById(R.id.viewPager);
    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPagerAdapter.addFragments(new ProfileFragment(), "Profile");
    viewPagerAdapter.addFragments(new AppointmentsFragment(), "Appointments");
    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);
}

}`

3 个答案:

答案 0 :(得分:1)

首先在Fragment

中打开LoginActivity

LoginActivity.class

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.example.vostro.myapplication.fragments.FragmentDemo;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);

        Bundle bundle = new Bundle();
        bundle.putString("first_name",jsonObject.getString("first_name"));
        bundle.putString("last_name",jsonObject.getString("last_name"));
        bundle.putString("username",jsonObject.getString("username"));
        bundle.putString("email",jsonObject.getString("email"));
        bundle.putString("mobile",jsonObject.getString("mobile"));
        bundle.putString("appointments",jsonObject.getString("appointments"));

        FragmentDemo fragment = new FragmentDemo();
        fragment.setArguments(bundle);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.navigation_container, fragment);
        fragmentTransaction.commit();

    }
}

然后在你的新片段中通过这样做获取数据

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.vostro.myapplication.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FragmentDemo extends Fragment {

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

        String jsonArrayAppointments = getArguments().getString("appointments");

        try {
            JSONArray jsonArray = new JSONArray(jsonArrayAppointments);
            for (int i=0; i<jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                String yourRequiredField = jsonObject.getString("yourRequiredField");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
}

login_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#2196F3"
            android:minHeight="?attr/actionBarSize"></android.support.v7.widget.Toolbar>

       <TextView
           android:id="@+id/tv"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_gravity="center"
           android:textSize="20sp"
           android:gravity="center"
           android:text="@string/text"/>

    <!-- This will hold your Fragment -->
        <LinearLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"></LinearLayout>

</LinearLayout>

答案 1 :(得分:0)

添加片段时请按照

进行操作

我希望你在LoginSuccess Activity.So中添加片段,在该活动中获取像

这样的参数
Bundle args = getIntent().getExtras(); 

在实例化片段时,请按照

进行操作
Fragment mainFragment = new MyFragment();
mainFragment.setArguments(args)
getFragmentManager().beginTransaction().add(R.id.fragment_holder,mainFragment,"my_fragment").commit();

在您的片段中使用getArguments()方法

获取您的参数

在你的片段中使用以下代码片段来获取json的对象。

Bundle args = getArguments();
String appointments = args.getString("appointments");

JSONParser parser = new JSONParser();

try{
Object obj = parser.parse(appointments);
JSONArray array = (JSONArray)obj;
for(Object appointment : array)
{
appObject = (JSONObject) appointment;
/* for getting data here like appointment status use,*/
String status = appObject.get("appointment_status");
}

答案 2 :(得分:0)

如果要将数据从活动发送到片段,则必须使用以下代码:

Bundle bundle = new Bundle()  
bundle.putString("first_name",jsonObject.getString("first_name"));
bundle.putString("last_name",jsonObject.getString("last_name"));
bundle.putString("username",jsonObject.getString("username"));
bundle.putString("email",jsonObject.getString("email"));
bundle.putString("mobile",jsonObject.getString("mobile"));
bundle.putString("appointments",jsonObject.getString("appointments"));

YourFragmentClass yfc = new YourFragmentClass();
yfc.setArguments(bundle);

从&#34; YourFragmentClass&#34;中的onCreateView()方法中获取值像这样:

String strappointments = getArguments().getString("appointments");