如何将 activity 中的 mylist 显示为片段 android

how show mylist in activity to fragment android

我希望我从显示我的列表的片段中的网站获得信息 请参阅以下代码: ..................................................... ......

在我做的activity的工作中你可以看到,我希望它在我嘴里的homefragment中显示出来,请指导我

您的 activity(主页和新)中有两个选项卡,对吗?你想显示 Home fragment 中的内容吗?如果那是你的意思,

  1. 在 HomeFragment class 中执行 AsyncTask。并在OnCreateView()方法或OnViewCreated()方法中执行AsyncTask。我在您粘贴的代码中没有看到执行语句。

2.There 无需更改 AsyncTask 部分中的任何内容,但如果您已将 "this" 用于上下文,请在其位置使用 getActivity()

3.Keep 抽屉导航部分的代码和 activity 本身的工具栏项目代码。(Eg.onOptionsItemSelected()、onCreateOptionsMenu() 等)

4.Don别忘了将 Adapter class 移动到 HomeFragment。

我认为这有帮助。

我已经编辑了您的代码,看看是否可行。 public class HomeFragment 扩展片段 {

public HomeFragment() {
    // Required empty public constructor
}

private ListView lv;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    lv = (ListView) getActivity().findViewById(R.id.lv);
    new mytask().execute("http://musiqikurdi.com/api/get_recent_posts/");
}

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

    return rootView;
}

public class mytask extends AsyncTask<String,String,List<MusicModel>> {

    @Override
    protected List<MusicModel> doInBackground(String... params) {

        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();

            String line = "";
            while ((line = reader.readLine()) != null)
            {
                buffer.append(line);
            }
            String finalJSON = buffer.toString();
            JSONObject parentobject = new JSONObject(finalJSON);
            JSONArray parentarray = parentobject.getJSONArray("posts");
            StringBuffer finalbufferData = new StringBuffer();


            List<MusicModel> musicModelList = new ArrayList<>();

            for(int i=0; i < parentarray.length(); i++) {
                JSONObject finalobject = parentarray.getJSONObject(i);
                MusicModel musicModel = new MusicModel();
                musicModel.setId(finalobject.getString("id"));
                musicModel.setTitle(finalobject.getString("title").replaceAll("[\&#8211;]", ""));
                musicModel.setUrl(finalobject.getString("url").replaceAll("[\&#8211;]", ""));
                musicModel.setDate(finalobject.getString("date").replaceAll("[\&#8211;]", ""));

                List<MusicModel.category> cateList = new ArrayList<>();
                for(int j = 0; j < finalobject.getJSONArray("categories").length(); j++ )
                {
                    MusicModel.category cate = new MusicModel.category();
                    cate.setCateTitle(finalobject.getJSONArray("categories").getJSONObject(j).getString("title"));
                    cateList.add(cate);
                }
                musicModel.setListCategory(cateList);
                musicModelList.add(musicModel);
            }

            return musicModelList;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }finally {
            if(connection != null) {
                connection.disconnect();
            }
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }

    @Override
    protected void onPostExecute(List<MusicModel> s) {
        super.onPostExecute(s);
        MusicAdapter adapter = new MusicAdapter(getActivity(),R.layout.row,s);
        lv.setAdapter(adapter);
    }
}
public class MusicAdapter extends ArrayAdapter {
    private List<MusicModel> musicModelList;
    private int resource;
    private LayoutInflater inflater;
    public MusicAdapter(Context context, int resource, List<MusicModel> objects) {
        super(getActivity(), resource, objects);
        musicModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.row,null);
        }
        ImageView img;
        TextView txt1;
        TextView txt2;
        TextView txt3;

        img = (ImageView)convertView.findViewById(R.id.img);
        txt1 = (TextView)convertView.findViewById(R.id.txt1);
        txt2 = (TextView)convertView.findViewById(R.id.txt2);
        txt3 = (TextView)convertView.findViewById(R.id.txt3);

        txt1.setText(musicModelList.get(position).getId());
        txt2.setText(musicModelList.get(position).getTitle());
        return convertView;
    }
}

}