如何将EditText项添加到RecyclerView?

时间:2017-05-23 20:58:52

标签: android android-recyclerview recycler-adapter recyclerview-layout

我在Android中制作记事本应用程序,我想在一个活动中创建一个便笺,然后将其保存到另一个包含RecyclerView的活动中。目前,它只保存一个音符,每次我创建另一个音符时,它会保存在该音符上。

MainPage.java

public class MainPage extends AppCompatActivity {

private TextView mTitleHolder;
private FloatingActionButton newNote;

public RecyclerView mRecyclerView;
public NoteAdapter mAdapter;

private NotePadDB dbHelper;
private Cursor cur;

private ArrayList<String> titles;
private List<SavedNotesItems> mList;

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_page);

    Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(tb);

    mTitleHolder = (TextView) findViewById(R.id.titlePlaceHolder);
    newNote = (FloatingActionButton) findViewById(R.id.createNewNote);
    newNote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(MainPage.this, MainNote.class);
            startActivity(in);
        }
    });

    mList = new ArrayList<>();

    mRecyclerView = (RecyclerView) findViewById(noteList);
    mRecyclerView.setAdapter(new NoteAdapter(mList,getApplicationContext()));
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    String notelist = getIntent().getStringExtra("title");

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open,
            R.string.close);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    addToMain();

}
public void addToMain(){
    String notelist = getIntent().getStringExtra("title");
    SavedNotesItems list = new SavedNotesItems(notelist);
    list.setTitle(notelist);
    mList.add(list);
    //mAdapter.notifyItemInserted(mList.indexOf(list));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
     if (mDrawerToggle.onOptionsItemSelected(item)){
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

MainNote.java(基本说明)      公共类MainNote扩展了AppCompatActivity {

private FloatingActionButton mCreateNew;
private EditText noteTitle;
NotePadDB db;
public TextView mSavedNote;
private List<String> list;

public RecyclerView rec;
public NoteAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_note);



    Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(tb);

    db = new NotePadDB(this);
    list = new ArrayList<>();
    mSavedNote = (TextView) findViewById(R.id.titlePlaceHolder);
    noteTitle = (EditText) findViewById((R.id.titleOfNote));

    mCreateNew = (FloatingActionButton) findViewById(R.id.createNewNoteWhileInsideNote);
    mCreateNew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(MainNote.this, MainNote.class);
            startActivity(in);
        }
    });

    rec = (RecyclerView) findViewById(R.id.noteList);


}

public void editNote(){
    SavedNotesItems items = (SavedNotesItems)getIntent().getExtras().getSerializable("notes");
}


public void TransferText(EditText title){
    Intent i = new Intent(MainNote.this,MainPage.class);
    //i.putExtra("title",title.getText().toString());
    i.putExtra("title",title.getText().toString());
    startActivity(i);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.toolbarbtns, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.cancel:
            Intent in = new Intent(MainNote.this, MainPage.class);
            startActivity(in);

        case R.id.save:
            String note = noteTitle.getText().toString();
            list.add(note);
            Intent intent = new Intent(getApplicationContext(),MainPage.class);
            intent.putExtra("title",note);
            startActivity(intent);

    }
    return super.onOptionsItemSelected(item);
}

}

NoteAdapter.java

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>  {

private List<SavedNotesItems> listOfSavedNotes;
private Context mContext;
AdapterView.OnItemClickListener mOnItemClickListener;


public NoteAdapter(List<SavedNotesItems> listOfSavedNotes, Context context) {
    this.listOfSavedNotes = listOfSavedNotes;
    this.mContext = context;

}
//Declare all variables in List Layout
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private TextView mNoteTitle;

    public ViewHolder(View itemView) {
        super(itemView);
        mNoteTitle = (TextView) itemView.findViewById(R.id.titlePlaceHolder);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

    }
}
//Display List Layout
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_saved_notes, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    //SavedNotesItems list = listOfSavedNotes.get(position);
    holder.mNoteTitle.setText(listOfSavedNotes.get(position).getTitle());
}

@Override
public int getItemCount() {
    return listOfSavedNotes.size();
}

0 个答案:

没有答案