3

Trying to set up an Adapter for a RecyclerView.

These two lines in Android Studio return an error message: "Cannot resolve symbol 'todo' and 'note1'":

holder.todo.setText(dbList.get(position).getTodo());
holder.note1.setText(dbList.get(position).getNote1());

"todo" and "note1" are Strings that collect data input from the user via two EditTexts.

Also viewHolder is showing an error: "Local variable viewHolder is redundant". Please advise.

Full ListAdaper.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {

List<DataModel> dbList = new ArrayList<>();
Context context;
LayoutInflater mLayoutInflater;

public ListAdapter(Context context, List<DataModel> dbList) {
    this.context =  context;
    this.dbList = dbList;
    mLayoutInflater = LayoutInflater.from(context);
}

@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View convertView = mLayoutInflater.inflate(R.layout.singlecard_layout, parent, false);
    ListViewHolder viewHolder = new ListViewHolder(convertView);
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        holder.todo.setText(dbList.get(position).getTodo());
        holder.note1.setText(dbList.get(position).getNote1());
}

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

public class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public ListenerEditText todo,note1;

    public ListViewHolder(View itemView) {
        super(itemView);

        todo = (ListenerEditText) itemView.findViewById(R.id.CEditText);
        note1 = (ListenerEditText) itemView.findViewById(R.id.DEditText);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    }
  }
}

Full DataModel.java

public class DataModel {

private String todo;
private String note1;

public String getTodo() {
    return todo;
}

public void setTodo(String todo) {
    this.todo = todo;
}

public String getNote1() {
    return note1;
}

public void setNote1(String note1) {
    this.note1 = note1;
}    
AJW
  • 1,578
  • 3
  • 36
  • 77

2 Answers2

3

All you need to do is change the definition of onBindViewHolder() to take your ListViewHolder type instead of the generic RecyclerView.ViewHolder:

@Override
public void onBindViewHolder(ListViewHolder holder, int position) {

        holder.todo.setText(dbList.get(position).getTodo());
        holder.note1.setText(dbList.get(position).getNote1());
}

Alternatively, you can keep using the generic RecyclerView.ViewHolder and cast it to your ViewHolder type. This is common when using different types of rows in one adapter:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ListViewHolder listViewHolder = (ListViewHolder) holder;

        listViewHolder.todo.setText(dbList.get(position).getTodo());
        listViewHolder.note1.setText(dbList.get(position).getNote1());
}

As for the "Local variable is redundant" warning, it just wants you consolidate this to one line instead of creating a local variable:

@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View convertView = mLayoutInflater.inflate(R.layout.abc_action_bar_title_item, parent, false);
    return new ListViewHolder(convertView);
}

Although, note that this is really a useless warning, I would just keep the code as-is and ignore the warning.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Ok, wow I clearly have been staring at the screen too long. Make it specific to the ListViewHolder where I reference the Strings! Thank you. Any thoughts on why the viewHolder object is throwing "Local variable is redundant"? – AJW Apr 12 '16 at 01:17
  • It's this line: "ListViewHolder viewHolder = new ListViewHolder(convertView);" – AJW Apr 12 '16 at 01:22
  • It is defined in that line and then "return viewHolder" in the next line and that is it in the file. – AJW Apr 12 '16 at 01:29
  • I did a clean and then a rebuild. Same issue. I then changed to a new object name and same error. In the meantime, answer upvoted and accepted. Not sure what to try next. – AJW Apr 12 '16 at 01:37
  • @DN There we go, that did it. You are a gentleman and a scholar. App is still crashing due to something in OnBindViewHolder() so if I can solve that I should be good to go. – AJW Apr 12 '16 at 01:41
1

make this a public class ListViewHolder

to public static class ListViewHolder

then public void onBindViewHolder(RecyclerView.ViewHolder holder,

to onBindViewHolder(ListAdapter.ViewHolder holder

you should use the ViewHolder you created on onBindViewHolder

hope this helps you out

dione llorera
  • 357
  • 4
  • 14