如何在片段中访问适配器类textview

时间:2017-12-04 12:04:24

标签: android android-fragments recycler-adapter

这是我的适配器类

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {

List<ListData> listData;


public RecyclerViewAdapter(List<ListData> listData) {
    this.listData = listData;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);

    return new MyHolder(view);
}


public void onBindViewHolder(MyHolder holder, int position) {
    ListData data = listData.get(position);
    holder.name.setText(data.getName());
    holder.message.setText(data.getMessage());
}

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


class MyHolder extends RecyclerView.ViewHolder {
    TextView message, name, badge;

    public MyHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.tv_email);
        message = itemView.findViewById(R.id.tv_message);
        badge = itemView.findViewById(R.id.badge);

    }
}

这是我的片段类。

public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle b) {
    View view = inflater.inflate(R.layout.fragment_chats, group, false);
    recyclerview = view.findViewById(R.id.rview);
    badge = view.findViewById(R.id.badge);
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("message");
    hashMap = new HashMap<>();
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            list = new ArrayList<>();

            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                UserDetails userdetails = dataSnapshot1.getValue(UserDetails.class);
                String name = userdetails.getName();
                String nameCaps = name.substring(0, 1).toUpperCase() + name.substring(1);
                String message = userdetails.getMessage();

                if (hashMap.containsKey(name)) {
                    if (!hashMap.get(name).equalsIgnoreCase(message)) 
                       hashMap.remove(name);
                        hashMap.put(nameCaps, message);
                    }
                } else
                    hashMap.put(nameCaps, message)
            }
            list = convertHashToObjectList(hashMap);
            RecyclerViewAdapter recycler = new RecyclerViewAdapter(list);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
            recyclerview.setLayoutManager(layoutManager);
            recyclerview.setItemAnimator(new DefaultItemAnimator());
            recyclerview.setAdapter(recycler);
        }
        @Override
        public void onCancelled(DatabaseError error) {

        }
    });
    return view;
}

private ArrayListconvertHashToObjectList(HashMap hashMap){

    ArrayList<ListData> listData = new ArrayList<>();
    for (Map.Entry<String, String> entry : hashMap.entrySet()) {
        listData.add(new ListData(entry.getKey(), entry.getValue()));
    }
 return listData;
}

我想在片段类中使用textview徽章。如何在片段类中访问适配器类textview。当我在Fragment类中实例化textview时,它会显示错误的null对象引用。

2 个答案:

答案 0 :(得分:1)

以下是相同的代码:

  for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {


                UserDetails userdetails = dataSnapshot1.getValue(UserDetails.class);
                String name = userdetails.getName();
                String nameCaps = name.substring(0, 1).toUpperCase() + name.substring(1);
                String message = userdetails.getMessage();

                if (!updateMessage.containsKey(nameCaps)) {

                    updateMessage.put(nameCaps,false);
                }
                if (hashMap.containsKey(name)) {
                    if (!hashMap.get(name).equalsIgnoreCase(message)) {

                        hashMap.remove(name);
                        hashMap.put(nameCaps, message);
                      updateMessage.put(nameCaps,true);

                    }
                } else
                    hashMap.put(nameCaps, message);



            }
            list = convertHashToObjectList(hashMap);

            RecyclerViewAdapter recycler = new RecyclerViewAdapter(list,updateMessage);

“updatemessage”是一个包含消息更改的哈希映射。

答案 1 :(得分:0)

更新RecyclerView中某些项目的方法是更新数据集,然后通知适配器数据集已更改。

首先,您需要更新List<ListData>徽章的数据。 然后,您需要通知适配器的更改。这是一个例子

代码进入你的适配器

public void updateData(List<ListData> newData) {
    listData = newData;
    notifyDataSetChanged();
}

并确保在onBindViewHolder()中设置徽章的值(如果存在)。