我对如何通过视图模型发送新查询以进行改造调用感到困惑。
我的活动如下:
public class SearchActivity extends AppCompatActivity {
RecyclerViewAdapter adapter;
Button filter_button;
RecyclerView recyclerView;
EditText currentSearch;
ImageButton imageButton;
List<Hits> mHits = new ArrayList<>();
String category;
String order;
String query;
String editor_choice;
String type;
ViewModel model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
filter_button = findViewById(R.id.button_filter);
imageButton = findViewById(R.id.imageButton_search);
currentSearch = findViewById(R.id.et_currentsearch);
recyclerView = findViewById(R.id.recyclerview);
// getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
final Intent intent = getIntent();
category = intent.getStringExtra("category");
order = intent.getStringExtra("order");
query = intent.getStringExtra("searchQuery");
editor_choice = intent.getStringExtra("editor");
type = intent.getStringExtra("type");
currentSearch.setText(query);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setHasFixedSize(true);
adapter = new RecyclerViewAdapter(this, mHits);
recyclerView.setAdapter(adapter);
model = ViewModelProviders.of(this, new MyViewModelFactory(this.getApplication()
, category, type, editor_choice, order, query)).get(ViewModel.class);
model.getList().observe(this, new Observer<List<Hits>>() {
@Override
public void onChanged(List<Hits> hits) {
adapter = new RecyclerViewAdapter(SearchActivity.this, hits);
recyclerView.setAdapter(adapter);
}
});
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newSearch();
}
});
filter_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(SearchActivity.this, MainActivity.class);
intent1.putExtra("query", currentSearch.getText().toString().trim());
startActivity(intent1);
}
});
}
这是我正在使用的方法:
void newSearch() {
String newSearch = currentSearch.getText().toString();
Log.d("___", "new Search : " + newSearch);
ViewModel model1 = ViewModelProviders.of(this, new MyViewModelFactory(this.getApplication()
, category, "photo", "false", "popular", newSearch)).get(ViewModel.class);
model1.getList().observe(this, new Observer<List<Hits>>() {
@Override
public void onChanged(List<Hits> hits) {
adapter = new RecyclerViewAdapter(SearchActivity.this, hits);
recyclerView.setAdapter(adapter);
}
});
}
}
“日志中的newSearch”显示所需的查询,但recyler视图仍显示先前获取的内容。
我的ViewModelFactory类:
public class MyViewModelFactory extends
ViewModelProvider.AndroidViewModelFactory {
private Application context;
private String category;
private String image_type;
private String editor_choice;
private String orderBy;
private String query;
public MyViewModelFactory(@NonNull Application application,String category, String image_type,
String editor, String orderBy, String query) {
super(application);
this.category = category;
this.image_type = image_type;
this.editor_choice = editor;
this.orderBy = orderBy;
this.query = query;
this.context=application;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new com.example.picsearch.ViewModel(context,category,image_type
,editor_choice,orderBy,query);
}
}
我是编程新手,请告诉我如何使用currentSearch文本作为新Retrofit调用的查询。