【问题标题】:ListAdapter for loading json file in a fragmentListAdapter 用于在片段中加载 json 文件
【发布时间】:2014-08-11 11:44:51
【问题描述】:

我正在使用一个适配器类来控制我的 Fragment 中的 ListView。我的适配器类扩展了 SimpleAdapter,当我用于活动时它工作正常。但它在一个片段中失败了。

这是我的适配器的代码:

class TrackAdapter extends SimpleAdapter {
Context context;
public TrackAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    this.context = context;
}





@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    final TrackAdapter proxy = this;
    final View trackView = super.getView(position, convertView, parent);

    final TextView name = (TextView) trackView.findViewById(R.id.name)

        }
    });
    return trackView;
}

这里是Fragment的代码:

public class Poems extends android.app.Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_movies, container, false);

    ListView listview = (ListView) rootView.findViewById(R.id.list);

    JSONObject obj = null;
    try {
        obj = new JSONObject(loadJSONFromAsset());
        JSONArray m_jArry = null;
        try {
            m_jArry = obj.getJSONArray("poems");
            ArrayList<HashMap<String, String>> formList= new ArrayList<HashMap<String, String>>();
            HashMap<String, String> m_li;

            for (int i = 0; i < m_jArry.length(); i++)
            {
                JSONObject jo_inside = m_jArry.getJSONObject(i);
                Log.d("Details-->", jo_inside.getString("name"));
                String names = jo_inside.getString("name");


                //Add your values in your `ArrayList` as below:

                m_li=new HashMap<String, String>();
                m_li.put("name", names );


                formList.add(m_li);
                //Same way for other value...

            }
            /*
            adapter = new Adapter(Names.this , formList);
            listview.setAdapter(adapter);
            Log.i("araaaay"  , formList.toString() );*/
            ListAdapter adapter1 = new TrackAdapter(
                    Poems.this, formList,
                    R.layout.list_view, new String[] { "name"}, new int[] { R.id.name});

            listview.setAdapter(adapter1);
        } catch (JSONException e) {
            e.printStackTrace();
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }




    return rootView;
}

//--------------------------------------------
public String loadJSONFromAsset() {
    String json = null;
    try {

        InputStream is = getAssets().open("myjson.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");


    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}

这个部分的错误:

ListAdapter adapter1 = new TrackAdapter(
                    Poems.this, formList,
                    R.layout.list_view, new String[] { "name"}, new int[] { R.id.name});

有没有办法改变TrackAdapter的构造函数来修复错误??如果没有,是否有任何其他示例适配器可以解决我的问题???

【问题讨论】:

  • 把 Poems.this 改成 this.getActivity()

标签: android json android-fragments fragment listadapter


【解决方案1】:

尝试改变这个:

ListAdapter adapter1 = new TrackAdapter(
                Poems.this, formList,
                R.layout.list_view, new String[] { "name"}, new int[] { R.id.name});

到这里:

 ListAdapter adapter1 = new TrackAdapter(
                getActivity(), formList,
                R.layout.list_view, new String[] { "name"}, new int[] { R.id.name});

【讨论】:

  • 感谢它现在已修复!
猜你喜欢
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多