1

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?

Community
  • 1
  • 1
HukeLau_DABA
  • 2,384
  • 6
  • 33
  • 51

2 Answers2

1

You have to use android.support.v7.widget.CardView. If you use Android Studio, add this to your dependencies:

compile 'com.android.support:cardview-v7:+'

in your build.gradle file. More info on Android Developer website.

Tamás Cseh
  • 3,050
  • 1
  • 20
  • 30
1

I am pretty sure that android studio support recyclerview. In your project build.gradle file add :

dependencies {
...
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
}

Here is a beautiful blog where you can learn & use recyclerview & cardview : http://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156

Hope it will be helpful.

Update

Version number can be changed overtime. Best way to get the latest :

right click on project - > open module settings - > go module section & select module -> on right side go "Dependency tab" -> click '+' button, select 'library dependency' -> search your required lib & add.

Make sure your SDK's support repository & support lib are updated.

Sayem
  • 4,891
  • 3
  • 28
  • 43