繁体   English   中英

如何发送数组列表 <Model> 到片段适配器

[英]How to send the arraylist<Model> to Fragment adapter

我想将arraylist传递给片段适配器。 但是,我真的不明白如何使用Bundle将arraylist从主活动传递到片段适配器,任何人都可以帮忙吗? 谢谢。 我试图在我的模型中实现parcelable。 但是它无法获得Bundle extra = getIntent().getExtras(); 因为我正在尝试获取arraylist对象并将其设置在片段适配器中,所以可以解决我的问题的任何想法

package com.example.myapplication;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import java.util.ArrayList;


public class FragmentAdapter extends FragmentStatePagerAdapter{
    private ArrayList<EssayElement> essayList;


    public FragmentAdapter(FragmentManager fm, ArrayList<EssayElement> mList) {
        super(fm);
        this.essayList = mList;
    }

    @Override
    public Fragment getItem(int position) {
        SwipeFragment swipeFragment= new SwipeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("titleString", essayList.get(position).getTitle());
        bundle.putString("essayString", essayList.get(position).getEssay());
        bundle.putString("dateString", essayList.get(position).getDate());
        bundle.putString("timeString", essayList.get(position).getTime());
        swipeFragment.setArguments(bundle);
        return swipeFragment;
    }

    @Override
    public int getCount() {
        return essayList.size();
    }


}

这是主要活动

package com.example.myapplication;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity{
    private ListView listView;
    private ArrayList<EssayElement> essayList = new ArrayList<>();
    private ArrayList<String> titleList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("essayList",essayList);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        getJSON("http://10.0.2.2:8081/getAll");
    }
    public void getJSON(final String urlWebService) {
        class GetJSON extends AsyncTask<Void, Void, String> {
            @Override
            protected void onPostExecute(String s) {
                try {
                    parseJSON(s);
                    loadIntoListView(s);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected String doInBackground(Void... voids) {
                        try {
                            URL url = new URL(urlWebService);
                            HttpURLConnection con = (HttpURLConnection) url.openConnection();
                            con.setRequestMethod("GET");
                            StringBuilder sb = new StringBuilder();
                            InputStream input = con.getInputStream();
                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
                            String json;
                            while ((json = bufferedReader.readLine()) != null) {
                                sb.append(json + "\n");
                            }
                            return sb.toString().trim();
                        } catch (Exception e) {
                            return null;
                }
            }
        }
        GetJSON getJSON = new GetJSON(
        );
        getJSON.execute();
    }

    private void parseJSON(String json) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<EssayElement>>(){}.getType();
        essayList = gson.fromJson(json, type);
        for (EssayElement essayElement : essayList){
        Log.i("Message: " + "", essayElement.id + "-" + essayElement.title + "-" + essayElement.essay + "-" + essayElement.date + "-" + essayElement.time);
        }
    }


    private void loadIntoListView(String json) throws JSONException {
        Gson gson = new Gson();
        Type type = new TypeToken<List<EssayElement>>(){}.getType();
        essayList = gson.fromJson(json, type);
        for(EssayElement essayElement : essayList){
           titleList.add(essayElement.title);
        }

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , titleList);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
                Intent newActivity = new Intent(MainActivity.this,Essay.class);
                newActivity.putExtra("TITLE", essayList.get(i).title);
                newActivity.putExtra("ESSAY", essayList.get(i).essay);
                newActivity.putExtra("DATE", essayList.get(i).date);
                newActivity.putExtra("TIME", essayList.get(i).time);
                newActivity.putExtra("ID", essayList.get(i).id);
                startActivity(newActivity);
            }
        });
    }


}
getIntent() or Bundle

不适用于FragmentStatePagerAdapter,用于在活动和片段之间传递数据。 您可以简单地将任何数据传递到其构造函数中的适配器。

您可以使用静态ArrayList,如果您更改了静态arraylist,则它将在片段适配器中自动更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM