【问题标题】:how show mylist in activity to fragment android如何在活动中显示 mylist 以分割 android
【发布时间】:2016-07-19 16:15:05
【问题描述】:

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

正如你在我所做的工作的活动中看到的那样,我希望它在我的嘴里家庭片段显示,请指导我

【问题讨论】:

  • 那么..您希望您的活动内容显示在Fragment 上吗?你读过我指的那篇文章吗?你试过别的吗?向我们展示您的研究。
  • 请仅包含重现问题所需的代码。 stackoverflow.com/help/mcve
  • //所以..您希望您的活动内容显示在片段上?是的

标签: java android json android-fragments fragment


【解决方案1】:

您的活动中有两个选项卡(主页和新建),对吗?您想在 Home 片段中显示内容吗?如果这就是你的意思,

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

2.AsyncTask 部分无需更改任何内容,但如果您已将“this”用于 context ,请使用 getActivity() 代替。

3.将导航抽屉部分的代码和工具栏项代码保留在活动本身中。(例如.onOptionsItemSelected(),onCreateOptionsMenu()等)

4.不要忘记将 Adapter 类移动到 HomeFragment。

我认为这会有所帮助。

我已经编辑了你的代码,看看这是否有效。 公共类 HomeFragment 扩展 Fragment {

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2010-11-09
    • 2011-02-12
    • 1970-01-01
    • 2017-05-01
    相关资源
    最近更新 更多