这是我之前提问的后续问题:Parse RecyclerView Image Query
我现在有一个矛盾的nullpointer异常。我试图通过下面概述的方式从内部方法中提取列表。
public List<ParseObject> objectList;
public List<String> getData(String filter){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> downloads, com.parse.ParseException e) {
if (e == null) {
objectList=downloads;
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
int size = objectList.size();
for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}
return urlList;
}
我该如何解决这个问题?有哪些方法比我使用的更好?
编辑:
这是它正在做的事情。目前没有例外,但Recyclerview没有显示任何内容。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_events, container, false);
setList("sport", rootView);
return rootView;
}
public void set() {
}
public void setList(String filter, View view){
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(getData(filter));
recyclerView.setAdapter(myAdapter);
}
编辑2:更新的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_events, container, false);
setList("sport", rootView);
return rootView;
}
public void set() {
}
public void setList(String filter, View view){
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(getData(filter));
recyclerView.setAdapter(myAdapter);
}
private List<String> urlList = new ArrayList<>();
public List<ParseObject> objectList = new ArrayList<>();
public int size;
public List<String> getData(String filter){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> downloads, com.parse.ParseException e) {
if (e == null) {
Toast.makeText(getActivity(), "Downloads retrieved", Toast.LENGTH_SHORT).show();
objectList.addAll(downloads);
} else {
Log.d("score", "Error: " + e.getMessage());
}
size = objectList.size();
}
});
for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}
return urlList;
}
编辑3:更新了,我添加了适配器代码。
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_events, container, false);
new GetUrlListTask(this).execute("sport");
return rootView;
}
public void set() {
}
private List<String> urlList = new ArrayList<>();
public List<ParseObject> objectList = new ArrayList<>();
public int size;
public List<String> getData(String filter){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> downloads, com.parse.ParseException e) {
if (e == null) {
Toast.makeText(getActivity(), "Downloads retrieved", Toast.LENGTH_SHORT).show();
objectList.addAll(downloads);
} else {
}
size = objectList.size();
}
});
for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}
return urlList;
}
private class GetUrlListTask extends AsyncTask<String,Void,List<String>>
{
private WeakReference<Fragment> weakRef;
public GetUrlListTask(Fragment frag){
weakRef = new WeakReference< Fragment >(frag);
}
@Override
protected List<String> doInBackground(String... filters) {
return getData(filters[0]);
}
@Override
protected void onPostExecute(List<String> urlList) {
Fragment frag = weakRef.get();
if (frag != null){
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(urlList);
recyclerView.setAdapter(myAdapter);
}
}
}
我的适配器:
List<String> urls;
ViewGroup parent;
public MyAdapter(List<String> urls){
this.urls=urls;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_entry, parent);
MyViewHolder myViewHolder = new MyViewHolder(view);
this.parent=parent;
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Picasso.with(parent.getContext())
.load(urls.get(position))
.into(holder.photo);
Toast.makeText(parent.getContext(), urls.get(position), Toast.LENGTH_LONG).show();
}
@Override
public int getItemCount() {
return urls.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView photo;
public MyViewHolder(View itemView) {
super(itemView);
photo = (ImageView) itemView.findViewById(R.id.listImage);
}
}
答案 0 :(得分:1)
所以我假设你想要做的是一旦你得到urlList,你想要更新一些视图。如果是这样,那么我认为你想要做的就是在后台任务中运行整个方法,而不仅仅是这样的查询:
首先更改你的getData()方法:
public List<String> getData(String filter){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
objectList = query.find(); //I think you have to surround this by a try catch or have the method throw an exception
int size = objectList.size();
for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}
return urlList;
}
请注意,由于您正在进行网络调用,因此上述方法现在必须从后台线程运行。
然后你可以使用像AsyncTask这样的东西来更新视图:
private class GetUrlListTask extends AsyncTask<String,Void,List<String>>
{
private WeakReference<Fragment> weakRef;
public PhotoTagsFetchTask(Fragment frag){
weakRef = new WeakReference< Fragment >(frag);
}
@Override
protected Void doInBackground(String... filters) {
return getData(filters[0]);
}
@Override
protected void onPostExecute(List<String> urlList) {
Fragment frag = weakRef.get();
if (frag != null){
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(urlList);
recyclerView.setAdapter(myAdapter);
}
}
}
请注意,使用上面的弱引用是为了避免上下文泄漏(基本上避免了不必要的内存泄漏)。
你也可以这样做:
public void getData(String filter){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> downloads, com.parse.ParseException e) {
if (e == null) {
objectList=downloads;
int size = objectList.size();
for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(urlList);
recyclerView.setAdapter(myAdapter);
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
答案 1 :(得分:0)
将objectList=downloads
更改为objectList.addAll(downloads)
并在第一行添加objectList = new ArrayList<>()
。
同时从查询回调的objectList.size()
方法中的done
开始移动该部分