Я хочу открыть подробное действие, как я сделал в моем обзоре переработчика, после результата поиска, показанного в обзоре переработчика.Поскольку результат поиска обновлен в представлении Recycler, онклик для открытия операции «Детализация» не работает.
Как реализовать щелчок в представлении ресайклера, которое обновляется после результата поиска
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, EnquiryAdapter.OnItemClickListener {
DbHelper mDbHelper;
public static final String FORM_ID = "form_id";
private RecyclerView mRecyclerView;
private EnquiryAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDbHelper = new DbHelper(this);
//RecyclerView Setup
mRecyclerView = findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new EnquiryAdapter(MainActivity.this,mDbHelper.getFormData());
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(MainActivity.this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
searchForm(s);
return false;
}
@Override
public boolean onQueryTextChange(String s) {
mRecyclerView.setAdapter(mAdapter);
return false;
}
});
return true;
}
@Override
public void onItemClick(int position) {
Intent intent = new Intent(this,DetailedActivity.class);
int id = EnquiryAdapter.id;
intent.putExtra(FORM_ID,id);
startActivity(intent);
}
private void searchForm(String keyword){
SQLiteDatabase db= mDbHelper.getReadableDatabase();
StringBuilder sb = new StringBuilder();
sb.append("SELECT * from ").append(FormEntry.COLUMN_TABLE_NAME).append(" where ").append(FormEntry.COLUMN_NAME_NAME).append(" = ?");
Cursor cursor = db.rawQuery(sb.toString(),new String[]{keyword});
mRecyclerView.setAdapter(new EnquiryAdapter(MainActivity.this,cursor));
}
}
EnquiryAdapter.java
Класс адаптера
public class EnquiryAdapter extends RecyclerView.Adapter<EnquiryAdapter.EnquiryViewHolder> {
private Context context;
private Cursor cursor;
private OnItemClickListener mListner;
public static int id;
public EnquiryAdapter(Context context, Cursor cursor) {
this.context = context;
this.cursor = cursor;
}
@NonNull
@Override
public EnquiryViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.enquiry_list,viewGroup,false);
return new EnquiryViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull EnquiryViewHolder enquiryViewHolder, int i) {
if(!cursor.moveToPosition(i)){
return;
}
String nameText = cursor.getString(cursor.getColumnIndex(FormContract.FormEntry.COLUMN_NAME_NAME));
String emailText = cursor.getString(cursor.getColumnIndex(FormContract.FormEntry.COLUMN_NAME_EMAIL));
int id = cursor.getInt(cursor.getColumnIndex(FormContract.FormEntry._ID));
String timestamp = cursor.getString(cursor.getColumnIndex(FormContract.FormEntry.TIMESTAMP));
Log.d("TIMESTAMP", timestamp);
enquiryViewHolder.mNameText.setText(nameText);
enquiryViewHolder.mEmailText.setText(emailText);
}
@Override
public int getItemCount() {
return cursor.getCount();
}
public class EnquiryViewHolder extends RecyclerView.ViewHolder{
public TextView mNameText, mEmailText, mDateText;
public EnquiryViewHolder(@NonNull View itemView) {
super(itemView);
mNameText = itemView.findViewById(R.id.name);
mEmailText = itemView.findViewById(R.id.email);
mDateText = itemView.findViewById(R.id.date_text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mListner != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
cursor.moveToPosition(position);
id = cursor.getInt(cursor.getColumnIndex(FormContract.FormEntry._ID));
Log.d("ID", String.valueOf(id));
mListner.onItemClick(position);
}
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListner = listener;
}
public void swapCursor(Cursor newCursor){
if(cursor != null){
cursor.close();
}
cursor = newCursor;
if(newCursor != null){
notifyDataSetChanged();
}
}
}