【问题标题】:Trouble with Array Adapter for JSONArrayJSONArray 的数组适配器出现问题
【发布时间】:2015-05-13 08:07:16
【问题描述】:

我无法将我的 JSON 数组加载到我的列表中。我基本上只是想让它加载任何东西,但我很难让它甚至到达 getView 部分。任何帮助将不胜感激......我在下面提供了两个相关类的代码。

我有以下主要活动:

public class MainActivity extends ActionBarActivity {

    public static PostArrayAdapter mainAdapter;
    public static ArrayList<JSONObject> postArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);
        String url ="<<QUERY>>";

        Log.v("tag", "Logged this at least");

        JsonArrayRequest jsObjRequest = new JsonArrayRequest
                (Request.Method.GET, url, new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.i("tag", "Response: " + response.toString());
                        try{
                            ArrayList<JSONObject> tempArray = new ArrayList<JSONObject>();
                            for(int i = 0; i<response.length(); i++){
                                JSONObject post = response.getJSONObject(i);
                                tempArray.add(post);
                            }
                            postArray = tempArray;
                            Log.i("tag", "temp array: " + postArray.toString());
                            mainAdapter.notifyDataSetChanged();
                        }
                        catch(JSONException e){
                            Log.e("Tag", "Error paring data "+e.toString());
                        }


                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Log.d("tag", "Error: " + error.toString());
                    }
                });

        // Add the request to the RequestQueue.
        queue.add(jsObjRequest);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            postArray = new ArrayList<JSONObject>();
            mainAdapter = new PostArrayAdapter(getActivity(), postArray);

            ListView listView = (ListView) rootView.findViewById(R.id.listview_main);
            listView.setAdapter(mainAdapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), DetailActivity.class);
                    startActivity(intent);
                }
            });

            Log.i("tag", "Logged this at least");
            return rootView;
        }
    }
}

还有下面的 ArrayAdapter 类:

公共类 PostArrayAdapter 扩展 ArrayAdapter {

private final Context context;
private final ArrayList<JSONObject> posts;

public PostArrayAdapter(Context context, ArrayList<JSONObject> posts) {
    super(context, R.layout.list_item_main, posts);
    Log.i("tag", "Got Here 0");
    Log.i("tag", "Constructor response: " + posts.toString());
    this.context = context;
    this.posts = posts;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.i("tag", "Got Here 1A");
    try {
        Log.i("tag", "Got Here 1");
        JSONObject post = (JSONObject) this.posts.get(position);
        Log.i("tag", "Got Here 2");
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.i("tag", "Got Here 3");
        Log.i("tag", "Post: " + post.toString());


        if (post.get("post_type").equals("post")) {
            Log.i("tag", "post");
            View rowView = inflater.inflate(R.layout.list_item_post, parent, false);
            TextView postText = (TextView) rowView.findViewById(R.id.postText);
            postText.setText("hello");
            return rowView;
        } else if (post.get("post_type").equals("meme")) {
            Log.i("tag", "meme");
            View rowView = inflater.inflate(R.layout.list_item_meme, parent, false);
            return rowView;
        } else if (post.get("post_type").equals("poll")) {
            Log.i("tag", "poll");
            View rowView = inflater.inflate(R.layout.list_item_poll, parent, false);
            return rowView;
        } else if (post.get("post_type").equals("reply")) {
            Log.i("tag", "reply");
            View rowView = inflater.inflate(R.layout.list_item_reply, parent, false);
            TextView replyText = (TextView) rowView.findViewById(R.id.replyText);
            replyText.setText("hello");
            return rowView;
        }
        return null;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

}

我知道 JSONArray 正在返回正确的响应。我遇到的问题是,在我收到响应后,适配器似乎没有更新值。有没有人有任何可能的解决方案?事实上,“Got Here 1A”甚至从未被调用过(可能是因为它在适配器通知更改后没有更新)

【问题讨论】:

  • getView 无法返回 null
  • 试试View rowView = inflater.inflate(R.layout.list_item_post, null, false);
  • "Got Here 1A" 甚至从未被调用过。如果我没记错的话,我认为问题出在之前

标签: java android android-arrayadapter jsonobject


【解决方案1】:

您是否将数据添加到适配器?我没有在你的代码中找到相关的方法。

mainAdapter.notifyDataSetChanged();前加mainAdapter.addAll(postArray);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    相关资源
    最近更新 更多