我无法将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类:
public class PostArrayAdapter extends 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正在返回正确的响应。我遇到麻烦的地方是,在得到响应之后,适配器似乎没有更新值。有人有任何可能的解决方案吗?事实上,&#34; Got Here 1A&#34;永远不会被调用(可能是因为它在适配器通知更改后没有更新)
答案 0 :(得分:0)
您是否将数据添加到适配器?我没有在你的代码中找到相关的方法。
在mainAdapter.notifyDataSetChanged();
之前添加mainAdapter.addAll(postArray)
;