我想像在recyclerview中显示的搜索结果之后一样,打开详细活动。由于搜索结果已在“ Recycler”视图中更新,因此无法打开“详细信息”活动的onclick操作。
如何在搜索结果更新后的recyclerview中实现onclick
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();
}
}
}
答案 0 :(得分:1)
尝试将OnitemClickListener
的界面更改为
public interface OnItemClickListener{
void onItemClick(int id);
}
然后,在EnquiryViewHolder itemView.onClick
中,仅发送ID而不是位置,您就不会使用它。
答案 1 :(得分:0)
在适配器类中创建一个接口。
public interface SelectedUser{
void selectedUser(UserModel userModel);
}
在您的活动中实现接口并覆盖selectedUser(UserModel userModel)。
并将数据传递给您的其他活动
@Override
public void selectedUser(UserModel userModel) {
startActivity(new Intent(MainActivity.this, SelectedUserActivity.class).putExtra("data",userModel));
}