这是我的类与parse.com API一起使用。
public class ListActivity extends AppCompatActivity {
RecyclerView recyclerView;
LinearLayoutManager manager;
List<Recipe> recipes;
static int count = 0;
private ProgressDialog progressDialog;
NetworkInfo mWifi;
ConnectivityManager connManager;
NetworkInfo mInternet;
Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recipes = new ArrayList<Recipe>();
connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mInternet = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//Parse.enableLocalDatastore(getApplicationContext());
// Enable Local Datastore.
if(mWifi.isConnected() && count == 0 || mInternet.isConnected() && count == 0) {
//Parse.initialize(getApplicationContext(), "1", "2");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipes");
progressDialog = ProgressDialog.show(ListActivity.this, "",
"Update", true);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> recipesList, ParseException e) {
if (e == null) {
ParseObject.pinAllInBackground(recipesList);
Log.d("score", "Retrieved " + recipesList.size() + " recipes");
for (ParseObject obj : recipesList) {
ParseFile image = (ParseFile) obj.get("Picture");
image.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
} else {
Log.d("mytag","coldnt load picture");
}
}
});
initializeData(obj, bmp);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
initializeAdapter();
progressDialog.dismiss();
}
});
count++;
}if(!mWifi.isConnected() && !mInternet.isConnected() || count != 0) {//error on image
ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipes");
query.fromLocalDatastore();
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
if(e == null){
Log.d("score", "Retrieved " + list.size() + " recipes");
for(ParseObject obj : list){
initializeData(obj,bmp);
}
}else {
Log.d("score", "Error: " + e.getMessage());
}
initializeAdapter();
}
});
}
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
manager = new LinearLayoutManager(getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(manager);
//added to avoid no adapter set exception
recipes = new ArrayList<>();
RVAdapter adapter = new RVAdapter(recipes);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@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);
}
private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(recipes);
recyclerView.setAdapter(adapter);
}
private void initializeData(ParseObject obj,Bitmap bmp){
recipes.add(new Recipe(obj.getString("Description"), obj.getString("shortDescription"), obj.getString("Name"), bmp));
Log.d("score", Integer.toString(recipes.size()));
}
}
}
编辑3:问题是现在我有2个对象。但有时当你改变方向时它们无法出现。我认为因为适配器是在数据之前设置的。没有如何解决它。
I add my debug log to make you see what is going on.
08-30 18:14:01.943 31442-31442/by.test.roma.myapplication D/score﹕ 1
08-30 18:14:01.943 31442-31442/by.test.roma.myapplication D/score﹕ 2
08-30 18:14:02.088 31442-31442/by.test.roma.myapplication D/OpenGLRenderer﹕ Flushing caches (mode 0)
08-30 18:14:02.488 31442-31443/by.test.roma.myapplication D/dalvikvm﹕ GC_CONCURRENT freed 264K, 8% free 13277K/14279K, paused 28ms+3ms
08-30 18:14:03.113 31442-31442/by.test.roma.myapplication D/score﹕ Retrieved 2 recipes
08-30 18:14:03.403 31442-31443/by.test.roma.myapplication D/dalvikvm﹕ GC_CONCURRENT freed 367K, 8% free 13376K/14471K, paused 36ms+17ms
08-30 18:14:03.553 31442-31442/by.test.roma.myapplication D/picture﹕ 7345
08-30 18:14:03.608 31442-31442/by.test.roma.myapplication D/score﹕ 3
08-30 18:14:03.613 31442-31442/by.test.roma.myapplication D/picture﹕ 7345
08-30 18:14:03.653 31442-31442/by.test.roma.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 79K, 8% free 13435K/14599K, paused 40ms
08-30 18:14:03.658 31442-31442/by.test.roma.myapplication D/score﹕ 4