所以基本上代码不会产生任何错误,但每次尝试运行时我的屏幕都会变成空白。有人可以查看下面的代码。
我想我搞砸了recyclerView适配器?
这是我的循环器适配器
public class recylerAdapter extends RecyclerView.Adapter<recylerAdapter.ViewHolder> {
private LayoutInflater inflater;
List<ImageUrl> data;
private Context context;
public recylerAdapter(Context context,List<ImageUrl> data){
inflater=LayoutInflater.from(context);
this.data=data;
this.context=context;
}
@Override
public recylerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_layout, parent, false);
ViewHolder holder= new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final recylerAdapter.ViewHolder holder, final int position) {
ImageUrl current= data.get(position);
Glide.with(context)
.load(current.Url)
.override(100,100)
.into(holder.imageView);
}
@Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
imageView=(ImageView)itemView.findViewById(R.id.imageView);
}
}
}
这是mainActivity
public class MainActivity extends ActionBarActivity {
private recylerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
adapter= new recylerAdapter(this,getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public static List<ImageUrl> getData(){
final List<ImageUrl> data= new ArrayList<>();
ParseQuery<ParseObject>query= ParseQuery.getQuery("TestObject");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e==null && objects.size()>0){
for (ParseObject object:objects){
ParseFile file= object.getParseFile("image");
ImageUrl current= new ImageUrl();
current.Url=file.getUrl();
data.add(current);
}
}
}
});
return data;
}
@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);
}
}
thi 是我的RecyclerView数据的POJO类
public class ImageUrl {
public String Url;
}
答案 0 :(得分:1)
void *
是异步方法,这意味着当方法返回但您必须等待时,您不会立即获得结果。这是findInBaackground
的目标:当结果可用时调用其FindCallBack
方法。
您的错误是您在done
返回后使用data
列表的内容,但它显然是空的!
您可以使用以下
替换findInBackground
方法
getData
你的onCreate:
public void loadData(){
final List<ImageUrl> data = new ArrayList<>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e==null && objects.size()>0){
for (ParseObject object:objects){
ParseFile file= object.getParseFile("image");
ImageUrl current= new ImageUrl();
current.Url=file.getUrl();
data.add(current);
}
adapter = new recylerAdapter(MainActivity.this, data);
recyclerView.setAdapter(adapter);
}
}
});
}
要改进UX,您应该添加ProgressBar或仅添加“加载”文本以提醒用户数据正在加载。在private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
loadData();
}
回调中,您可以停止加载,并且数据对用户可见