如何从 Bundle 中创建一个 ListView?

How to make a ListView from the Bundle?

我的登录 activity 需要 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);

如何获取片段中的包 "appointments"?提前致谢。

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 { 工具栏工具栏; 选项卡布局选项卡布局; 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);
}

}`

添加片段时遵循此

我希望你在 LoginSuccess Activity.So 中添加片段,因为 activity 得到像

这样的参数
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");
}

如果您想将数据从 activity 发送到片段,则必须使用此代码:

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

像这样从 "YourFragmentClass" 中的 onCreateView() 方法获取值:

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

首先在 LoginActivity

中执行此操作以打开 Fragment

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

    }
}

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

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>