我正在使用改造构建循环视图列表,我想在操作栏上添加searchview。但我收到错误消息No adapter attached; skipping layout
这是我的mainActivity
public class ListActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<AndroidVersion> data;
private DataAdapter adapter;
private SearchView searchView;
private MenuItem searchMenuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
recyclerView = (RecyclerView) findViewById(R.id.card_recView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.learn2crack.com/android/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
data = new ArrayList<AndroidVersion>(Arrays.asList(jsonResponse.getAndroid()));
adapter.swapData(data);
Toast.makeText(ListActivity.this, " Hasil", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("ERROR", t.getMessage());
Toast.makeText(ListActivity.this, " ERROR ", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener((SearchView.OnQueryTextListener) this);
return true;
}
}
上面的代码仅用于显示searchview布局。如果有人有其他方式或其他完整的教程,请。提前谢谢
修改 这是我的适配器
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder>{
private ArrayList<AndroidVersion> android;
public DataAdapter(ArrayList<AndroidVersion> android){
this.android = android;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.nama_dokter.setText(android.get(i).getName());
viewHolder.alamat_1.setText(android.get(i).getVer());
viewHolder.alamat_2.setText(android.get(i).getVer());
viewHolder.alamat_3.setText(android.get(i).getVer());
viewHolder.total_ps.setText(android.get(i).getApi());
viewHolder.total_omzet.setText(android.get(i).getApi());
viewHolder.total_pasien.setText(android.get(i).getApi());
}
@Override
public int getItemCount() {
return android.size();
}
private ArrayList<AndroidVersion> data = new ArrayList<>();
public void swapData(ArrayList<AndroidVersion> data) {
this.data.addAll(data);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nama_dokter, alamat_1, alamat_2, alamat_3, total_ps, total_omzet, total_pasien;
public ViewHolder(View view) {
super(view);
nama_dokter = (TextView)view.findViewById(R.id.nama_dok);
alamat_1 = (TextView)view.findViewById(R.id.alamat_1);
alamat_2 = (TextView)view.findViewById(R.id.alamat_2);
alamat_3 = (TextView)view.findViewById(R.id.alamat_3);
total_ps = (TextView)view.findViewById(R.id.total_ps);
total_omzet = (TextView)view.findViewById(R.id.total_omset);
total_pasien = (TextView)view.findViewById(R.id.total_pasien);
}
}
}
答案 0 :(得分:0)
<强>更新强>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
recyclerView = (RecyclerView) findViewById(R.id.card_recView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
}
在onResponse
adapter.swapData(data)
在DataAdapter
private ArrayList<AndroidVersion> data= new ArrayList<>();
public void sawpData( ArrayList<AndroidVersion> data) {
this.data.addAll(data);
notifyDataSetChanged();
}
使用空构造函数初始化适配器或传递Context
并在onResponse
内data
设置adapter.swapData(data)
内部交换数据中的notifyDataSetChanged();
将设置列表并调用此/api/login-token
答案 1 :(得分:0)