数组和适配器无法调用Void Fragment

时间:2016-07-07 12:37:12

标签: android arrays fragment

请帮帮我。我的家庭片段是空白的。我无法将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;
    }
}

}

3 个答案:

答案 0 :(得分:0)

在公共类中声明FeedAdapter adapterArrayList<Post> posts作为全局变量。必须在public void onActivityCreated(Bundle savedInstanceState)

之外宣布它们

答案 1 :(得分:0)

这里发生了什么:

  • 在onActivityCreated中,您声明局部变量而不为它们赋值。因为它们是本地的,所以一旦方法终止它们就会消失。将方法上方的变量声明为类变量,以便稍后在null
  • 中访问它们
  • 您没有为变量onFeedlistView分配值。我假设在其他地方,通常的做法是在adapter中执行此操作,您为片段设置主视图。如果您不知道如何操作,请参阅this question。然后你可以使用onViewCreated加载listView,假设listView在你在片段中膨胀的布局中。然后,您必须创建一个新的findViewById并使用FeedAdapter
  • 将其分配给listView
祝你好运!

答案 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