我正在开发一个简单的记事本应用程序,首先我使用了ListView来显示所有的笔记。但现在我正在使用RecyclerView。当我使用ListView时,我使用OnItemClickListener将数据传递到另一个活动中以编辑该笔记。现在我对RecyclerView应该怎么做感到困惑。
对于ListView,我正在使用以下内容:
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();
}
现在我该怎么做来实现 Recycle View,请帮忙。
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);
}