有我的代码。问题是当我改变方向时我得到了这样的线:没有连接适配器;跳过布局。用红色标记。但是在连接了一些毫秒布局之后。所以我想知道它是否正常,或者il应该以某种方式更改我的代码?
package by.test.roma.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
LinearLayoutManager manager;
List<Recipe> recipes;
static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recipes = new ArrayList<Recipe>();
// Enable Local Datastore.
if(count == 0) {
Parse.enableLocalDatastore(this);
Parse.initialize(this, "id", "key");
count++;
}
ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipes");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> recipesList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + recipesList.size() + " recipes");
for(ParseObject obj :recipesList) {
initializeData(obj);
}
} 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);
//initializeData();
}
@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){
recipes.add(new Recipe(obj.getString("Description"), obj.getString("shortDescription"), obj.getString("Name"), R.drawable.share));
Log.d("score", Integer.toString(recipes.size()));
}
}
My log :
08-28 14:04:28.913 14256-14256/by.test.roma.myapplication D/﹕ mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Fri May 4 10:32:42 KST 2012
08-28 14:04:28.948 14256-14256/by.test.roma.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
08-28 14:04:28.948 14256-14256/by.test.roma.myapplication E/RecyclerView﹕ No adapter attached; skipping layout
08-28 14:04:28.983 14256-14256/by.test.roma.myapplication E/RecyclerView﹕ No adapter attached; skipping layout
08-28 14:04:29.058 14256-14256/by.test.roma.myapplication D/CLIPBOARD﹕ Hide Clipboard dialog at Starting input: finished by someone else... !
08-28 14:04:29.573 14256-14256/by.test.roma.myapplication D/score﹕ Retrieved 2 recipes
08-28 14:04:29.573 14256-14256/by.test.roma.myapplication D/score﹕ 1
08-28 14:04:29.573 14256-14256/by.test.roma.myapplication D/score﹕ 2
08-28 14:04:29.603 14256-14257/by.test.roma.myapplication D/dalvikvm﹕ GC_CONCURRENT freed 299K, 8% free 13141K/14215K, paused 2ms+2ms
08-28 14:04:29.618 14256-14256/by.test.roma.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 4K, 8% free 13239K/14343K, paused 13ms
08-28 14:04:29.618 14256-14256/by.test.roma.myapplication I/dalvikvm-heap﹕ Grow heap (frag case) to 14.041MB for 417616-byte allocation
08-28 14:04:29.633 14256-14256/by.test.roma.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 8% free 13646K/14791K, paused 13ms
08-28 14:04:43.178 14256-14256/by.test.roma.myapplication D/OpenGLRenderer﹕ Flushing caches (mode 0)
08-28 14:04:43.418 14256-14256/by.test.roma.myapplication E/RecyclerView﹕ No adapter attached; skipping layout
08-28 14:04:43.428 14256-14256/by.test.roma.myapplication E/RecyclerView﹕ No adapter attached; skipping layout
08-28 14:04:43.578 14256-14256/by.test.roma.myapplication D/score﹕ Retrieved 2 recipes
08-28 14:04:43.578 14256-14256/by.test.roma.myapplication D/score﹕ 1
08-28 14:04:43.578 14256-14256/by.test.roma.myapplication D/score﹕ 2
.
.
.
答案 0 :(得分:1)
您需要为要绘制的RecyclerView创建和设置适配器:
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
manager = new LinearLayoutManager(getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(manager);
// initialize your array and set an adapter even if its empty to avoid the
// skipping layout warning
recipes = new ArrayList<>();
RVAdapter adapter = new RVAdapter(recipes);
recyclerView.setAdapter(adapter);
然后,一旦您获取并添加数据:
adapter.notifyDataSetChanged();
或效率更高:
adapter.notifyItemInserted(positionOfItem);