如何在ReccyclerView的onBindViewHolder中设置setText?

时间:2016-04-12 00:58:01

标签: android android-recyclerview recycler-adapter

尝试为RecyclerView设置适配器。

Android Studio中的这两行会返回错误消息:“无法解析符号'todo'和'note1'”:

"#{reply.rsvp}"

“todo”和“note1”是通过两个EditTexts收集用户输入数据的字符串。

viewHolder也显示错误:“局部变量viewHolder是多余的”。 请指教。

完整ListAdaper.java

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

完整DataModel.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) {
    }
  }
}

2 个答案:

答案 0 :(得分:3)

您需要做的就是更改onBindViewHolder()的定义以采用ListViewHolder类型而不是通用RecyclerView.ViewHolder

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

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

或者,您可以继续使用通用RecyclerView.ViewHolder并将其强制转换为ViewHolder类型。在一个适配器中使用不同类型的行时,这种情况很常见:

@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());
}

至于“局部变量是多余的”警告,它只是希望你将它合并到一行而不是创建局部变量:

@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);
}

虽然,请注意,这实际上是一个无用的警告,我只是按原样保留代码并忽略警告。

答案 1 :(得分:1)

将其设为公共类ListViewHolder

到公共静态类ListViewHolder

然后public void onBindViewHolder(RecyclerView.ViewHolder holder,

到onBindViewHolder(ListAdapter.ViewHolder holder

你应该使用你在onBindViewHolder上创建的ViewHolder

希望这可以帮助你