CardLayout和循环视图无法在viewholder中获取我的视图元素

时间:2017-05-25 09:06:34

标签: android android-recyclerview adapter android-cardview

我正在尝试使用适配器构建一个简单的回收视图,我正在遵循this教程来实现这一目标。

当我在我的适配器设置工作时,我得到一个错误,说我需要将我的所有元素(TextViews和那些)转换为静态字段,作者不需要它,我不知道为什么。

这是我的代码:

#!/usr/bin/python

import ldap3

SERVER='127.0.0.1'
BASEDN="DC=domain,DC=com"
USER="test-ms-ad@domain.com"
CURREENTPWD="current_password"
NEWPWD="new_password"

SEARCHFILTER='(&(|(userPrincipalName='+USER+')(samaccountname='+USER+')(mail='+USER+'))(objectClass=person))'

USER_DN=""

ldap_server = ldap3.Server(SERVER, get_info=ldap3.ALL)
conn = ldap3.Connection(ldap_server, USER, CURREENTPWD, auto_bind=True)
conn.start_tls()

print conn

conn.search(search_base = BASEDN,
         search_filter = SEARCHFILTER,
         search_scope = ldap3.SUBTREE,
         attributes = ['cn', 'givenName'],
         paged_size = 5)

for entry in conn.response:
    if entry.get("dn") and entry.get("attributes"):
        if entry.get("attributes").get("cn"):
            USER_DN=entry.get("dn")

print USER_DN
print ldap3.extend.microsoft.modifyPassword.ad_modify_password(conn, USER_DN, NEWPWD, CURREENTPWD,  controls=None)

问题在于:

public class SimiliarPlantsAdapter extends RecyclerView.Adapter<SimiliarPlantsAdapter.PlantViewHolder>{

ArrayList<Plant> plants = new ArrayList<Plant>();

public static class PlantViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView plantName;
    TextView plantCheck;
    ImageView plantPhoto;

    PlantViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        plantName = (TextView)itemView.findViewById(R.id.plantName);
        plantCheck = (TextView)itemView.findViewById(R.id.plantCheck);
        plantPhoto = (ImageView)itemView.findViewById(R.id.plantPhoto);
    }

}

@Override
public PlantViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.similiar_photo_row, viewGroup, false);
    PlantViewHolder pvh = new PlantViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(PlantViewHolder holder, int position) {
    PlantViewHolder.plantName.setText(plants.get(position).getSpecie());
    PlantViewHolder.plantCheck.setText("Are you sure this is the plant?");
    PlantViewHolder.personPhoto.setImageResource(plants.get(position).photoId);
}


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

public SimiliarPlantsAdapter(ArrayList<Plant> plants) {
    this.plants = plants;
}

我的 plantName和plantCheck 不起作用,我需要将初始值转换为静态字段,为什么会发生这种情况,任何提示?

由于

2 个答案:

答案 0 :(得分:0)

更改 onBindViewHolder 方法我认为您访问属性的方式 PlantViewHolder 类错了。喜欢这个

*(ptr+1) <<8 | *(ptr+2)

您不必使用; 7 : u16 len = (u16)*ptr++ <<8 | *ptr++; mov ecx, DWORD PTR _ptr$[ebp] movzx edx, BYTE PTR [ecx] shl edx, 8 mov eax, DWORD PTR _ptr$[ebp] movzx ecx, BYTE PTR [eax] or edx, ecx mov WORD PTR _len$[ebp], dx mov edx, DWORD PTR _ptr$[ebp] add edx, 1 mov DWORD PTR _ptr$[ebp], edx mov eax, DWORD PTR _ptr$[ebp] add eax, 1 mov DWORD PTR _ptr$[ebp], eax 持有者对象已经在方法内部传递尝试使用传递的参考来访问它的变量 ë

答案 1 :(得分:0)

使用PlantViewHolder's对象holder设置views属性。

更新onBindViewHolder,如下所示:

@Override
public void onBindViewHolder(PlantViewHolder holder, int position) {
    holder.plantName.setText(plants.get(position).getSpecie());
    holder.plantCheck.setText("Are you sure this is the plant?");
    holder.personPhoto.setImageResource(plants.get(position).photoId);
}