请帮帮我。我的家庭片段是空白的。我无法将JSON数组打印到我的Home Fragment。我希望ListView显示来自API的Json数组的数据,我如何使用片段从Json放置数组数据。索里为我的英语。
我正在使用Fragment:
public class HomeFragment extends Fragment implements OnFeedListener{
ListView listView;
FeedAdapter adapter;
ArrayList<Post> posts;
View myView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.home, container, false);
return myView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onFeed(JSONArray array) {
posts = new ArrayList<>();
int lenght = array.length();
for(int i = 0; i < lenght; i++)
{
JSONObject object = array.optJSONObject(i);
Post post = new Post(object.optString("title"), object.optString("excerpt"), object.optString("thumbnail"));
posts.addAll(posts);
}
adapter.addAll(posts);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
FeedTask task = new FeedTask(this);
task.execute("http://indo-coc.com/api/get_recent_posts/");
}
public class FeedTask extends AsyncTask<String, Void, JSONArray>
{
private OnFeedListener listener;
public FeedTask(OnFeedListener listener)
{
this.listener = listener;
}
@Override
protected JSONArray doInBackground(String... params)
{
String url = params[0];
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request request = builder.url(url).build();
try {
Response response = client.newCall(request).execute();
String json = response.body().string();
try
{
JSONObject object = new JSONObject(json);
JSONArray array = object.optJSONArray("posts");
return array;
}
catch (JSONException e)
{
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONArray array) {
super.onPostExecute(array);
if(null == array)
return;
if(null != listener )
listener.onFeed(array);
}
}
public class FeedAdapter extends ArrayAdapter<Post>
{
private int resource;
public FeedAdapter(Context context, int resource) {
super(context, resource);
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Convert View -> Reuse
if (null==convertView)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource, null);
}
// Binding Data
Post post = getItem(position);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView desc = (TextView) convertView.findViewById(R.id.description);
title.setText(post.title);
desc.setText(post.description);
return convertView;
}
}
public class Post
{
public String title;
public String description;
public String thumbnail; //URL
public Post(String title, String desc, String thumbnail)
{
this.title = title;
this.description = desc;
this.thumbnail = thumbnail;
}
}
}
答案 0 :(得分:0)
在公共类中声明FeedAdapter adapter
和ArrayList<Post> posts
作为全局变量。必须在public void onActivityCreated(Bundle savedInstanceState)
答案 1 :(得分:0)
这里发生了什么:
null
onFeed
和listView
分配值。我假设在其他地方,通常的做法是在adapter
中执行此操作,您为片段设置主视图。如果您不知道如何操作,请参阅this question。然后你可以使用onViewCreated
加载listView,假设listView在你在片段中膨胀的布局中。然后,您必须创建一个新的findViewById
并使用FeedAdapter
答案 2 :(得分:0)
试试这个:
class MyFragment extends Fragment{
ListView listView;
FeedAdapter adapter;
ArrayList<Post> posts;
public void onCreate(Bundle b){
super.onCreate(b);
//My bad listview will only be assigned properly in onCreateView once the view is inflated
posts = new ArrayList<>();
adapter=new FeedAdapter(getActivity(),R.id.yourresouceid,posts); // which ever way your adapter is defined
listView.setAdapter(adapter);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void onFeed(JSONArray array) {
int lenght = array.length();
for (int i = 0; i < lenght; i++)
{
JSONObject object = array.optJSONObject(i);
posts.add(post);
}
//adapter.addAll(posts); //you don't need to do this, already done when creating your adapter
adapter.notifyDataSetChanged();
}
}
说明:onActivityCreated的范围仅限于其结束括号。内部声明的任何外部功能都无法看到。有关其工作原理的更多详细信息,请参阅java中的范围解析。
此外,您必须在使用它们之前对其进行初始化。只是声明它并尝试使用它将给出NullPointerExceptions