I am developing a simple notepad app first i used listView to show all the notes.But now I am using RecyclerView. While i am using listview i used OnItemClickListener to pass the Data to another activity for editing the note now i am confuse what to do in Recylerview
For listView i am using this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = filenames.get(position).getName();
note = filenames.get(position).getShorttext();
Alert(); // this method is in main activity
}
});*/
public void Alert()
{
final AlertDialog dialog;
View mview = getLayoutInflater().inflate(R.layout.dialog_pass,null);
final EditText mEdittext = (EditText) mview.findViewById(R.id.Epass);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setView(mview);
mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String col = mEdittext.getText().toString();
String password = dBhelper.searchpass(col);
if (col.equals(password)) {
Intent intent = new Intent(MainActivity.this,Note2.class);
intent.putExtra("Name",name);
intent.putExtra("Note",note);
startActivity(intent);
} else {
Toast temp = Toast.makeText(MainActivity.this, "Password does not match", Toast.LENGTH_SHORT);
temp.show();
}
}
});
mBuilder.setNegativeButton("Cancel",null);
mBuilder.setCancelable(false);
dialog = mBuilder.create();
dialog.show();
}
Now What Should i Do for Recycle View Please Help
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;
public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
}
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
R.layout.grid_item, parent, false);
RecycleViewHolder listHolder = new RecycleViewHolder(mainGroup);
return listHolder;
}
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
final FileName model = fileNames.get(position);
RecycleViewHolder mainHolder = (RecycleViewHolder) holder;// holder
mainHolder.title.setText(model.getName());
mainHolder.note.setText(model.getShorttext());
}
@Override
public int getItemCount() {
return (null != fileNames ? fileNames.size() : 0);
}
You can handle this using two ways
1). Gesture touch https://www.google.co.in/amp/sapandiwakar.in/recycler-view-item-click-handler/amp/
2).Using interface in adapter https://antonioleiva.com/recyclerview-listener/
I suggest second way using interface
How to use interface for recycleritemclick
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;
//declare interface
private OnItemClicked onClick;
//make interface like this
public interface OnItemClicked {
void onItemClick(int position);
}
public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
}
Now assign click to interface
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
//............//
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
}
At the end of the adapter class up from finishing bracket make one method to assign itemclick to interface
public void setOnClick(OnItemClicked onClick){
this.onClick=onClick;
}
In MainActivity.java Bind the item click with adapter
public class MainActivity extends Activity implements OnItemClicked {
private RecyclerView mRecyclerView;
private CityAdapter mAdapter;
private List<City> cities;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
mRecyclerView = (RecyclerView)findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CityAdapter(cities, R.layout.row_city, this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnClick(MainActivity.this); // Bind the listener
}
@Override
public void onItemClick(int position) {
// The onClick implementation of the RecyclerView item click
//ur intent code here
}
}
For Kotlin code please refer How to handle recyclerview item click in kotlin?
More queries? comment.
Simplest solution would be to access the "itemView" public variable of holder class and set onClickListener on this.
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NewActivity.class);
intent.putExtra("FileName", list.get(position));
context.startActivity(intent);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With