I am trying out the support library's recyclerview and cardview by creating a todo app that loads Task items persisted through a sqlite db.
Each card consists of a task description string and an 'x' image in the top right corner to remove the card from the recyclerview.
I'm testing out the first part, adding a task. When submit is pressed, I see in my debugger that the task is persisted. I then call adapter.notifyItemAdded, passing in the latest index of my data source. The app doesn't show any card after running and no errors were shown in the logcat.
MyActivity.xml:
....a bunch of RoboGuice injections
@InjectView(R.id.palettePurple)
private RelativeLayout palettePurple;
private ImageView deleteBtn;
private String currentColor;
TaskAdapter adapter;
List<Task> allTasks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// register click listeners, "this" activity will listen for clicks on these views
editBtn.setOnClickListener(this);
submitBtn.setOnClickListener(this);
deleteBtn.setOnClickListener(this);
paletteRed.setOnClickListener(this);
paletteOrange.setOnClickListener(this);
paletteGreen.setOnClickListener(this);
paletteRoyalBlue.setOnClickListener(this);
palettePurple.setOnClickListener(this);
RecyclerView recList = (RecyclerView) findViewById(R.id.tasksRV);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
allTasks = Task.getAll();
adapter = new TaskAdapter(allTasks);
recList.setAdapter(adapter);
}
....menu overrides....
@Override
public void onClick(View v) {
Log.d("TRACE", "inside onClick");
....handling other clicks....
// submit button was clicked, create a new task
else if(v.getId() == R.id.submitBtn) {
String taskDescription = taskET.getText().toString();
Task newTask = new Task(taskDescription, currentColor);
newTask.save();
int lastIndex = allTasks.size()-1;
adapter.notifyItemInserted(lastIndex);
editSection.setVisibility(View.GONE);
editBtn.setVisibility(View.VISIBLE);
}
}
}
My card xml and adapter java: How do I get the position selected in a RecyclerView?